不管学习什么都应该总结
这里我把关于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;
-
-
-
-
-
-
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;
-
-
-
-
-
-
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;
-
-
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();
-
-
configureConnection(ProviderManager.getInstance());
-
return true;
-
}
-
} catch (XMPPException xe) {
-
xe.printStackTrace();
-
connection = null;
-
}
-
return false;
-
}
-
-
-
-
-
public void closeConnection() {
-
if(connection!=null){
-
-
-
if(connection.isConnected())
-
connection.disconnect();
-
connection = null;
-
}
-
Log.i("XmppConnection", "關閉連接");
-
}
-
-
-
-
-
-
-
-
-
-
-
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;
-
}
-
-
-
-
-
-
-
-
-
-
-
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());
-
-
reg.setUsername(account);
-
reg.setPassword(password);
-
-
reg.addAttribute("android", "geolo_createUser_android");
-
PacketFilter filter = new AndFilter(new PacketIDFilter(
-
reg.getPacketID()), new PacketTypeFilter(IQ.class));
-
PacketCollector collector = getConnection().createPacketCollector(
-
filter);
-
getConnection().sendPacket(reg);
-
IQ result = (IQ) collector.nextResult(SmackConfiguration
-
.getPacketReplyTimeout());
-
-
collector.cancel();
-
if (result == null) {
-
Log.e("regist", "No response from server.");
-
return "0";
-
} else if (result.getType() == IQ.Type.RESULT) {
-
Log.v("regist", "regist success.");
-
return "1";
-
} else {
-
if (result.getError().toString().equalsIgnoreCase("conflict(409)")) {
-
Log.e("regist", "IQ.Type.ERROR: "
-
+ result.getError().toString());
-
return "2";
-
} else {
-
Log.e("regist", "IQ.Type.ERROR: "
-
+ result.getError().toString());
-
return "3";
-
}
-
}
-
}
-
-
-
-
-
public void setPresence(int code) {
-
XMPPConnection con = getConnection();
-
if (con == null)
-
return;
-
Presence presence;
-
switch (code) {
-
case 0:
-
presence = new Presence(Presence.Type.available);
-
con.sendPacket(presence);
-
Log.v("state", "设置在线");
-
break;
-
case 1:
-
presence = new Presence(Presence.Type.available);
-
presence.setMode(Presence.Mode.chat);
-
con.sendPacket(presence);
-
Log.v("state", "设置Q我吧");
-
break;
-
case 2:
-
presence = new Presence(Presence.Type.available);
-
presence.setMode(Presence.Mode.dnd);
-
con.sendPacket(presence);
-
Log.v("state", "设置忙碌");
-
break;
-
case 3:
-
presence = new Presence(Presence.Type.available);
-
presence.setMode(Presence.Mode.away);
-
con.sendPacket(presence);
-
Log.v("state", "设置离开");
-
break;
-
case 4:
-
Roster roster = con.getRoster();
-
Collection entries = roster.getEntries();
-
for (RosterEntry entry : entries) {
-
presence = new Presence(Presence.Type.unavailable);
-
presence.setPacketID(Packet.ID_NOT_AVAILABLE);
-
presence.setFrom(con.getUser());
-
presence.setTo(entry.getUser());
-
con.sendPacket(presence);
-
Log.v("state", presence.toXML());
-
}
-
-
presence = new Presence(Presence.Type.unavailable);
-
presence.setPacketID(Packet.ID_NOT_AVAILABLE);
-
presence.setFrom(con.getUser());
-
presence.setTo(StringUtils.parseBareAddress(con.getUser()));
-
con.sendPacket(presence);
-
Log.v("state", "设置隐身");
-
break;
-
case 5:
-
presence = new Presence(Presence.Type.unavailable);
-
con.sendPacket(presence);
-
Log.v("state", "设置离线");
-
break;
-
default:
-
break;
-
}
-
}
-
-
-
-
-
-
-
public List getGroups() {
-
if (getConnection() == null)
-
return null;
-
List grouplist = new ArrayList();
-
Collection rosterGroup = getConnection().getRoster()
-
.getGroups();
-
Iterator i = rosterGroup.iterator();
-
while (i.hasNext()) {
-
grouplist.add(i.next());
-
}
-
return grouplist;
-
}
-
-
-
-
-
-
-
-
-
-
public List getEntriesByGroup(String groupName) {
-
if (getConnection() == null)
-
return null;
-
List Entrieslist = new ArrayList();
-
RosterGroup rosterGroup = getConnection().getRoster().getGroup(
-
groupName);
-
Collection rosterEntry = rosterGroup.getEntries();
-
Iterator i = rosterEntry.iterator();
-
while (i.hasNext()) {
-
Entrieslist.add(i.next());
-
}
-
return Entrieslist;
-
}
-
-
-
-
-
-
-
public List getAllEntries() {
-
if (getConnection() == null)
-
return null;
-
List Entrieslist = new ArrayList();
-
Collection rosterEntry = getConnection().getRoster()
-
.getEntries();
-
Iterator i = rosterEntry.iterator();
-
while (i.hasNext()) {
-
Entrieslist.add(i.next());
-
}
-
return Entrieslist;
-
}
-
-
-
-
-
-
-
-
-
-
public VCard getUserVCard(String user) {
-
if (getConnection() == null)
-
return null;
-
VCard vcard = new VCard();
-
try {
-
vcard.load(getConnection(), user);
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
-
return vcard;
-
}
-
-
-
-
-
-
-
-
-
public Drawable getUserImage(String user) {
-
if (getConnection() == null)
-
return null;
-
ByteArrayInputStream bais = null;
-
try {
-
VCard vcard = new VCard();
-
-
ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
-
new org.jivesoftware.smackx.provider.VCardProvider());
-
if (user == "" || user == null || user.trim().length() <= 0) {
-
return null;
-
}
-
vcard.load(getConnection(), user + "@"
-
+ getConnection().getServiceName());
-
-
if (vcard == null || vcard.getAvatar() == null)
-
return null;
-
bais = new ByteArrayInputStream(vcard.getAvatar());
-
} catch (Exception e) {
-
e.printStackTrace();
-
return null;
-
}
-
return FormatTools.getInstance().InputStream2Drawable(bais);
-
}
-
-
-
-
-
-
-
-
public boolean addGroup(String groupName) {
-
if (getConnection() == null)
-
return false;
-
try {
-
getConnection().getRoster().createGroup(groupName);
-
Log.v("addGroup", groupName + "創建成功");
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
-
-
-
-
-
-
-
-
public boolean removeGroup(String groupName) {
-
return true;
-
}
-
-
-
-
-
-
-
-
-
public boolean addUser(String userName, String name) {
-
if (getConnection() == null)
-
return false;
-
try {
-
getConnection().getRoster().createEntry(userName, name, null);
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
-
-
-
-
-
-
-
-
-
-
public boolean addUser(String userName, String name, String groupName) {
-
if (getConnection() == null)
-
return false;
-
try {
-
Presence subscription = new Presence(Presence.Type.subscribed);
-
subscription.setTo(userName);
-
userName += "@" + getConnection().getServiceName();
-
getConnection().sendPacket(subscription);
-
getConnection().getRoster().createEntry(userName, name,
-
new String[] { groupName });
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
-
-
-
-
-
-
-
-
public boolean removeUser(String userName) {
-
if (getConnection() == null)
-
return false;
-
try {
-
RosterEntry entry = null;
-
if (userName.contains("@"))
-
entry = getConnection().getRoster().getEntry(userName);
-
else
-
entry = getConnection().getRoster().getEntry(
-
userName + "@" + getConnection().getServiceName());
-
if (entry == null)
-
entry = getConnection().getRoster().getEntry(userName);
-
getConnection().getRoster().removeEntry(entry);
-
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
-
-
-
-
-
-
-
-
-
public List> searchUsers(String userName) {
-
if (getConnection() == null)
-
return null;
-
HashMap user = null;
-
List> results = new ArrayList>();
-
try {
-
new ServiceDiscoveryManager(getConnection());
-
-
UserSearchManager usm = new UserSearchManager(getConnection());
-
-
Form searchForm = usm.getSearchForm(getConnection()
-
.getServiceName());
-
Form answerForm = searchForm.createAnswerForm();
-
answerForm.setAnswer("userAccount", true);
-
answerForm.setAnswer("userPhote", userName);
-
ReportedData data = usm.getSearchResults(answerForm, "search"
-
+ getConnection().getServiceName());
-
-
Iterator
it = data.getRows();
-
Row row = null;
-
while (it.hasNext()) {
-
user = new HashMap();
-
row = it.next();
-
user.put("userAccount", row.getValues("userAccount").next()
-
.toString());
-
user.put("userPhote", row.getValues("userPhote").next()
-
.toString());
-
results.add(user);
-
-
}
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
-
return results;
-
}
-
-
-
-
-
-
-
-
public void changeStateMessage(String status) {
-
if (getConnection() == null)
-
return;
-
Presence presence = new Presence(Presence.Type.available);
-
presence.setStatus(status);
-
getConnection().sendPacket(presence);
-
}
-
-
-
-
-
-
-
public boolean changeImage(File file) {
-
if (getConnection() == null)
-
return false;
-
try {
-
VCard vcard = new VCard();
-
vcard.load(getConnection());
-
-
byte[] bytes;
-
-
bytes = getFileBytes(file);
-
String encodedImage = StringUtils.encodeBase64(bytes);
-
vcard.setAvatar(bytes, encodedImage);
-
vcard.setEncodedImage(encodedImage);
-
vcard.setField("PHOTO", "image/jpg"
-
+ encodedImage + "", true);
-
-
ByteArrayInputStream bais = new ByteArrayInputStream(
-
vcard.getAvatar());
-
FormatTools.getInstance().InputStream2Bitmap(bais);
-
-
vcard.save(getConnection());
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
-
-
-
-
-
-
-
-
-
private byte[] getFileBytes(File file) throws IOException {
-
BufferedInputStream bis = null;
-
try {
-
bis = new BufferedInputStream(new FileInputStream(file));
-
int bytes = (int) file.length();
-
byte[] buffer = new byte[bytes];
-
int readBytes = bis.read(buffer);
-
if (readBytes != buffer.length) {
-
throw new IOException("Entire file not read");
-
}
-
return buffer;
-
} finally {
-
if (bis != null) {
-
bis.close();
-
}
-
}
-
}
-
-
-
-
-
-
-
public boolean deleteAccount() {
-
if (getConnection() == null)
-
return false;
-
try {
-
getConnection().getAccountManager().deleteAccount();
-
return true;
-
} catch (XMPPException e) {
-
return false;
-
}
-
}
-
-
-
-
-
-
-
public boolean changePassword(String pwd) {
-
if (getConnection() == null)
-
return false;
-
try {
-
getConnection().getAccountManager().changePassword(pwd);
-
return true;
-
} catch (XMPPException e) {
-
return false;
-
}
-
}
-
-
-
-
-
public List getHostRooms() {
-
if (getConnection() == null)
-
return null;
-
Collection hostrooms = null;
-
List roominfos = new ArrayList();
-
try {
-
new ServiceDiscoveryManager(getConnection());
-
hostrooms = MultiUserChat.getHostedRooms(getConnection(),
-
getConnection().getServiceName());
-
for (HostedRoom entry : hostrooms) {
-
roominfos.add(entry);
-
Log.i("room",
-
"名字:" + entry.getName() + " - ID:" + entry.getJid());
-
}
-
Log.i("room", "服务会议数量:" + roominfos.size());
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
-
return roominfos;
-
}
-
-
-
-
-
-
-
-
public MultiUserChat createRoom(String user, String roomName,
-
String password) {
-
if (getConnection() == null)
-
return null;
-
-
MultiUserChat muc = null;
-
try {
-
-
muc = new MultiUserChat(getConnection(), roomName + "@conference."
-
+ getConnection().getServiceName());
-
-
muc.create(roomName);
-
-
Form form = muc.getConfigurationForm();
-
-
Form submitForm = form.createAnswerForm();
-
-
for (Iterator fields = form.getFields(); fields
-
.hasNext();) {
-
FormField field = (FormField) fields.next();
-
if (!FormField.TYPE_HIDDEN.equals(field.getType())
-
&& field.getVariable() != null) {
-
-
submitForm.setDefaultAnswer(field.getVariable());
-
}
-
}
-
-
List owners = new ArrayList();
-
owners.add(getConnection().getUser());
-
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
-
-
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
-
-
submitForm.setAnswer("muc#roomconfig_membersonly", false);
-
-
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
-
if (!password.equals("")) {
-
-
submitForm.setAnswer("muc#roomconfig_passwordprotectedroom",
-
true);
-
-
submitForm.setAnswer("muc#roomconfig_roomsecret", password);
-
}
-
-
-
-
submitForm.setAnswer("muc#roomconfig_enablelogging", true);
-
-
submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
-
-
submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
-
-
submitForm.setAnswer("x-muc#roomconfig_registration", false);
-
-
muc.sendConfigurationForm(submitForm);
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
return null;
-
}
-
return muc;
-
}
-
-
-
-
-
-
-
-
-
-
-
-
public MultiUserChat joinMultiUserChat(String user, String roomsName,
-
String password) {
-
if (getConnection() == null)
-
return null;
-
try {
-
-
MultiUserChat muc = new MultiUserChat(getConnection(), roomsName
-
+ "@conference." + getConnection().getServiceName());
-
-
DiscussionHistory history = new DiscussionHistory();
-
history.setMaxChars(0);
-
-
-
muc.join(user, password, history,
-
SmackConfiguration.getPacketReplyTimeout());
-
Log.i("MultiUserChat", "会议室【"+roomsName+"】加入成功........");
-
return muc;
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
Log.i("MultiUserChat", "会议室【"+roomsName+"】加入失败........");
-
return null;
-
}
-
}
-
-
-
-
-
-
-
public List findMulitUser(MultiUserChat muc) {
-
if (getConnection() == null)
-
return null;
-
List listUser = new ArrayList();
-
Iterator it = muc.getOccupants();
-
-
while (it.hasNext()) {
-
-
String name = StringUtils.parseResource(it.next());
-
listUser.add(name);
-
}
-
return listUser;
-
}
-
-
-
-
-
-
-
-
public void sendFile(String user, String filePath) {
-
if (getConnection() == null)
-
return;
-
-
FileTransferManager manager = new FileTransferManager(getConnection());
-
-
-
OutgoingFileTransfer transfer = manager
-
.createOutgoingFileTransfer(user);
-
-
-
try {
-
transfer.sendFile(new File(filePath), "You won't believe this!");
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
-
-
-
-
public Map>> getHisMessage() {
-
if (getConnection() == null)
-
return null;
-
Map>> offlineMsgs = null;
-
-
try {
-
OfflineMessageManager offlineManager = new OfflineMessageManager(
-
getConnection());
-
Iterator it = offlineManager.getMessages();
-
-
int count = offlineManager.getMessageCount();
-
if (count <= 0)
-
return null;
-
offlineMsgs = new HashMap>>();
-
-
while (it.hasNext()) {
-
Message message = it.next();
-
String fromUser = StringUtils.parseName(message.getFrom());
-
;
-
HashMap histrory = new HashMap();
-
histrory.put("useraccount",
-
StringUtils.parseName(getConnection().getUser()));
-
histrory.put("friendaccount", fromUser);
-
histrory.put("info", message.getBody());
-
histrory.put("type", "left");
-
if (offlineMsgs.containsKey(fromUser)) {
-
offlineMsgs.get(fromUser).add(histrory);
-
} else {
-
List> temp = new ArrayList>();
-
temp.add(histrory);
-
offlineMsgs.put(fromUser, temp);
-
}
-
}
-
offlineManager.deleteMessages();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return offlineMsgs;
-
}
-
-
-
-
-
-
-
-
-
public int IsUserOnLine(String user) {
-
String url = "http://"+SERVER_HOST+":9090/plugins/presence/status?" +
-
"jid="+ user +"@"+ SERVER_NAME +"&type=xml";
-
int shOnLineState = 0;
-
try {
-
URL oUrl = new URL(url);
-
URLConnection oConn = oUrl.openConnection();
-
if (oConn != null) {
-
BufferedReader oIn = new BufferedReader(new InputStreamReader(
-
oConn.getInputStream()));
-
if (null != oIn) {
-
String strFlag = oIn.readLine();
-
oIn.close();
-
System.out.println("strFlag"+strFlag);
-
if (strFlag.indexOf("type=\"unavailable\"") >= 0) {
-
shOnLineState = 2;
-
}
-
if (strFlag.indexOf("type=\"error\"") >= 0) {
-
shOnLineState = 0;
-
} else if (strFlag.indexOf("priority") >= 0
-
|| strFlag.indexOf("id=\"") >= 0) {
-
shOnLineState = 1;
-
}
-
}
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
-
return shOnLineState;
-
}
-
-
-
-
-
-
-
public void configureConnection(ProviderManager pm) {
-
-
-
pm.addIQProvider("query", "jabber:iq:private",
-
new PrivateDataManager.PrivateDataIQProvider());
-
-
-
try {
-
pm.addIQProvider("query", "jabber:iq:time",
-
Class.forName("org.jivesoftware.smackx.packet.Time"));
-
} catch (ClassNotFoundException e) {
-
Log.w("TestClient",
-
"Can't load class for org.jivesoftware.smackx.packet.Time");
-
}
-
-
-
pm.addExtensionProvider("x", "jabber:x:roster",
-
new RosterExchangeProvider());
-
-
-
pm.addExtensionProvider("x", "jabber:x:event",
-
new MessageEventProvider());
-
-
-
pm.addExtensionProvider("active",
-
"http://jabber.org/protocol/chatstates",
-
new ChatStateExtension.Provider());
-
pm.addExtensionProvider("composing",
-
"http://jabber.org/protocol/chatstates",
-
new ChatStateExtension.Provider());
-
pm.addExtensionProvider("paused",
-
"http://jabber.org/protocol/chatstates",
-
new ChatStateExtension.Provider());
-
pm.addExtensionProvider("inactive",
-
"http://jabber.org/protocol/chatstates",
-
new ChatStateExtension.Provider());
-
pm.addExtensionProvider("gone",
-
"http://jabber.org/protocol/chatstates",
-
new ChatStateExtension.Provider());
-
-
-
pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im",
-
new XHTMLExtensionProvider());
-
-
-
pm.addExtensionProvider("x", "jabber:x:conference",
-
new GroupChatInvitation.Provider());
-
-
-
pm.addIQProvider("query", "http://jabber.org/protocol/disco#items",
-
new DiscoverItemsProvider());
-
-
-
pm.addIQProvider("query", "http://jabber.org/protocol/disco#info",
-
new DiscoverInfoProvider());
-
-
-
pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());
-
-
-
pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user",
-
new MUCUserProvider());
-
-
-
pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin",
-
new MUCAdminProvider());
-
-
-
pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner",
-
new MUCOwnerProvider());
-
-
-
pm.addExtensionProvider("x", "jabber:x:delay",
-
new DelayInformationProvider());
-
-
-
try {
-
pm.addIQProvider("query", "jabber:iq:version",
-
Class.forName("org.jivesoftware.smackx.packet.Version"));
-
} catch (ClassNotFoundException e) {
-
-
}
-
-
-
pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());
-
-
-
pm.addIQProvider("offline", "http://jabber.org/protocol/offline",
-
new OfflineMessageRequest.Provider());
-
-
-
pm.addExtensionProvider("offline",
-
"http://jabber.org/protocol/offline",
-
new OfflineMessageInfo.Provider());
-
-
-
pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());
-
-
-
pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());
-
-
-
pm.addIQProvider("sharedgroup",
-
"http://www.jivesoftware.org/protocol/sharedgroup",
-
new SharedGroupsInfo.Provider());
-
-
-
pm.addExtensionProvider("addresses",
-
"http://jabber.org/protocol/address",
-
new MultipleAddressesProvider());
-
-
-
pm.addIQProvider("si", "http://jabber.org/protocol/si",
-
new StreamInitiationProvider());
-
-
pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams",
-
new BytestreamsProvider());
-
-
-
pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
-
pm.addIQProvider("command", "http://jabber.org/protocol/commands",
-
new AdHocCommandDataProvider());
-
pm.addExtensionProvider("malformed-action",
-
"http://jabber.org/protocol/commands",
-
new AdHocCommandDataProvider.MalformedActionError());
-
pm.addExtensionProvider("bad-locale",
-
"http://jabber.org/protocol/commands",
-
new AdHocCommandDataProvider.BadLocaleError());
-
pm.addExtensionProvider("bad-payload",
-
"http://jabber.org/protocol/commands",
-
new AdHocCommandDataProvider.BadPayloadError());
-
pm.addExtensionProvider("bad-sessionid",
-
"http://jabber.org/protocol/commands",
-
new AdHocCommandDataProvider.BadSessionIDError());
-
pm.addExtensionProvider("session-expired",
-
"http://jabber.org/protocol/commands",
-
new AdHocCommandDataProvider.SessionExpiredError());
-
}
-
-
}
-
-
-
-
-
-
-
-
public int IsUserOnLine(String user) {
-
String url = "http://"+SERVER_HOST+":9090/plugins/presence/status?" +
-
"jid="+ user +"@"+ SERVER_NAME +"&type=xml";
-
int shOnLineState = 0;
-
try {
-
URL oUrl = new URL(url);
-
URLConnection oConn = oUrl.openConnection();
-
if (oConn != null) {
-
BufferedReader oIn = new BufferedReader(new InputStreamReader(
-
oConn.getInputStream()));
-
if (null != oIn) {
-
String strFlag = oIn.readLine();
-
oIn.close();
-
System.out.println("strFlag"+strFlag);
-
if (strFlag.indexOf("type=\"unavailable\"") >= 0) {
-
shOnLineState = 2;
-
}
-
if (strFlag.indexOf("type=\"error\"") >= 0) {
-
shOnLineState = 0;
-
} else if (strFlag.indexOf("priority") >= 0
-
|| strFlag.indexOf("id=\"") >= 0) {
-
shOnLineState = 1;
-
}
-
}
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return shOnLineState;
-
}
调用该工具类的方法很简单,用了一个单例模式,里面的方法都可以用相同的方法调用
-
XmppConnection.getInstance().login(username,password)
希望对大家有所帮助~
阅读(2159) | 评论(0) | 转发(0) |