万事开头难!
Android+smack开发环境的配置可参考:http://www.yunliaoim.com/im/4556.html
Android使用smack登录openfire注意事项(针对萌新,老手忽略)
1.在AndroidManifest.xml添加android.permission.INTERNET权限……
<uses-permission android:name="android.permission.INTERNET" />
2.在UI主线程中执行连接openfire的操作……在Android中不可以在UI主线程中连接网络……
new Thread() {
public void run() {
//连接操作
}
}.start
连接openfire,参考:
https://download.igniterealtime.org/smack/docs/latest/documentation/gettingstarted.html
这里要注意:如果服务器没开启安全模式,客户端这里也要关闭安全模式
代码如下:
XMPPTCPConnectionConfiguration config = null;
try {
config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("test2", "123456")
.setXmppDomain("yuliaoim.com")
.setHost("188.188.88.88")
.setPort(5222)
.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled)
.build();
} catch (XmppStringprepException e) {
e.printStackTrace();
}
AbstractXMPPConnection conn = new XMPPTCPConnection(config);
try {
conn.connect().login();
#handler.sendEmptyMessage(1);
} catch (XMPPException e) {
e.printStackTrace();
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
执行上述操作后,如果没有抛出异常,应该就是连接成功了。可以openfire后台查看

版权声明:部分文章、图片等内容为用户发布或互联网整理而来,仅供学习参考。如有侵犯您的版权,请联系我们,将立刻删除。