aSmack 使用

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);
// 设置登录状态为离线,默认为true
config .setSendPresence(false);
config .setSendPresence(true);
// 断网重连,默认为true
config .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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值