smack笔记
注册思路
new注册类 set他的类型(IQ.TYPE.SET常量) to哪里 账号密码 注册模式(addAttribute)
新建一个 “与”过滤器 条件是 ID过滤器和type过滤器 type为IQ.Class
连接对象创建刚刚新建的过滤器
连接对象发送注册Packet
过滤器的下一个结果(.netResult(连接超时时间))强转为IQ类保存
根据返回的结果类型属性判断是否注册成功。
public void regist() {
Registration registration = new Registration();
registration.setType(IQ.Type.SET);
registration.setTo(connection.getServiceName());// ----------------------
registration.setUsername("");
registration.setPassword("");
registration.addAttribute("", "");// ------------
PacketFilter filter = new AndFilter(new PacketIDFilter(
registration.getPacketID()), new PacketTypeFilter(IQ.class));
PacketCollector collector = connection.createPacketCollector(filter);
connection.sendPacket(registration);
IQ result = (IQ) collector.nextResult(SmackConfiguration
.getPacketReplyTimeout());
collector.cancel();
if (result == null) {
Log.e("regist", "没有响应");
} else if (result.getType() == IQ.Type.ERROR) {
if ("conflict(409)".equalsIgnoreCase(result.getError().toString())) {
Log.e("regist", "这个账号已经存在");
} else {
Log.e("regist", "注册失败 " + result.getError().toString());
}
} else if (result.getType() == IQ.Type.RESULT) {
Log.e("regist", "注册成功");
}
}
登录思路
检查连接是否在,isAuthenticated(是否已经登录?)
连接类尝试登录(加账号密码)
public boolean login(){ if(!connection.isConnected()){ return false; } if(connection.isAuthenticated()){ return true; }else { try { connection.login("count", "password"); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection.isAuthenticated(); } }
浙公网安备 33010602011771号