**XMPP是一种基于标准通用标记语言的子集XML的协议,它继承了在XML环境中灵活的发展性。因此,基于XMPP的应用具有超强的可扩展性。经过扩展以后的XMPP可以通过发送扩展的信息来处理用户的需求,以及在XMPP的顶端建立如内容发布系统和基于地址的服务等应用程序。而且,XMPP包含了针对服务器端的软件协议,使之能与另一个进行通话,这使得开发者更容易建立客户应用程序或给一个配好系统添加功能。
由于考虑到经济以及稳重性而言,我们决定在用xmpp来实现我们的通讯功能,在网上查阅不难发现我们还要有一个openfire服务器配合使用,那么问题就来了,如何来搭建呢,其实只要双击openfire不断点击下一步就可以了,既然我们的openfire已经完成的差不多了,那么我们还需要下载smack的包,把smack下载下来,打开里面的lib文件,将里面的把里面的asmack拷贝到我们的eclipse的工程目录下,既然万事具备,那么我们就来编程这一步吧,首先我们的实现登陆以及注册对吧,那么问题就来了,如何实现呢,其实smack的文档有已经有详细的记录了,这一部分比较简单,我干脆就亮代码吧,注册功能如下:**
工具类如下:
public class LoginUtil {
private static XMPPConnection con = null;
private static void openConnection() {
try {
ConnectionConfiguration connConfig = new ConnectionConfiguration("10.0.3.2" , 5222);//连接设置
connConfig.setReconnectionAllowed( true);//是否重新连接
connConfig.setCompressionEnabled( false);//是否压缩字节流
connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode. disabled);//是否设置安全模式//参数为否
connConfig.setSASLAuthenticationEnabled( false);//对字节进行加密,设置为false表示不需要
con = new XMPPConnection(connConfig);//获取配置参数,创建xmppconnection
con.connect();//获取连接
} catch (XMPPException xe) {
xe.printStackTrace();
}
}
public final static int register(String username,String password){ //注册
if(con ==null){
getConnection();//判断连接是否为空
}
Registration reg= new Registration();//设置注册对象
reg.setType(IQ.Type. SET);//通过设置IQ来进行注册,路由,权限其他方面的操作
reg.setTo( con.getServiceName());//获取服务器的名字
reg.setUsername(username); //设置用户名
reg.setPassword(password); //设置密码
reg.addAttribute( "android", "geolo_createUser_android" );//设置属性,要不然会出错
PacketFilter filter = new AndFilter(new PacketIDFilter(
reg.getPacketID()), new PacketTypeFilter(IQ.class)); //过滤信息,只有符合条件的才允许通过
PacketCollector collector = con
.createPacketCollector(filter);
con.sendPacket(reg); //将信息发送到服务器哪一端去
IQ result = (IQ) collector.nextResult(SmackConfiguration
. getPacketReplyTimeout());
// Stop queuing results
collector.cancel(); // 停止请求results(是否成功的结果)
if (result == null) {
Log. e("RegistActivity", "No response from server.");
return 0;
} else if (result.getType() == IQ.Type.RESULT) {
return 1;
} else { // if (result.getType() == IQ.Type.ERROR)
if (result.getError().toString().equalsIgnoreCase("conflict(409)" )) {
Log. e("RegistActivity", "IQ.Type.ERROR: "
+ result.getError().toString());
return 2;
} else {
Log. e("RegistActivity", "IQ.Type.ERROR: "
+ result.getError().toString());
return 3;
}
}
}
public static boolean login(String username,String password){ //登陆功能
try {
if(con ==null)
getConnection();
con.login(username, password);
return true ;
} catch (XMPPException e) {
// TODO Auto-generated catch block
closeConnection();
return false ;
}
}
public static boolean changePassword(XMPPConnection connection,String pwd) //修改密码
{
try {
connection.getAccountManager().changePassword(pwd);
return true ;
} catch (Exception e) {
return false ;
}
}
public final static XMPPConnection getConnection() { //获取连接
if(con ==null){
openConnection();
}
return con ;
}
public static void closeConnection() {//关闭连接
con.disconnect();
con = null ;
}
}
下面是我在MainActivity中的代码,如下,就可以实现注册功能:
case R.id.btn_qtlogin :{
Toast. makeText(this, “ss”, Toast.LENGTH_SHORT ).show();
final String USERID = this.edit_username .getText().toString();
final String PWD = this.edit_password.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
LoginUtil. register(USERID, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
break;
}
以下是登陆的功能,如下:
case R.id.btn_login :{
final String USERID = this.edit_username .getText().toString();
final String PWD = this.edit_password.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
LoginUtil. login(USERID, PWD);
Presence presence = new Presence(Presence.Type.available );
LoginUtil. getConnection().sendPacket(presence);
Intent intent= new Intent(MainActivity.this , ChatRoom.class);
intent.putExtra( "USERID", USERID);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
break;
是不是觉得相当的简单啊,好吧,老实说我弄了好久,一直都说我连不上服务器,知道后来我把我的服务器ip地址换了,由于用的是Genymotion的安卓模拟器,所以问题不知咋的其好像用不了,所以我就用了它的内置模拟器ip:10.0.3.2,结果竟然神奇般的好了,嘎嘎,太爽了,有不有这种感觉,算了,补上我所有的代码,免得有些初学者看不懂,这是login.xml
的代码:
<?xml version= "1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:background= "#ffffff"
android:orientation= "vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="12dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="48dp" >
<EditText
android:id="@+id/et_usertel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/edittext_login"
android:hint="你的手机号码"
android:paddingLeft="90dp"
android:singleLine="true"
android:maxLength="11"
android:textColorHint="#DDDDDD"
android:textSize="16sp" />
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/et_usertel"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:text="+86"
android:textColor="#353535"
android:textSize="16sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="48dp" >
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/edittext_login"
android:hint="填写密码"
android:paddingLeft="90dp"
android:password="true"
android:singleLine="true"
android:textColorHint="#DDDDDD"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_password"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/et_password"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="密码"
android:textColor="#353535"
android:textSize="16sp" />
</RelativeLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="40dp"
android:background="@drawable/btn_enable_green"
android:paddingBottom="7dp"
android:paddingTop="7dp"
android:text="登录"
android:textColor="@android:color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_wenti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:text="登录遇到问题?"
android:textColor="#576B95"
android:textSize="14sp" />
</LinearLayout >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_margin="33dp"
android:padding="8dp"
android:background="#F0F0F0"
android:id="@+id/btn_qtlogin"
android:layout_width="80dp"
android:layout_height="33dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="注册"
android:textColor="#353535"
android:textSize="15sp" />
</RelativeLayout >
</LinearLayout>
以下是我的MainActivity的代码:
public class MainActivity extends Activity implements OnClickListener {
private Button login ;
private Button register ;
private EditText edit_username ;
private EditText edit_password ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. login);
initView();
initListenner();
}
public void initView(){
login=(Button) findViewById(R.id. btn_login);
register=(Button) findViewById(R.id. btn_qtlogin);
edit_password=(EditText) findViewById(R.id.et_password);
edit_username=(EditText) findViewById(R.id.et_usertel);
}
public void initListenner(){
login.setOnClickListener( this);
register.setOnClickListener( this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login :{
final String USERID = this.edit_username .getText().toString();
final String PWD = this.edit_password.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
LoginUtil. login(USERID, PWD);
Presence presence = new Presence(Presence.Type.available );
LoginUtil. getConnection().sendPacket(presence);
Intent intent= new Intent(MainActivity.this , ChatRoom.class);
intent.putExtra( "USERID", USERID);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
break;
case R.id.btn_qtlogin :{
Toast. makeText(this, "ss", Toast.LENGTH_SHORT ).show();
final String USERID = this.edit_username .getText().toString();
final String PWD = this.edit_password.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
LoginUtil. register(USERID, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
break;
}
}
}
好了,希望对您的学习有帮助