分享互联网,转载请注明出处。
最近需要做一些有关即时通讯的项目,花了几天时间搜集了一下有关即时通讯方面的资料
最终选定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(); }
暂时就这么多了。往后发现继续更新。一直到这个项目完毕。
修改后的asmack.jar 可以在我的资源里面下载