如下:
[img]
[/img]
第二、创建一个用户,我 这里注册的用户名:test 密码:test.
[img]
[/img]
第三,创建android客户端,代码如下:( 我的电脑的ip是:192.168.0.124,测试的时候改成自己的)
package com.example.openfiretest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText accountEditText; private EditText passwordEditText; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); accountEditText = (EditText) findViewById(R.id.username); passwordEditText = (EditText) findViewById(R.id.password); findViewById(R.id.login).setOnClickListener(new OnClickListener() { public void onClick(View v) { String account = accountEditText.getText().toString(); String password = passwordEditText.getText().toString(); if (account.equals("") || password.equals("")) { Toast.makeText(MainActivity.this, "账号或密码不能为空!", Toast.LENGTH_SHORT).show(); } else { ClientConServer ccs = new ClientConServer(MainActivity.this); boolean b = ccs.login(account, password); // 如果登录成功 if (b) { Toast.makeText(MainActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show(); // startActivity(new // Intent(LoginActivity.this,MainActivity.class)); // MainActivity.account=account; } else { Toast.makeText(MainActivity.this, "登陆失败!", Toast.LENGTH_SHORT).show(); } } } }); } }
package com.example.openfiretest; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import android.content.Context; public class ClientConServer { private static int PORT=5222; private Context context; public ClientConServer(Context context){ this.context=context; } public boolean login(String a,String p){ ConnectionConfiguration config = new ConnectionConfiguration("192.168.0.124", PORT); /** 是否启用安全验证 */ config.setSASLAuthenticationEnabled(false); /** 是否启用调试 */ //config.setDebuggerEnabled(true); /** 创建connection链接 */ XMPPConnection connection = new XMPPConnection(config); try { /** 建立连接 */ connection.connect(); /** 登录*/ connection.login(a, p); /** 设置状态 */ Presence presence = new Presence (Presence.Type.available); presence.setStatus("Q我吧"); connection.sendPacket(presence); /** 开启读写线程,并加入到管理类中*/ //ClientSendThread cst=new ClientSendThread(connection); //cst.start(); //ManageClientThread.addClientSendThread(a, cst); return true; } catch (XMPPException e) { e.printStackTrace(); } return false; } }
AndroidManifest.xml中添加:
第四,启动android项目,用test用户登录,密码也是test,提示登录成功。
[img]
[/img]
第五,在openfire中的会话中多了一个用户:test
[img]
[/img]
android的客户端使用XMPP协议进行通信需要的jar包: