2014-03-26 20 views
3

我能夠連接xmpp for gtalk,但我不知道如何連接Facebook聊天xmpp,我搜索了很多,然後我寫了一些代碼,它不工作,如何連接使用xmpp的Facebook聊天,我想輸入朋友的用戶名,然後顯示聊天顯示SASL身份驗證失敗

現在我想這樣,用戶需要鍵入他的用戶ID和密碼,然後用戶必須輸入他的朋友用戶名和信息,然後聊天。

XMPPClient.java

public class XMPPClient extends Activity { 

    private ArrayList<String> messages = new ArrayList(); 
    private Handler mHandler = new Handler(); 
    private SettingsDialog mDialog; 
    private EditText mRecipient; 
    private EditText mSendText; 
    private ListView mList; 
    private XMPPConnection connection; 

    /** 
    * Called with the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     Log.i("XMPPClient", "onCreate called"); 
     setContentView(R.layout.main); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     mRecipient = (EditText) this.findViewById(R.id.recipient); 
     Log.i("XMPPClient", "mRecipient = " + mRecipient); 
     mSendText = (EditText) this.findViewById(R.id.sendText); 
     Log.i("XMPPClient", "mSendText = " + mSendText); 
     mList = (ListView) this.findViewById(R.id.listMessages); 
     Log.i("XMPPClient", "mList = " + mList); 
     setListAdapter(); 

     // Dialog for getting the xmpp settings 
     mDialog = new SettingsDialog(this); 

     // Set a listener to show the settings dialog 
     Button setup = (Button) this.findViewById(R.id.setup); 
     setup.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       mHandler.post(new Runnable() { 
        public void run() 
        { 
         mDialog.show(); 
        } 
       }); 
      } 
     }); 

     // Set a listener to send a chat text message 
     Button send = (Button) this.findViewById(R.id.send); 
     send.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View view) { 
       String to = mRecipient.getText().toString(); 
       String text = mSendText.getText().toString(); 

       Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]"); 
       Message msg = new Message(to, Message.Type.chat); 
       msg.setBody(text); 
       connection.sendPacket(msg); 
       messages.add(connection.getUser() + ":"); 
       messages.add(text); 
       setListAdapter(); 
      } 
     }); 
    } 

    /** 
    * Called by Settings dialog when a connection is establised with the XMPP server 
    * 
    * @param connection 
    */ 
    public void setConnection 
      (XMPPConnection 
        connection) { 
     this.connection = connection; 
     if (connection != null) { 
      // Add a packet listener to get messages sent to us 
      PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
      connection.addPacketListener(new PacketListener() { 
       public void processPacket(Packet packet) { 
        Message message = (Message) packet; 
        if (message.getBody() != null) { 
         String fromName = StringUtils.parseBareAddress(message.getFrom()); 
         Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]"); 
         messages.add(fromName + ":"); 
         messages.add(message.getBody()); 
         // Add the incoming message to the list view 
         mHandler.post(new Runnable() { 
          public void run() { 
           setListAdapter(); 
          } 
         }); 
        } 
       } 
      }, filter); 
     } 
    } 

    private void setListAdapter 
      () { 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       R.layout.multi_line_list_item, 
       messages); 
     mList.setAdapter(adapter); 
    } 
} 

