如何使用xmpp连接Facebook聊天,我想输入朋友的用户名,然后聊天显示SASL身份validation失败

我能够连接xmpp用于gtalk,但我不知道如何连接xmpp用于Facebook聊天,我搜索了很多,然后我写了一些代码,它也没有用,

现在我正在尝试这样,用户需要键入他的用户ID和密码,然后用户必须键入他的朋友用户名和消息,然后聊天。

XMPPClient.java

public class XMPPClient extends Activity { private ArrayList 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 adapter = new ArrayAdapter(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 props = new HashMap(); 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 props = new HashMap(); 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 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 getQueryMap(String query) { Map map = new HashMap(); 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; } } 

现在我的更新代码在这些行中抛出NPE

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

首先它将打开对话框,我们需要输入用户ID和pwd然后想要输入朋友列表的用户名然后输入msg,然后点击发送这样我正在尝试,,,我也正确输入用户名,但单击发送按钮时显示NUll ponterexception

我认为问题可能是你有两个包含相同包和类的jar文件。 因此,我强烈建议您再次检查所有jar文件并删除导致问题的任何人。

从其中一个JAR文件中删除此包将解决该问题。

有一个官方的Facebook聊天API文档 ,你需要在实现聊天客户端时遵循 – validation你需要的用户显示他的WebView表单,他在Facebook上validation你的应用程序,获得身份validation令牌,并且令牌将在X-中使用FACEBOOK-PLATFORM SASL机制。