我正在为FACEBOOK创建 XMPP 客户端。我为 gmail 做了这个,现在我必须为 FaceBook 创建相同的内容。我为此搜索了很多代码,但我仍然遇到这种类型的Not connected to server
错误service-unavailable(503)
在这里,我正在分享我所做的代码。
public class ClientJabberActivity extends Activity {
ArrayList<String> m_discussionThread;
ArrayAdapter<String> m_discussionThreadAdapter;
XMPPConnection m_connection;
private Handler m_handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_handler = new Handler();
try {
initConnection();
} catch (XMPPException e) {
e.printStackTrace();
}
final EditText recipient = (EditText) this.findViewById(R.id.recipient);
final EditText message = (EditText) this.findViewById(R.id.message);
ListView list = (ListView) this.findViewById(R.id.thread);
m_discussionThread = new ArrayList<String>();
m_discussionThreadAdapter = new ArrayAdapter<String>(this,
R.layout.multi_line_list_item, m_discussionThread);
list.setAdapter(m_discussionThreadAdapter);
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String to = recipient.getText().toString();
String text = message.getText().toString();
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
m_connection.sendPacket(msg);
m_discussionThread.add(" Me : ");
m_discussionThread.add(text);
m_discussionThreadAdapter.notifyDataSetChanged();
}
});
}
private void initConnection() throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration(
"chat.facebook.com", 5222, "chat.facebook.com");
config.setSASLAuthenticationEnabled(true);
m_connection = new XMPPConnection(config);
try {
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",
SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
m_connection.connect();
m_connection.login(apiKey + "|" + sessionKey, sessionSecret, "Application");
} catch (XMPPException e) {
m_connection.disconnect();
e.printStackTrace();
}
Presence presence = new Presence(Presence.Type.available);
m_connection.sendPacket(presence);
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
m_connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message
.getFrom());
m_discussionThread.add(fromName + ":");
m_discussionThread.add(message.getBody());
m_handler.post(new Runnable() {
public void run() {
m_discussionThreadAdapter.notifyDataSetChanged();
}
});
}
}
}, filter);
ChatManager chatmanager = m_connection.getChatManager();
chatmanager.addChatListener(new ChatManagerListener() {
public void chatCreated(final Chat chat,
final boolean createdLocally) {
chat.addMessageListener(new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: "
+ (message != null ? message.getBody() : "NULL"));
Log.i("CHAT USER",
"Received message is: " + message.getBody());
}
});
}
});
}
}
还有这个类SASLXFacebookPlatformMechanism
我怎样才能像这样登录xmpp.login(apiKey + "|" + sessionKey, sessionSecret, "Application");
我知道如何获取 facebook 的 accessToken,应用程序密钥。我不知道 sessionKey、sessionSecret 如何获取这些值以及如何解决这个问题。
如果我使用xmpp.login(apiKey, accessToken, "Application");
我会收到此错误--IllegalArgumentException: API key or session key is not present
编辑:最后我从 Amal 解决方案中得到了解决方案:xmpp.login(apiKey, accessToken, "Application");