`

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

 
阅读更多

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

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

 

1.只能发 不能收

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

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

[java] view plaincopy
 
  1. ChatManager cm=conn.getChatManager();  
  2.             Chat newChat = cm.createChat(  
  3.                     "hanchenxi@workgroup"null);  
  4.             cm.addChatListener(new ChatManagerListener() {  
  5.                   
  6.                 @Override  
  7.                 public void chatCreated(Chat arg0, boolean arg1) {  
  8.                     arg0.addMessageListener(new MessageListener() {  
  9.                           
  10.                         @Override  
  11.                         public void processMessage(Chat arg0, Message arg1) {  
  12.                             if (arg1.getFrom().contains("")) {  
  13.                                   
  14.                             }  
  15.                             Log.i("收到消息", arg1.getBody());  
  16.                               
  17.                               
  18.                         }  
  19.                     });  
  20.                       
  21.                 }  
  22.             });  


2.找不到密钥凭证

在连接配置中加入。

[java] view plaincopy
 
  1. ConnectionConfiguration connConfig = new ConnectionConfiguration("192.168.1.116"5222);  
  2.             connConfig.setTruststorePath("/system/etc/security/cacerts.bks");  
  3.             connConfig.setTruststoreType("bks");  
  4.             con = new XMPPConnection(connConfig);  
  5.             con.connect();  

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

[java] view plaincopy
 
  1. try {  
  2.     ConnectionConfiguration connConfig = new ConnectionConfiguration(  
  3.             Config.getString("XmppTools.ServerAddress"), 5222); //$NON-NLS-1$  
  4.     Log.i("当前操作系统版本API Level=", Build.VERSION.SDK_INT + ""); //$NON-NLS-1$ //$NON-NLS-2$  
  5.     if (Build.VERSION.SDK_INT >= 14) {  
  6.         connConfig.setTruststoreType("AndroidCAStore"); //$NON-NLS-1$  
  7.         connConfig.setTruststorePassword(null);  
  8.         connConfig.setTruststorePath(null);  
  9.     } else {  
  10.         connConfig.setTruststoreType("BKS"); //$NON-NLS-1$  
  11.         String path = System.getProperty("javax.net.ssl.trustStore"); //$NON-NLS-1$  
  12.         if (path == null)  
  13.             path = System.getProperty("java.home") + File.separator //$NON-NLS-1$  
  14.                     + "etc" + File.separator + "security" //$NON-NLS-1$ //$NON-NLS-2$  
  15.                     + File.separator + "cacerts.bks"//$NON-NLS-1$  
  16.         connConfig.setTruststorePath(path);  
  17.     }  
  18.     // connConfig.setSASLAuthenticationEnabled(false);  
  19.     connConfig.setReconnectionAllowed(true);  
  20.     connConfig.setSecurityMode(SecurityMode.disabled);  
  21.     con = new XMPPConnection(connConfig);  
  22.     con.connect();  


 


3.网络方面的异常

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

[java] view plaincopy
 
  1. {  
  2.             java.lang.System.setProperty("java.net.preferIPv4Stack""true");  
  3.             java.lang.System.setProperty("java.net.preferIPv6Addresses",  
  4.                     "false");  
  5.         }  


4.文件传输

修改asmack源码包 org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.discoverLocalIP()方法

[java] view plaincopy
 
  1. private String discoverLocalIP() throws UnknownHostException {    
  2.         try {    
  3.             for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {    
  4.                 NetworkInterface intf = en.nextElement();    
  5.                 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {    
  6.                     InetAddress inetAddress = enumIpAddr.nextElement();    
  7.                     if (!inetAddress.isLoopbackAddress()) {    
  8.                         return inetAddress.getHostAddress().toString();    
  9.                     }    
  10.                 }    
  11.             }    
  12.         } catch (SocketException ex) {    
  13.             Logger.error("Error retrieving the local IP", ex);    
  14.         }    
  15.         throw new UnknownHostException("Failed to retrieve local IP");    
  16.         //return InetAddress.getLocalHost().getHostAddress();     
  17.     }    

 

暂时就这么多了。

 

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

 

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

                 connConfig.setSASLAuthenticationEnabled(false);

    这个属性默认值是true,设置时得需要与服务器那边统一,如果不一致,就算用户注册成功后,登录时也会返回 server-unavailable(503)错误,我们用的是ejabberd服务器,默认设置SASL认证开启,所以开始我设置为false,怎么都无法登录,最后注释这句代码,成功登录:)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics