2012-05-10 2 views
0

안드로이드 입문, 이제는 asmack을 사용하여 페이스 북 채팅 클라이언트를 개발하려고합니다. 다음 코드를 사용하여 로그인 할 수 있습니다. // 연결 만들기 및 권한 부여를 위해 FBMessageListener 메시지 리스너 클래스 공공 문자열) (연결된다 XMPPException을 던졌습니다 {안드로이드에서 xmpp를 사용하는 안드로이드 - 페이스 북 채팅 클라이언트

ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); // This is to avoid the keystore exception. 
    config.setSASLAuthenticationEnabled(true); 
    config.setDebuggerEnabled(true); 
    config.setTruststorePath("/system/etc/security/cacerts.bks"); 
    config.setTruststorePassword("changeit"); 
    config.setTruststoreType("bks"); 

    connection = new XMPPConnection(config); 
    mFbml = new FBMessageListener(connection); 

    try 
    {  
     SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", CustomSASLDigestMD5Mechanism.class);  

     connection.connect(); 

    } 
    catch (XMPPException e) 
    {  
     connection.disconnect();  
     e.printStackTrace(); 
    } 

    return connection.getConnectionID(); 
}  
and for login 
connection.login(userName, password, FB_XMPP_HOST); 
But the problem is the message is not sending . There is no error or exception in the logcat .That is why i am struggling !! This is my code to send the message.. 
ChatManager chatManager = connection.getChatManager(); 
// am able to get the userjid .. 
public void sendMessage() throws XMPPException { 
    Chat chat = chatManager.createChat("userjid", mFbml); 
    String text = "Testing client"; 
    Log.v(" Successsssssss===========", "Your Messge "+text+"has been sent"); 
    chat.sendMessage(text); 
    } 
In Logcat showing when sending the 
message //userjid mentioned above 
<message id="f4zMl-13" to="userjid" type="chat"><body>TEstingggggggggg</body> <thread>QwLOE5</thread></message> 
Please help me to do the chatting.!! 
+0

