aSmack4.0之旅(一)配置介绍注册篇
aSmack4.0之旅(一)配置注册篇
一、Smack是什么
二、Smack的优点
一、我们先到官网下载需要的jar包如果需要源码官方也是提供了的
configuration = new ConnectionConfiguration(config.getXmppHost(),config.getXmppPort(), config.getXmppServernanme());
//config.getXmppHost() openfir服务器地址 config.getXmppPort() 端口号(5222)config.getXmppServernanme()名字随意。Android默认为Smack
3、configuration.setSecurityMode(SecurityMode.disabled); 设置为disabled否则认证失败。
/** * 注册 * * @param account * 注册帐号 * @param password * 注册密码 */ public int regist(String account, String password) { try { XMPPConnection connection = XMPPConnectionManager.getInstance() .getConnection(); connection.connect(); Registration reg = new Registration(); reg.setType(IQ.Type.SET); reg.setTo(connection.getServiceName()); Map<String, String> map = new HashMap<String, String>(); map.put("username", account); map.put("password", password); reg.setAttributes(map); PacketFilter filter = new AndFilter(new PacketIDFilter( reg.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector collector = XMPPConnectionManager.getInstance() .getConnection().createPacketCollector(filter); connection.sendPacket(reg); IQ result = (IQ) collector.nextResult(SmackConfiguration .getDefaultPacketReplyTimeout()); System.out.println("-----------------result--------------------" + result); collector.cancel();// 停止请求results(是否成功的结果) } catch (Exception e) { e.printStackTrace(); return Constant.LOGIN_ERROR; } return Constant.LOGIN_SECCESS; }
注意事项:
1、需要注意点是注册是需要异步操作否则连接异常
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。