settings.java

 public class SettingsDialog extends Dialog implements android.view.View.OnClickListener { 
    private XMPPClient xmppClient; 


    public SettingsDialog(XMPPClient xmppClient) { 
     super(xmppClient); 
     this.xmppClient = xmppClient; 
    } 

    protected void onStart() { 
     super.onStart(); 
     setContentView(R.layout.settings); 
     getWindow().setFlags(4, 4); 
     setTitle("XMPP Settings"); 
     Button ok = (Button) findViewById(R.id.ok); 
     ok.setOnClickListener(this); 
    } 
    public void onClick(View v) { 
     String host = getText(R.id.host); 
     String port = getText(R.id.port); 
     String service = getText(R.id.service); 
     String username = getText(R.id.userid); 
     String password = getText(R.id.password); 

     //GTalk...Host name : talk.google.com  The port number is 5222 service name : gmail.com 
     //Yahoo...Host name : iopibm.msg.yahoo.com The default port is 5061 service name : yahoo.com 
     //Facebook Hostname : chat.facebook.com  The port number is 5222 service = chat.facebook.com for authentication SASLAuthentication.supportSASLMechanism("PLAIN", 0); 
     ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222); 
     config.setSASLAuthenticationEnabled(true); 
     XMPPConnection xmpp = new XMPPConnection(config); 
     try 
     { 
      SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class); 
      SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0); 
      xmpp.connect(); 
      xmpp.login("268651109963113", "268651109963113|zq84UUmSj7vh_I8oj7yfGLebKgY", "Application"); 
     } catch (XMPPException e) 
     { 
      xmpp.disconnect(); 
      e.printStackTrace(); 
     } 
    } 
    private String getText(int id) { 
     EditText widget = (EditText) this.findViewById(id); 
     return widget.getText().toString(); 
    } 
} 

SASLXFacebookPlatformMechanism的.java

public class SASLXFacebookPlatformMechanism extends SASLMechanism { 

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

private String apiKey = "268651109963113"; 
private String access_token = "268651109963113|zq84UUmSj7vh_I8oj7yfGLebKgY"; 

/** 
* 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 acces_token) 
     throws IOException, XMPPException { 
    if (apiKey == null || acces_token == null) { 
     throw new IllegalArgumentException("Invalid parameters"); 
    } 

    this.access_token = acces_token; 
    this.apiKey = apiKey; 
    this.hostname = host; 

    String[] mechanisms = { NAME }; 
    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 = { NAME }; 
    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"); 

     long callId = new GregorianCalendar().getTimeInMillis(); 

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

     response = composedResponse.getBytes("utf-8"); 
    } 

    String authenticationText = ""; 

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

    // 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; 
} 
} 

現在我更新的代碼在這些行

Message msg = new Message(to, Message.Type.chat); 
       msg.setBody(text); 
       connection.sendPacket(msg); 

首先,它會打開對話框,從那裏,我們需要輸入用戶名和PWD拋出NPE,然後想鍵入的好友列表中的用戶名,然後味精,然後單擊像這樣發送我正在嘗試,,,我也正確輸入用戶名,但它顯示NUll pterter異常,同時單擊發送按鈕

+0

嘗試清理您的項目,如果清潔項目不起作用,那麼只需重新啓動一次eclipse並檢查。 – InnocentKiller

+0

是我試過所有的方式花花公子@InnocentKiller,你能檢查我的代碼是正確還是錯誤 –

+0

好吧,那麼問題可能是你有2個jar文件的某個地方,包括相同的包和類。 – InnocentKiller

回答

1

我認爲問題可能是你有2個jar文件,其中包括相同的包和類。所以我強烈建議你再次檢查你的所有jar文件並刪除任何導致問題的東西。

從其中一個JAR文件中刪除此包將解決此問題。

+0

Dalvik格式失敗,錯誤1這個問題解決了,謝謝你的答案,但如何連接facebook聊天使用xmpp –

+0

@KarthickM,我對此沒有任何想法,真的很抱歉。 – InnocentKiller

0

有一個官方Facebook Chat API documentation,你實現聊天客戶端時需要遵循 - 驗證你需要告訴他的WebView形式,他在Facebook上的身份驗證您的應用程序,獲得身份驗證令牌的用戶,並該無線本地環路中使用令牌在X-FACEBOOK-PLATFORM SASL機制中。

+0

從某種意義上說你是apikey和accesstoken –

+0

請認真閱讀完整的認證流程描述:api key和訪問令牌是你在連接到facebook時所給予的,而認證令牌是facebook在給你的應用程序訪問他的Facebook帳戶。 – vitalyster

+0

yup dude我現在全部了,這是我的代碼是否正確 –