I'm creating XMPP client for FACEBOOK. i did this for gmail, now i have to create same for FaceBook. i googled a lot for this got some code, still i'm getting this type of errors Not connected to server
and service-unavailable(503)
here i'm sharing the code what i did.
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());
}
});
}
});
}
}
and this class SASLXFacebookPlatformMechanism
How can i login like this xmpp.login(apiKey + "|" + sessionKey, sessionSecret, "Application");
i know how to get acessToken, Application Key for facebook. i don't know about sessionKey, sessionSecret how to get those values and how to solve this problem.
If i use xmpp.login(apiKey, accessToken, "Application");
i am getting this error --IllegalArgumentException: API key or session key is not present
EDIT: Finally i got solution from Amal solution : xmpp.login(apiKey, accessToken, "Application");
You need an access token. http://developers.facebook.com/tools/access_token/
For more info http://developers.facebook.com/docs/chat/
to get access token first you have to login
SASLXFacebookPlatformMecha class
I created ChatManager class
at the end to use that FacebookChatManager class note that rosterListnr is used to get info about your friends state change implement one as you want
Connections must be made in AsyncTask for android 2.2 higher...
Dont forget to add permissions.