您的位置:首页 > 移动开发 > Android开发

Xmpp协议 Asmack Android客户端 一些Bug的解决方法

2013-04-09 20:48 471 查看

Xmpp协议 Asmack Android客户端 一些Bug的解决方法

最近需要做一些有关即时通讯的项目,花了几天时间搜集了一下有关即时通讯方面的资料

最终选定Openfire做为服务器,Asmack 作为Android端的实现。

1.只能发 不能收

如果按照API上写的去做,直接在new 与某个用户的Chat 之后 addListener,结果就是只能发不能收。

按照下面这样写,可以解决。

[java]
view plaincopyprint?

ChatManager cm=conn.getChatManager();
Chat newChat = cm.createChat(
"hanchenxi@workgroup", null);
cm.addChatListener(new ChatManagerListener() {

@Override
public void chatCreated(Chat arg0, boolean arg1) {
arg0.addMessageListener(new MessageListener() {

@Override
public void processMessage(Chat arg0, Message arg1) {
if (arg1.getFrom().contains("")) {

}
Log.i("收到消息", arg1.getBody());

}
});

}
});

2.找不到密钥凭证
在连接配置中加入。

[java]
view plaincopyprint?

ConnectionConfiguration connConfig = new ConnectionConfiguration("192.168.1.116", 5222);
connConfig.setTruststorePath("/system/etc/security/cacerts.bks");
connConfig.setTruststoreType("bks");
con = new XMPPConnection(connConfig);
con.connect();

10月20日,再添加一种支持4.0以上系统的写法

[java]
view plaincopyprint?

try {
ConnectionConfiguration connConfig = new ConnectionConfiguration(
Config.getString("XmppTools.ServerAddress"), 5222); //$NON-NLS-1$
Log.i("当前操作系统版本API Level=", Build.VERSION.SDK_INT + ""); //$NON-NLS-1$ //$NON-NLS-2$
if (Build.VERSION.SDK_INT >= 14) {
connConfig.setTruststoreType("AndroidCAStore"); //$NON-NLS-1$
connConfig.setTruststorePassword(null);
connConfig.setTruststorePath(null);
} else {
connConfig.setTruststoreType("BKS"); //$NON-NLS-1$
String path = System.getProperty("javax.net.ssl.trustStore"); //$NON-NLS-1$
if (path == null)
path = System.getProperty("java.home") + File.separator //$NON-NLS-1$
+ "etc" + File.separator + "security" //$NON-NLS-1$ //$NON-NLS-2$
+ File.separator + "cacerts.bks"; //$NON-NLS-1$
connConfig.setTruststorePath(path);
}
// connConfig.setSASLAuthenticationEnabled(false);
connConfig.setReconnectionAllowed(true);
connConfig.setSecurityMode(SecurityMode.disabled);
con = new XMPPConnection(connConfig);
con.connect();

3.网络方面的异常

保证网络连接的前提下,在连接前

[java]
view plaincopyprint?

{
java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
java.lang.System.setProperty("java.net.preferIPv6Addresses",
"false");
}

4.文件传输
修改asmack源码包 org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.discoverLocalIP()方法

[java]
view plaincopyprint?

private String discoverLocalIP() throws UnknownHostException {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Logger.error("Error retrieving the local IP", ex);
}
throw new UnknownHostException("Failed to retrieve local IP");
//return InetAddress.getLocalHost().getHostAddress();
}

暂时就这么多了。

原址:http://blog.csdn.net/yaeio/article/details/7906943

特别补充,在设置configuaration的时候对认证的设置,代码如下:

connConfig.setSASLAuthenticationEnabled(false);

这个属性默认值是true,设置时得需要与服务器那边统一,如果不一致,就算用户注册成功后,登录时也会返回 server-unavailable(503)错误,我们用的是ejabberd服务器,默认设置SASL认证开启,所以开始我设置为false,怎么都无法登录,最后注释这句代码,成功登录:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: