Android 的 XMPP/facebook 聊天连接
public class ClientJabberActivity extends Activity {
private final static String SERVER_HOST = "chat.facebook.com";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "chat.facebook.com";
private final static String LOGIN = "[email protected]";
private final static String PASSWORD = "xxxxxx";
private List<String> m_discussionThread;
private ArrayAdapter<String> m_discussionThreadAdapter;
private XMPPConnection m_connection;
private Handler m_handler;
/** Called when the activity is first created. */
@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("moi :");
m_discussionThread.add(text);
m_discussionThreadAdapter.notifyDataSetChanged();
}
});
}
private void initConnection() throws XMPPException {
//Initialisation de la connexion
ConnectionConfiguration config =
new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME);
m_connection = new XMPPConnection(config);
m_connection.connect();
m_connection.login(LOGIN, PASSWORD);
Presence presence = new Presence(Presence.Type.available);
m_connection.sendPacket(presence);
//enregistrement de l'écouteur de messages
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);
}
}
这段代码我用来与 facebook 聊天建立连接,但无法建立连接,我不知道为什么。它与 Gmail 配合良好
02-18 16:06:20.178: ERROR/AndroidRuntime(453): java.lang.IllegalStateException: Not connected to server.
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at org.jivesoftware.smack.XMPPConnection.sendPacket(XMPPConnection.java:665)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at com.developpez.florentgarin.android.ClientJabberActivity$1.onClick(ClientJabberActivity.java:61)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.view.View.performClick(View.java:2485)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.view.View$PerformClick.run(View.java:9080)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.os.Handler.handleCallback(Handler.java:587)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.os.Looper.loop(Looper.java:123)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.app.ActivityThread.main(ActivityThread.java:3647)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at dalvik.system.NativeStart.main(Native Method)
02-18 16:06:43.167: ERROR/installd(35): eof
02-18 16:06:43.167: ERROR/installd(35): failed to read size
02-18 16:07:11.857: ERROR/Zygote(470): setreuid() failed. errno: 17
02-18 16:07:16.408: ERROR/BatteryService(477): usbOnlinePath not found
02-18 16:07:16.408: ERROR/BatteryService(477): batteryVoltagePath not found
02-18 16:07:16.408: ERROR/BatteryService(477): batteryTemperaturePath not found
02-18 16:07:16.477: ERROR/SurfaceFlinger(477): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
02-18 16:07:16.758: ERROR/SensorService(477): couldn't open device for module sensors (Invalid argument)
public class ClientJabberActivity extends Activity {
private final static String SERVER_HOST = "chat.facebook.com";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "chat.facebook.com";
private final static String LOGIN = "[email protected]";
private final static String PASSWORD = "xxxxxx";
private List<String> m_discussionThread;
private ArrayAdapter<String> m_discussionThreadAdapter;
private XMPPConnection m_connection;
private Handler m_handler;
/** Called when the activity is first created. */
@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("moi :");
m_discussionThread.add(text);
m_discussionThreadAdapter.notifyDataSetChanged();
}
});
}
private void initConnection() throws XMPPException {
//Initialisation de la connexion
ConnectionConfiguration config =
new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME);
m_connection = new XMPPConnection(config);
m_connection.connect();
m_connection.login(LOGIN, PASSWORD);
Presence presence = new Presence(Presence.Type.available);
m_connection.sendPacket(presence);
//enregistrement de l'écouteur de messages
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);
}
}
this code i used to make connection with facebook chat, but the connection can not be established i don't know why . it works well with gmail
02-18 16:06:20.178: ERROR/AndroidRuntime(453): java.lang.IllegalStateException: Not connected to server.
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at org.jivesoftware.smack.XMPPConnection.sendPacket(XMPPConnection.java:665)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at com.developpez.florentgarin.android.ClientJabberActivity$1.onClick(ClientJabberActivity.java:61)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.view.View.performClick(View.java:2485)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.view.View$PerformClick.run(View.java:9080)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.os.Handler.handleCallback(Handler.java:587)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.os.Looper.loop(Looper.java:123)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at android.app.ActivityThread.main(ActivityThread.java:3647)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 16:06:20.178: ERROR/AndroidRuntime(453): at dalvik.system.NativeStart.main(Native Method)
02-18 16:06:43.167: ERROR/installd(35): eof
02-18 16:06:43.167: ERROR/installd(35): failed to read size
02-18 16:07:11.857: ERROR/Zygote(470): setreuid() failed. errno: 17
02-18 16:07:16.408: ERROR/BatteryService(477): usbOnlinePath not found
02-18 16:07:16.408: ERROR/BatteryService(477): batteryVoltagePath not found
02-18 16:07:16.408: ERROR/BatteryService(477): batteryTemperaturePath not found
02-18 16:07:16.477: ERROR/SurfaceFlinger(477): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
02-18 16:07:16.758: ERROR/SensorService(477): couldn't open device for module sensors (Invalid argument)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改:
至:
嗯,不完全是;)。不过你明白了
。facebook 的 XMPP 规范不包括用于登录的@server.here。将其包含在您的用户名字符串中或完全删除您的服务器字符串。
Change:
To:
Well not exactly ;). You get the idea though..
XMPP spec for facebook does not include the @server.here for login. Either include it in your username string or delete your server string entirely.
Facebook 登录方式不同:
Facebook login is different: