最近需要做一些有關即時通訊的項目,花了幾天時間蒐集了一下有關即時通訊方面的資料
最終選定Openfire做爲服務器,Asmack 作爲Android端的實現。
1.只能發 不能收
如果按照API上寫的去做,直接在new 與某個用戶的Chat 之後 addListener,結果就是隻能發不能收。
按照下面這樣寫,可以解決。
- 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.找不到密鑰憑證
在連接配置中加入。
- 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以上系統的寫法
- 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.lang.System.setProperty("java.net.preferIPv4Stack", "true");
- java.lang.System.setProperty("java.net.preferIPv6Addresses",
- "false");
- }
4.文件傳輸
修改asmack源碼包 org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.discoverLocalIP()方法
- 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,怎麼都無法登錄,最後註釋這句代碼,成功登錄:)