Openfire做為服務(wù)器,Asmack 作為Android端的實(shí)現(xiàn)。
1.只能發(fā) 不能收
如果按照API上寫(xiě)的去做,直接在new 與某個(gè)用戶(hù)的Chat 之后 addListener,結(jié)果就是只能發(fā)不能收。
按照下面這樣寫(xiě),可以解決。
- 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();
一種支持4.0以上系統(tǒng)的寫(xiě)法
- try {
- ConnectionConfiguration connConfig = new ConnectionConfiguration(
- Config.getString("XmppTools.ServerAddress"), 5222);
- Log.i("當(dāng)前操作系統(tǒng)版本API Level=", Build.VERSION.SDK_INT + "");
- if (Build.VERSION.SDK_INT >= 14) {
- connConfig.setTruststoreType("AndroidCAStore");
- connConfig.setTruststorePassword(null);
- connConfig.setTruststorePath(null);
- } else {
- connConfig.setTruststoreType("BKS");
- String path = System.getProperty("javax.net.ssl.trustStore");
- if (path == null)
- path = System.getProperty("java.home") + File.separator
- + "etc" + File.separator + "security"
- + File.separator + "cacerts.bks";
- connConfig.setTruststorePath(path);
- }
-
- connConfig.setReconnectionAllowed(true);
- connConfig.setSecurityMode(SecurityMode.disabled);
- con = new XMPPConnection(connConfig);
- con.connect();
3.網(wǎng)絡(luò)方面的異常
保證網(wǎng)絡(luò)連接的前提下,在連接前
- {
- 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");
-
- }
暫時(shí)就這么多了。往后發(fā)現(xiàn)繼續(xù)更新。一直到這個(gè)項(xiàng)目完畢。修改后的asmack.jar 可以在我的資源里面下載
http://download.csdn.net/detail/yaeio/4525142
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。