Android基于XMPP Smack Openfire下学习开发IM(六)总结
不管学习什么都应该总结这里我把关于Xmpp的一些方法整理到一个工具类中了
我就分享给大家
XmppConnection.java
package com.techrare.utils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.GroupChatInvitation;
import org.jivesoftware.smackx.OfflineMessageManager;
import org.jivesoftware.smackx.PrivateDataManager;
import org.jivesoftware.smackx.ReportedData;
import org.jivesoftware.smackx.ReportedData.Row;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.bytestreams.socks5.provider.BytestreamsProvider;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import org.jivesoftware.smackx.muc.DiscussionHistory;
import org.jivesoftware.smackx.muc.HostedRoom;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.packet.ChatStateExtension;
import org.jivesoftware.smackx.packet.LastActivity;
import org.jivesoftware.smackx.packet.OfflineMessageInfo;
import org.jivesoftware.smackx.packet.OfflineMessageRequest;
import org.jivesoftware.smackx.packet.SharedGroupsInfo;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.smackx.provider.AdHocCommandDataProvider;
import org.jivesoftware.smackx.provider.DataFormProvider;
import org.jivesoftware.smackx.provider.DelayInformationProvider;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
import org.jivesoftware.smackx.provider.MUCAdminProvider;
import org.jivesoftware.smackx.provider.MUCOwnerProvider;
import org.jivesoftware.smackx.provider.MUCUserProvider;
import org.jivesoftware.smackx.provider.MessageEventProvider;
import org.jivesoftware.smackx.provider.MultipleAddressesProvider;
import org.jivesoftware.smackx.provider.RosterExchangeProvider;
import org.jivesoftware.smackx.provider.StreamInitiationProvider;
import org.jivesoftware.smackx.provider.VCardProvider;
import org.jivesoftware.smackx.provider.XHTMLExtensionProvider;
import org.jivesoftware.smackx.search.UserSearch;
import org.jivesoftware.smackx.search.UserSearchManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.techrare.listener.TaxiConnectionListener;
/**
* XmppConnection 工具类
* @author 肖赛SoAi
*
*/
public class XmppConnection {
private int SERVER_PORT = 5222;
private String SERVER_HOST = "127.0.0.1";
private XMPPConnection connection = null;
private String SERVER_NAME = "ubuntuserver4java";
private static XmppConnection xmppConnection = new XmppConnection();
private TaxiConnectionListener connectionListener;
/**
* 单例模式
*
* @return
*/
synchronized public static XmppConnection getInstance() {
return xmppConnection;
}
/**
* 创建连接
*/
public XMPPConnection getConnection() {
if (connection == null) {
openConnection();
}
return connection;
}
/**
* 打开连接
*/
public boolean openConnection() {
try {
if (null == connection || !connection.isAuthenticated()) {
XMPPConnection.DEBUG_ENABLED = true;// 开启DEBUG模式
// 配置连接
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;
}
/**
* 关闭连接
*/
public void closeConnection() {
if(connection!=null){
//移除連接監聽
//connection.removeConnectionListener(connectionListener);
if(connection.isConnected())
connection.disconnect();
connection = null;
}
Log.i("XmppConnection", "關閉連接");
}
/**
* 登录
*
* @param account
* 登录帐号
* @param password
* 登录密码
* @return
*/
public boolean login(String account, String password) {
try {
if (getConnection() == null)
return false;
getConnection().login(account, password);
// 更改在綫狀態
Presence presence = new Presence(Presence.Type.available);
getConnection().sendPacket(presence);
// 添加連接監聽
connectionListener = new TaxiConnectionListener();
getConnection().addConnectionListener(connectionListener);
return true;
} catch (XMPPException xe) {
xe.printStackTrace();
}
return false;
}
/**
* 注册
*
* @param account
* 注册帐号
* @param password
* 注册密码
* @return 1、注册成功 0、服务器没有返回结果2、这个账号已经存在3、注册失败
*/
public String regist(String account, String password) {
if (getConnection() == null)
return "0";
Registration reg = new Registration();
reg.setType(IQ.Type.SET);
reg.setTo(getConnection().getServiceName());
// 注意这里createAccount注册时,参数是UserName,不是jid,是"@"前面的部补充:移动开发 , Android ,