aSmack 使用

来源:互联网 发布:黑暗之魂3帕奇捏脸数据 编辑:程序博客网 时间:2024/05/29 02:43

Smack
https://github.com/igniterealtime/Smack

aSmack项目
https://github.com/Flowdalic/asmack

从4.1开始smack已经支持Android了,aSmack项目也不再维护了。

aSmack 0.8.x 和4.x的改变还是很多的,对比一下他们的写法就知道了。(服务端 为 Openfire 3.10.0~3.10.2 默认配置)

0.8.x连接服务器的配置

ConnectionConfiguration config = new ConnectionConfiguration(HOST, PORT);SASLAuthentication.supportSASLMechanism("PLAIN", 0);// 设置登录状态为离线,默认为trueconfig .setSendPresence(false);config .setSendPresence(true);// 断网重连,默认为trueconfig .setReconnectionAllowed(true);XMPPConnection  connction = new XMPPConnection(config);connection.connect();

这样就能够正常登录了。

4.x的代码片段

// 认证模式:PLAIN 最简单的,也是危险的SASLAuthentication.supportSASLMechanism("PLAIN", 0);ConnectionConfiguration config = new ConnectionConfiguration(SERVER_HOST, SERVER_PORT);// 设置登录状态为在线config.setSendPresence(true);// 断线重连config.setReconnectionAllowed(true);// 安全连接:根据服务器设置是否启用安全连接// 4.x默认开启,设置为禁用,否则提示证书错误之类的,需要配置证书        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);connection = new XMPPTCPConnection(config);
  1. 0.8x中直接new 一个XmppConnection,而4.xXmppConnection已经是抽象类了,只能通过其子类XmppTCPConnection来实现了。
  2. 在4.x中DNS-java被移除了,需要手动添加。
    而且还需要初始化 Android环境,貌似是用于DNS解析的广播声明SmackAndroid.init(getApplicationContext());

使用过程中可能出现各种错误,google一般都是有答案的。

0 0
原创粉丝点击