Xmpp協議 Asmack Android用戶端 一些Bug的解決方案

來源:互聯網
上載者:User

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

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

1.只能發 不能收

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

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

[java]
view plaincopyprint?
  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 plaincopyprint?
  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 plaincopyprint?
  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 plaincopyprint?
  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 plaincopyprint?
  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,怎麼都無法登入,最後注釋這句代碼,成功登入:)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.