[이 답변] (http://stackoverflow.com/a/11233702/1318048)는 문제를 해결합니다. –

+0

이봐 봐 [이 링크] [1] u는 도와 줄 것이다 [http://stackoverflow.com/questions/11395185/xmpp-facebook-not-send-message/11590238#11590238][2] [1] : http://stackoverflow.com/questions/11395185/xmpp-facebook-not-send-message/11590238#11590238 [2] : http://stackoverflow.com/questions/11395185/xmpp- facebook-not-send-message/11590238 # 11590238 –

답변

0

당신은 XMPP 클라이언트 API의

Smack

요이있는 SMACK.JAR을 사용할 수 있습니다 당신은 안드로이드를위한 XMPP 클라이언트를이 곳에서 찾을 수 있습니다 link

+0

안녕하세요, 제 질문에 답해 주셔서 감사합니다.하지만 이미 링크를 시도했지만, 저에게는 효과가 없었습니다. 나는 페이스 북 채팅이 다른 것 같아요 !! 작업 코드가 있으면 게시하십시오. 감사합니다. –

0

먼저 SASLXFacebookPlatformMechanism 클래스를 편집하십시오. 이 코드를 복사하여 붙여 넣으십시오.

package com.facebook.android; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.net.URLEncoder; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.util.GregorianCalendar; 
import java.util.HashMap; 
import java.util.Map; 

import org.apache.harmony.javax.security.auth.callback.CallbackHandler; 
import org.apache.harmony.javax.security.sasl.Sasl; 
import org.jivesoftware.smack.SASLAuthentication; 
import org.jivesoftware.smack.XMPPException; 
import org.jivesoftware.smack.sasl.SASLMechanism; 
import org.jivesoftware.smack.util.Base64; 

import android.util.Log; 

public class SASLXFacebookPlatformMechanism extends SASLMechanism { 

    private static final String NAME    = "X-FACEBOOK-PLATFORM"; 

    private String    apiKey   = ""; 
    private String    accessToken  = ""; 

    /** 
    * Constructor. 
    */ 
    public SASLXFacebookPlatformMechanism(SASLAuthentication saslAuthentication) { 
     super(saslAuthentication); 
    } 

    @Override 
    protected void authenticate() throws IOException, XMPPException { 
     getSASLAuthentication().send(new AuthMechanism(NAME, "")); 
    } 

    @Override 
    public void authenticate(String apiKey, String host, String accessToken) throws IOException, XMPPException { 
     if (apiKey == null || accessToken == null) { 
      throw new IllegalArgumentException("Invalid parameters"); 
     } 

     this.apiKey = apiKey; 
     this.accessToken = accessToken; 
     this.hostname = host; 

     String[] mechanisms = { "DIGEST-MD5" }; 
     Map<String, String> props = new HashMap<String, String>(); 
     this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this); 
     authenticate(); 
    } 

    @Override 
    public void authenticate(String username, String host, CallbackHandler cbh) throws IOException, XMPPException { 
     String[] mechanisms = { "DIGEST-MD5" }; 
     Map<String, String> props = new HashMap<String, String>(); 
     this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh); 
     authenticate(); 
    } 

    @Override 
    protected String getName() { 
     return NAME; 
    } 

    @Override 
    public void challengeReceived(String challenge) throws IOException { 
     byte[] response = null; 

     if (challenge != null) { 
      String decodedChallenge = new String(Base64.decode(challenge)); 
      Map<String, String> parameters = getQueryMap(decodedChallenge); 

      String version = "1.0"; 
      String nonce = parameters.get("nonce"); 
      String method = parameters.get("method"); 

      String composedResponse = 
       "method=" + URLEncoder.encode(method, "utf-8") + 
         "&nonce=" + URLEncoder.encode(nonce, "utf-8") + 
         "&access_token=" + URLEncoder.encode(accessToken, "utf-8") + 
         "&api_key=" + URLEncoder.encode(apiKey, "utf-8") + 
         "&call_id=0" + 
         "&v=" + URLEncoder.encode(version, "utf-8"); 
      response = composedResponse.getBytes(); 
     } 

     String authenticationText = ""; 

     if (response != null) { 
      authenticationText = Base64.encodeBytes(response); 
     } 

     // Send the authentication to the server 
     getSASLAuthentication().send(new Response(authenticationText)); 
    } 

    private Map<String, String> getQueryMap(String query) { 
     Map<String, String> map = new HashMap<String, String>(); 
     String[] params = query.split("\\&"); 

     for (String param : params) { 
      String[] fields = param.split("=", 2); 
      map.put(fields[0], (fields.length > 1 ? fields[1] : null)); 
     } 

     return map; 
    } 
} 

그런 다음 활동 클래스에서이 메소드를 사용하여 페이스 북에 로그인하십시오.

private void testLogin(){ 
     ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
     config.setSASLAuthenticationEnabled(true); 
     config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled); 
     xmpp = new XMPPConnection(config); 
     SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class); 
     SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0); 
     Log.i("XMPPClient", 
       "Access token to " + mFacebook.getAccessToken()); 
     Log.i("XMPPClient", 
       "Access token to " + mFacebook.getAppId()); 
     Log.i("XMPPClient", 
       "Access token to " + mFacebook.getAccessToken()); 
     try { 
      xmpp.connect(); 
      Log.i("XMPPClient", 
        "Connected to " + xmpp.getHost()); 

     } catch (XMPPException e1) { 
      Log.i("XMPPClient", 
        "Unable to " + xmpp.getHost()); 

      e1.printStackTrace(); 
     } 
     try { 
      xmpp.login(PreferenceConnector.APP_ID, mFacebook.getAccessToken()); 
      Log.i("XMPPClient", 
        " its logined "); 


     } catch (XMPPException e) { 
      e.printStackTrace(); 
     } 
    } 
관련 문제