此文是自己在本地进行的测试。由于openfire等都是安装在电脑上,而运行是在真机上运行的。所以在测试的时候必须保证电脑和手机是在同一个网络环境下,不然会无法连接到openfire服务器。
至于测试环境的下载配置可以参考作者:https://blog.csdn.net/u013086747/article/details/75216653 该文章里面很详细的介绍了如何下载配置测试环境。
我是基于smack 4.1.9开发的。
dependencies {
compile 'org.igniterealtime.smack:smack-android:4.1.9'
compile 'org.igniterealtime.smack:smack-tcp:4.1.9'
compile 'org.igniterealtime.smack:smack-im:4.1.9'
compile 'org.igniterealtime.smack:smack-extensions:4.1.9'
}
整体代码较多,可以分开来看,每一个方法名的意思就代表了实现的功能。具体代码实现如下:
package com.xie.com.imoocmusic.config;
import android.util.Log;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
public class MyXmppConfig extends XMPPTCPConnection {
private static MyXmppConfig connect;
public MyXmppConfig(XMPPTCPConnectionConfiguration config) {
super(config);
}
/**
* 初始化连接数据
* @return
*/
public static synchronized MyXmppConfig getInstance(){
//初始化配置
if (connect ==null){
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
//设置连接超时的最大时间
builder.setConnectTimeout(10000);
//设置登录openfire的用户名和密码
builder.setUsernameAndPassword("88888888","123");
//设置安全模式
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
//设置客服端类型
builder.setResource("Android");
//设置服务器名称
builder.setServiceName("xie-pc");
//设置主机地址
builder.setHost("192.168.0.130");
//设置端口号
builder.setPort(5222);
//设置是否查看日志
builder.setDebuggerEnabled(true);
connect = new MyXmppConfig(builder.build());
}
Log.e("MyXmppConfig","connect: "+ connect);
connectionListener();
return connect;
}
/**
* 连接openfire服务器
*/
public static void connectOpenfire(){
try {
if (connect != null){
//如果没有连接openfire服务器,则连接;若已连接openfire服务器则跳过。
if(!connect.isConnected()){
connect.connect();
}
}else {
MyXmppConfig.getInstance();
connect.connect();
}
}catch (Exception e){
Log.e("connectOpenfire","Exception: "+e.toString());
}
}
/**
*
* @param phone 登录用户名
* @param password 登录密码
*/
public static void loginOpenfire(String phone,String password){
try {
if(connect.isConnected()){
//第一个参数是 用户名(即你在openfire服务器用户管理页面创建的用户名)
//第二个参数是 用户密码(同上)
//connection.login("用户名", "密码")
connect.login( phone, password);
}else {
connectOpenfire();
connect.login(phone, password);
}
} catch (Exception e) {
Log.e("Login","Exception: "+e.toString());
}
}
public MyXmppConfig(CharSequence jid, String password) {
super(jid, password);
}
public MyXmppConfig(CharSequence username, String password, String serviceName) {
super(username, password, serviceName);
}
public static void connectionListener(){
if (connect!=null){
connect.addConnectionListener(new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
Log.e("connected", "connected");
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
Log.e("authenticated", "authenticated");
}
@Override
public void connectionClosed() {
Log.e("connectionClosed", "connectionClosed");
}
@Override
public void connectionClosedOnError(Exception e) {
Log.e("connectionClosedOnError", "connectionClosedOnError");
}
@Override
public void reconnectionSuccessful() {
Log.e("reconnectionSuccessful", "reconnectionSuccessful");
}
@Override
public void reconnectingIn(int seconds) {
Log.e("reconnectingIn", "reconnectingIn");
}
@Override
public void reconnectionFailed(Exception e) {
Log.e("reconnectionFailed", "reconnectionFailed");
}
});
}
}
}
调用loginOpenfire就能看到是否成功登录。很多时候会报无法连接服务器等等,注意检查网络环境,电脑和手机是否处于同一网络环境下。
new Thread(new Runnable(){
@Override
public void run() {
//服务器登录
MyXmppConfig.getInstance().loginOpenfire(phone,password);
}
}).start();