Xmpp協議 Asmack Android客戶端 一些Bug的解決方法

最近需要做一些有關即時通訊的項目,花了幾天時間蒐集了一下有關即時通訊方面的資料

最終選定Openfire做爲服務器,Asmack 作爲Android端的實現。


1.只能發 不能收

如果按照API上寫的去做,直接在new 與某個用戶的Chat 之後 addListener,結果就是隻能發不能收。

按照下面這樣寫,可以解決。

  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.找不到密鑰憑證

在連接配置中加入。

  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以上系統的寫法

  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.網絡方面的異常

保證網絡連接的前提下,在連接前

  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()方法

  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,怎麼都無法登錄,最後註釋這句代碼,成功登錄:)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章