smack android集成问题记录
2016-08-25 10:18
225 查看
最近使用的项目中需要集成即时通讯,故使用smack 4.1.0 作为集成,集成方法见
https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide
由于并不懂的新xmpp协议,所有先写个Demo来测试一下功能,有一些坑,特此记录一下:
1.连接
其中setServiceName设置的是服务器地址,UserName 需要使用JID全名,由UserName@Host组成。
其次为了顺利登陆需要配置开启SASL机制,否则登陆时会报错org.jivesoftware.smack.sasl.SASLErrorException: SASLError using PLAIN: not-authorized
2.登陆
3.发送文字消息
4.接收消息
就可以了
https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide
由于并不懂的新xmpp协议,所有先写个Demo来测试一下功能,有一些坑,特此记录一下:
1.连接
Thread connectThread = new Thread(){ @Override public void run() { //配置连接 XMPPTCPConnectionConfiguration mConnectConfig = XMPPTCPConnectionConfiguration.builder() .setServiceName(HOST).setUsernameAndPassword("18101399687@"+HOST,"123456").setDebuggerEnabled(true).setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) .setCompressionEnabled(false).build(); //配置授权信息 SASLMechanism mechanism = new SASLDigestMD5Mechanism(); SASLAuthentication.registerSASLMechanism(mechanism); SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1"); SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5"); // Create a connection to the igniterealtime.org XMPP server. con = new XMPPTCPConnection(mConnectConfig); try { con.connect(); } catch (SmackException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XMPPException e) { e.printStackTrace(); } } }; connectThread.start();
其中setServiceName设置的是服务器地址,UserName 需要使用JID全名,由UserName@Host组成。
其次为了顺利登陆需要配置开启SASL机制,否则登陆时会报错org.jivesoftware.smack.sasl.SASLErrorException: SASLError using PLAIN: not-authorized
2.登陆
try { con.login(); } catch (XMPPException e) { e.printStackTrace(); } catch (SmackException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
3.发送文字消息
Chat chat = ChatManager.getInstanceFor(con).createChat("18101399690@27.17.34.22",this); try { chat.sendMessage("Hello world!"); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); }
4.接收消息
ChatMessageListener
就可以了
相关文章推荐
- [Android Pro] android 杀死进程的方法
- 编译vitamio的ffmpeg
- google第三方登录集成说明(android)
- Android EditText的设置
- Android Camera 使用小结
- android studio 和svn配合上传代码
- Android Toast 自定义背景、图片 随心使用
- Android 各种布局 控件内部属性大全|我是记不住了搞下来看看
- Android中Layer-list的使用 为控件添加边线
- android studio undefined reference to `__android_log_print'
- Android中overridePendingTransition的应用(部分代码)
- 【Android studio】 unable to start the daemon process
- android自定义view之圆形头像的完美例子
- Android性能优化典范
- 1CoordinatorLayout与behavior入门
- android中string.xml中%1$s、%1$d等的用法
- Android Studio 引入Library项目
- Android布局文件中android:textColor属性失效问题
- Android 重启activity
- Anroid开发乱码解决