在上一篇中我们已经完成了aSmack的配置以及注册了接下来我给大家介绍一下aSmack4.0以后的登录
第一步当然是初始化配置
在上一篇已经讲的很详细了,就不再赘述了
http://blog.csdn.net/hellohhj/article/details/41074803
第二步 登录
应为这个比较简单就直接给代码了
/**
* @param str
* @return Integer
* @throws exception
*/
private Integer login() {
String username = loginConfig.getUsername();
String password = loginConfig.getPassword();
try
{
XMPPConnection connection = XMPPConnectionManager.getInstance()
.getConnection();
connection.connect();
connection.login(username, password); // 登录
connection.sendPacket(new Presence(Presence.Type.available));
PacketFilter filter = new PacketTypeFilter(IQ.class);
PacketCollector collector = XMPPConnectionManager.getInstance().getConnection()
.createPacketCollector(filter);
IQ result = (IQ) collector.nextResult(SmackConfiguration
.getDefaultPacketReplyTimeout());
System.out.println("-----------------result--------------------"+result);
return Constant.LOGIN_SECCESS;
} catch (Exception xee) {
xee.printStackTrace();
return Constant.LOGIN_ERROR;
}
}
注意事项:
2、
connection.sendPacket(new Presence(Presence.Type.available));
这个漏了就你不能看到你的登录状态了,之前我没写这个,还以为出问题了,有没报错,各种纠结啊
好的,登录也完成了,很简单吧。
转载请注明出处: