我正在尝试使用asmack 18连接到gtlak服务器以进行XMPP连接。
public static final String HOST = "talk.google.com";
public static final int PORT = 5222;
public static final String SERVICE = "gmail.com";
ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
//Connect to the server
connection.connect();
connection.login("xxxxxxxx@gmail.com", "password");
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
//xmppClient.setConnection(connection);
Log.d("connection","connection successfull");
} catch (XMPPException ex) {
connection = null;
Log.d("connection","connection fail");
//Unable to connect to server
}
但它会给出超时错误。 talk.google.com:5222例外:无法连接到talk.google.com:5222。 :remote-server-timeout(504) - 由:java.net.UnknownHostException:talk.google.com
引起答案 0 :(得分:2)
阅读ReadME =)http://asmack.freakempire.de/0.8.9/README
静态代码
为了在Android上正常工作,您需要注册Smack 手动提供XMPP Providers和Extensions并初始化一些静态代码 在执行任何XMPP激活之前阻塞。调用 SmackAndroid.init(上下文)(在org.jivesoftware.smack中)会这样做 对你而言。
SmackAndroid.init(getApplicationContext());
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
...