最近需要做一些有關即時通訊的項目,花了幾天時間搜集了一下有關即時通訊方面的資料
最終選定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,怎麼都無法登入,最後注釋這句代碼,成功登入:)