您的位置:首页 > 移动开发 > Android开发

android XMPP实现IM聊天一(登录和注册)

2014-01-11 17:14 429 查看
其实对于android基于openfire+asmack+spark的有关于IM聊天的源码、网上已经可以找出一大堆这些资料了,但是每个人的项目经历不同,对XMPP的体验也不同,多少都会有些感悟的,现在我就把我项目中用到XMPP的流程贴出来,也算是自己的一个项目经历吧。

对于openfire服务器的搭建与安装这里我就不介绍了,自己也可以去网上下载一个服务器,安装方法也很简单,直接下一步就行,数据库用自带的,如果服务器安装错误的话只需要将你openfire的路径下一个文件改掉,该服务器就会自动从新配置,比如我的openfire路径:C:\Program Files\Openfire\conf\,找到该配置文件openfire.xml,把 <setup>true</setup>,去掉就行了,当再次重启openfire服务器的时候,就会重新配置。

对于asmack,其实是smack针对android兴起所制定的一个jar包,更好的配合android开发,这些我也懂的不多,都是从网上了解的....

而spark则是完全是用openfire+asmack实现的一个客户端软件,这里只是为了方便测试使用。

好了话也不多说了,直接切入主题吧,刚刚开始做的时候还是以登陆注册为主,这是最基本的功能。首先,先连接服务器

1、定义一些变量

private int server_port=5222;//端口
public XMPPConnection connection=null;//连接
private String server_name = "ubuntuserver4java";//服务器名称
private CccallConnectionListener connectionlistener;//断开连接监听
private String server_host = "192.168.1.140";//服务器ip

public static XmppUtil xmpputil=new XmppUtil();


2、打开连接

/   /打開連接
public boolean openConnection(){
try {
if (null == connection || !connection.isAuthenticated()) {
// 配置连接
ConnectionConfiguration config = new ConnectionConfiguration(
server_host, server_port, server_name);
config.setReconnectionAllowed(true);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setSendPresence(true); // 状态设为离线,目的为了取离线消息
config.setSASLAuthenticationEnabled(false); // 是否启用安全验证
config.setTruststorePath("/system/etc/security/cacerts.bks");
config.setTruststorePassword("changeit");
config.setTruststoreType("bks");
connection = new XMPPConnection(config);
connection.connect();// 连接到服务器
// 配置各种Provider,如果不配置,则会无法解析数据
configureConnection(ProviderManager.getInstance());
return true;
}
} catch (XMPPException xe) {
xe.printStackTrace();
connection = null;
}
return false;
}


3、定义的方法

//單例模式
synchronized public static XmppUtil getinstance(){
return xmpputil;
}

//創建連接
public  XMPPConnection getconnection(){
if(connection==null){
openConnection();
}
return connection;
}


4、关闭连接

//關閉連接
public void closeConnection(){
if(connection!=null){
if(connection.isConnected())
connection.disconnect();
//移除监听
connection.removeConnectionListener(connectionlistener);
connection=null;
}
}


5、登陆(直接传入用户名和密码就可以了,注意这里的用户名和密码是openfire上面的)

//登錄方法
public boolean login(String name,String pwd){
try {
if(getconnection()==null)
return false;

//服务器断开重连的监听方法(移除)
if(connectionlistener!=null)
connection.removeConnectionListener(connectionlistener);
getconnection().login(name, pwd);//登录方法

//更改在線狀態
Presence pre=new Presence(Presence.Type.available);
getconnection().sendPacket(pre);

//服务器断开重连的监听方法(添加)
connectionlistener=new CccallConnectionListener();
getconnection().addConnectionListener(connectionlistener);

return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


6、注册(添加用户名和密码就可以了)

//实现注册(1、注册成功 0、服务器没有返回结果2、这个账号已经存在3、注册失败 )
public String register(String loginName,String pwd){
System.out.println("===================开始注册到openfire中去了=======================");
if(connection==null)
XmppUtil.getinstance().getconnection();

Registration re=new Registration();
re.setType(IQ.Type.SET);
re.setTo(connection.getServiceName());

re.setPassword(pwd);
re.setUsername(loginName);
re.addAttribute("android", "geolo_createUser_android");//不能为空

System.out.println("注册到openfire中的姓名是====="+loginName+"密码是====="+pwd);

PacketFilter filter=new AndFilter(new PacketIDFilter(re.getPacketID()),new PacketTypeFilter(IQ.class));
PacketCollector collector=connection.createPacketCollector(filter);

connection.sendPacket(re);
IQ result=(IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());

collector.cancel();//停止请求result(是否成功的结果)
if(result==null)
return "0";
else if(result.getType()==IQ.Type.RESULT){
return "1";
}else{
if(result.getError().toString().equalsIgnoreCase("conflict(409)")){
return "2";
}else{
return "3";
}
}
}


View Code

方法就这些了,下面讲讲如何使用上面的方法了

登陆:我的数据都是从Mysql上读取过来的,所以先在后台判断了一次登陆,然后再登陆上openfire中

boolean loginFlag=XmppUtil.getinstance().login(user.getuId(), upwd);


注册:根据返回来的res判断状态

String res=XmppUtil.getinstance().register(uid, repwd);


登陆、注册就先到这里吧,下一篇....期待吧!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