我的世界java.io.ioexception:Bad packet.dll id 72

Android基于XMPP Smack Openfire下学习开发IM(六)总结 - 肖赛SoAi
- 博客频道 - CSDN.NET
14234人阅读
不管学习什么都应该总结
这里我把关于Xmpp的一些方法整理到一个工具类中了
我就分享给大家
XmppConnection.java
package com.techrare.
import java.io.BufferedInputS
import java.io.BufferedR
import java.io.ByteArrayInputS
import java.io.F
import java.io.FileInputS
import java.io.IOE
import java.io.InputStreamR
import java.net.URL;
import java.net.URLC
import java.util.ArrayL
import java.util.C
import java.util.HashM
import java.util.I
import java.util.L
import java.util.M
import org.jivesoftware.smack.ConnectionC
import org.jivesoftware.smack.PacketC
import org.jivesoftware.smack.R
import org.jivesoftware.smack.RosterE
import org.jivesoftware.smack.RosterG
import org.jivesoftware.smack.SmackC
import org.jivesoftware.smack.XMPPC
import org.jivesoftware.smack.XMPPE
import org.jivesoftware.smack.filter.AndF
import org.jivesoftware.smack.filter.PacketF
import org.jivesoftware.smack.filter.PacketIDF
import org.jivesoftware.smack.filter.PacketTypeF
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.M
import org.jivesoftware.smack.packet.P
import org.jivesoftware.smack.packet.P
import org.jivesoftware.smack.packet.R
import org.jivesoftware.smack.provider.PrivacyP
import org.jivesoftware.smack.provider.ProviderM
import org.jivesoftware.smack.util.StringU
import org.jivesoftware.smackx.F
import org.jivesoftware.smackx.FormF
import org.jivesoftware.smackx.GroupChatI
import org.jivesoftware.smackx.OfflineMessageM
import org.jivesoftware.smackx.PrivateDataM
import org.jivesoftware.smackx.ReportedD
import org.jivesoftware.smackx.ReportedData.R
import org.jivesoftware.smackx.ServiceDiscoveryM
import org.jivesoftware.smackx.bytestreams.socks5.provider.BytestreamsP
import org.jivesoftware.smackx.filetransfer.FileTransferM
import org.jivesoftware.smackx.filetransfer.OutgoingFileT
import org.jivesoftware.smackx.muc.DiscussionH
import org.jivesoftware.smackx.muc.HostedR
import org.jivesoftware.smackx.muc.MultiUserC
import org.jivesoftware.smackx.packet.ChatStateE
import org.jivesoftware.smackx.packet.LastA
import org.jivesoftware.smackx.packet.OfflineMessageI
import org.jivesoftware.smackx.packet.OfflineMessageR
import org.jivesoftware.smackx.packet.SharedGroupsI
import org.jivesoftware.smackx.packet.VC
import org.jivesoftware.smackx.provider.AdHocCommandDataP
import org.jivesoftware.smackx.provider.DataFormP
import org.jivesoftware.smackx.provider.DelayInformationP
import org.jivesoftware.smackx.provider.DiscoverInfoP
import org.jivesoftware.smackx.provider.DiscoverItemsP
import org.jivesoftware.smackx.provider.MUCAdminP
import org.jivesoftware.smackx.provider.MUCOwnerP
import org.jivesoftware.smackx.provider.MUCUserP
import org.jivesoftware.smackx.provider.MessageEventP
import org.jivesoftware.smackx.provider.MultipleAddressesP
import org.jivesoftware.smackx.provider.RosterExchangeP
import org.jivesoftware.smackx.provider.StreamInitiationP
import org.jivesoftware.smackx.provider.VCardP
import org.jivesoftware.smackx.provider.XHTMLExtensionP
import org.jivesoftware.smackx.search.UserS
import org.jivesoftware.smackx.search.UserSearchM
import android.graphics.drawable.D
import android.util.L
import com.techrare.listener.TaxiConnectionL
* XmppConnection 工具类
* @author 肖赛SoAi
public class XmppConnection {
private int SERVER_PORT = 5222;
private String SERVER_HOST = &127.0.0.1&;
private XMPPConnection connection =
private String SERVER_NAME = &ubuntuserver4java&;
private static XmppConnection xmppConnection = new XmppConnection();
private TaxiConnectionListener connectionL
* 单例模式
synchronized public static XmppConnection getInstance() {
return xmppC
* 创建连接
public XMPPConnection getConnection() {
if (connection == null) {
openConnection();
* 打开连接
public boolean openConnection() {
if (null == connection || !connection.isAuthenticated()) {
XMPPConnection.DEBUG_ENABLED =// 开启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());
} catch (XMPPException xe) {
xe.printStackTrace();
connection =
* 关闭连接
public void closeConnection() {
if(connection!=null){
//移除連接監聽
//connection.removeConnectionListener(connectionListener);
if(connection.isConnected())
connection.disconnect();
connection =
Log.i(&XmppConnection&, &關閉連接&);
* @param account
* @param password
public boolean login(String account, String password) {
if (getConnection() == null)
getConnection().login(account, password);
// 更改在綫狀態
Presence presence = new Presence(Presence.Type.available);
getConnection().sendPacket(presence);
// 添加連接監聽
connectionListener = new TaxiConnectionListener();
getConnection().addConnectionListener(connectionListener);
} catch (XMPPException xe) {
xe.printStackTrace();
* @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,是&@&前面的部分。
reg.setUsername(account);
reg.setPassword(password);
// 这边addAttribute不能为空,否则出错。所以做个标志是android手机创建的吧!!!!!
reg.addAttribute(&android&, &geolo_createUser_android&);
PacketFilter filter = new AndFilter(new PacketIDFilter(
reg.getPacketID()), new PacketTypeFilter(IQ.class));
PacketCollector collector = getConnection().createPacketCollector(
getConnection().sendPacket(reg);
IQ result = (IQ) collector.nextResult(SmackConfiguration
.getPacketReplyTimeout());
// Stop queuing results停止请求results(是否成功的结果)
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.getType() == IQ.Type.ERROR)
if (result.getError().toString().equalsIgnoreCase(&conflict(409)&)) {
Log.e(&regist&, &IQ.Type.ERROR: &
+ result.getError().toString());
return &2&;
Log.e(&regist&, &IQ.Type.ERROR: &
+ result.getError().toString());
return &3&;
* 更改用户状态
public void setPresence(int code) {
XMPPConnection con = getConnection();
if (con == null)
switch (code) {
presence = new Presence(Presence.Type.available);
con.sendPacket(presence);
Log.v(&state&, &设置在线&);
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.chat);
con.sendPacket(presence);
Log.v(&state&, &设置Q我吧&);
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.dnd);
con.sendPacket(presence);
Log.v(&state&, &设置忙碌&);
presence = new Presence(Presence.Type.available);
presence.setMode(Presence.Mode.away);
con.sendPacket(presence);
Log.v(&state&, &设置离开&);
Roster roster = con.getRoster();
Collection&RosterEntry& 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&, &设置隐身&);
presence = new Presence(Presence.Type.unavailable);
con.sendPacket(presence);
Log.v(&state&, &设置离线&);
* 获取所有组
* @return 所有组集合
public List&RosterGroup& getGroups() {
if (getConnection() == null)
List&RosterGroup& grouplist = new ArrayList&RosterGroup&();
Collection&RosterGroup& rosterGroup = getConnection().getRoster()
.getGroups();
Iterator&RosterGroup& i = rosterGroup.iterator();
while (i.hasNext()) {
grouplist.add(i.next());
* 获取某个组里面的所有好友
* @param roster
* @param groupName
public List&RosterEntry& getEntriesByGroup(String groupName) {
if (getConnection() == null)
List&RosterEntry& Entrieslist = new ArrayList&RosterEntry&();
RosterGroup rosterGroup = getConnection().getRoster().getGroup(
groupName);
Collection&RosterEntry& rosterEntry = rosterGroup.getEntries();
Iterator&RosterEntry& i = rosterEntry.iterator();
while (i.hasNext()) {
Entrieslist.add(i.next());
* 获取所有好友信息
public List&RosterEntry& getAllEntries() {
if (getConnection() == null)
List&RosterEntry& Entrieslist = new ArrayList&RosterEntry&();
Collection&RosterEntry& rosterEntry = getConnection().getRoster()
.getEntries();
Iterator&RosterEntry& i = rosterEntry.iterator();
while (i.hasNext()) {
Entrieslist.add(i.next());
* 获取用户VCard信息
* @param connection
* @param user
* @throws XMPPException
public VCard getUserVCard(String user) {
if (getConnection() == null)
VCard vcard = new VCard();
vcard.load(getConnection(), user);
} catch (XMPPException e) {
e.printStackTrace();
* 获取用户头像信息
* @param connection
* @param user
public Drawable getUserImage(String user) {
if (getConnection() == null)
ByteArrayInputStream bais =
VCard vcard = new VCard();
// 加入这句代码,解决No VCard for
ProviderManager.getInstance().addIQProvider(&vCard&, &vcard-temp&,
new org.jivesoftware.smackx.provider.VCardProvider());
if (user == && || user == null || user.trim().length() &= 0) {
vcard.load(getConnection(), user + &@&
+ getConnection().getServiceName());
if (vcard == null || vcard.getAvatar() == null)
bais = new ByteArrayInputStream(vcard.getAvatar());
} catch (Exception e) {
e.printStackTrace();
return FormatTools.getInstance().InputStream2Drawable(bais);
* 添加一个分组
* @param groupName
public boolean addGroup(String groupName) {
if (getConnection() == null)
getConnection().getRoster().createGroup(groupName);
Log.v(&addGroup&, groupName + &創建成功&);
} catch (Exception e) {
e.printStackTrace();
* 删除分组
* @param groupName
public boolean removeGroup(String groupName) {
* 添加好友 无分组
* @param userName
* @param name
public boolean addUser(String userName, String name) {
if (getConnection() == null)
getConnection().getRoster().createEntry(userName, name, null);
} catch (Exception e) {
e.printStackTrace();
* 添加好友 有分组
* @param userName
* @param name
* @param groupName
public boolean addUser(String userName, String name, String groupName) {
if (getConnection() == null)
Presence subscription = new Presence(Presence.Type.subscribed);
subscription.setTo(userName);
userName += &@& + getConnection().getServiceName();
getConnection().sendPacket(subscription);
getConnection().getRoster().createEntry(userName, name,
new String[] { groupName });
} catch (Exception e) {
e.printStackTrace();
* 删除好友
* @param userName
public boolean removeUser(String userName) {
if (getConnection() == null)
RosterEntry entry =
if (userName.contains(&@&))
entry = getConnection().getRoster().getEntry(userName);
entry = getConnection().getRoster().getEntry(
userName + &@& + getConnection().getServiceName());
if (entry == null)
entry = getConnection().getRoster().getEntry(userName);
getConnection().getRoster().removeEntry(entry);
} catch (Exception e) {
e.printStackTrace();
* 查询用户
* @param userName
* @throws XMPPException
public List&HashMap&String, String&& searchUsers(String userName) {
if (getConnection() == null)
HashMap&String, String& user =
List&HashMap&String, String&& results = new ArrayList&HashMap&String, String&&();
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&Row& it = data.getRows();
while (it.hasNext()) {
user = new HashMap&String, String&();
row = it.next();
user.put(&userAccount&, row.getValues(&userAccount&).next()
.toString());
user.put(&userPhote&, row.getValues(&userPhote&).next()
.toString());
results.add(user);
// 若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空
} catch (XMPPException e) {
e.printStackTrace();
* 修改心情
* @param connection
* @param status
public void changeStateMessage(String status) {
if (getConnection() == null)
Presence presence = new Presence(Presence.Type.available);
presence.setStatus(status);
getConnection().sendPacket(presence);
* 修改用户头像
* @param file
public boolean changeImage(File file) {
if (getConnection() == null)
VCard vcard = new VCard();
vcard.load(getConnection());
bytes = getFileBytes(file);
String encodedImage = StringUtils.encodeBase64(bytes);
vcard.setAvatar(bytes, encodedImage);
vcard.setEncodedImage(encodedImage);
vcard.setField(&PHOTO&, &&TYPE&image/jpg&/TYPE&&BINVAL&&
+ encodedImage + &&/BINVAL&&, true);
ByteArrayInputStream bais = new ByteArrayInputStream(
vcard.getAvatar());
FormatTools.getInstance().InputStream2Bitmap(bais);
vcard.save(getConnection());
} catch (Exception e) {
e.printStackTrace();
* 文件转字节
* @param file
* @throws IOException
private byte[] getFileBytes(File file) throws IOException {
BufferedInputStream bis =
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&);
} finally {
if (bis != null) {
bis.close();
* 删除当前用户
public boolean deleteAccount() {
if (getConnection() == null)
getConnection().getAccountManager().deleteAccount();
} catch (XMPPException e) {
* 修改密码
public boolean changePassword(String pwd) {
if (getConnection() == null)
getConnection().getAccountManager().changePassword(pwd);
} catch (XMPPException e) {
* 初始化会议室列表
public List&HostedRoom& getHostRooms() {
if (getConnection() == null)
Collection&HostedRoom& hostrooms =
List&HostedRoom& roominfos = new ArrayList&HostedRoom&();
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();
* 创建房间
* @param roomName
public MultiUserChat createRoom(String user, String roomName,
String password) {
if (getConnection() == null)
MultiUserChat muc =
// 创建一个MultiUserChat
muc = new MultiUserChat(getConnection(), roomName + &@conference.&
+ getConnection().getServiceName());
// 创建聊天室
muc.create(roomName);
// 获得聊天室的配置表单
Form form = muc.getConfigurationForm();
// 根据原始表单创建一个要提交的新表单。
Form submitForm = form.createAnswerForm();
// 向要提交的表单添加默认答复
for (Iterator&FormField& 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&String& owners = new ArrayList&String&();
owners.add(getConnection().getUser());// 用户JID
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&,
// 设置进入密码
submitForm.setAnswer(&muc#roomconfig_roomsecret&, password);
// 能够发现占有者真实 JID 的角色
// submitForm.setAnswer(&muc#roomconfig_whois&, &anyone&);
// 登录房间对话
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();
* 加入会议室
* @param user
* @param password
会议室密码
* @param roomsName
public MultiUserChat joinMultiUserChat(String user, String roomsName,
String password) {
if (getConnection() == null)
// 使用XMPPConnection创建一个MultiUserChat窗口
MultiUserChat muc = new MultiUserChat(getConnection(), roomsName
+ &@conference.& + getConnection().getServiceName());
// 聊天室服务将会决定要接受的历史记录数量
DiscussionHistory history = new DiscussionHistory();
history.setMaxChars(0);
// history.setSince(new Date());
// 用户加入聊天室
muc.join(user, password, history,
SmackConfiguration.getPacketReplyTimeout());
Log.i(&MultiUserChat&, &会议室【&+roomsName+&】加入成功........&);
} catch (XMPPException e) {
e.printStackTrace();
Log.i(&MultiUserChat&, &会议室【&+roomsName+&】加入失败........&);
* 查询会议室成员名字
* @param muc
public List&String& findMulitUser(MultiUserChat muc) {
if (getConnection() == null)
List&String& listUser = new ArrayList&String&();
Iterator&String& it = muc.getOccupants();
// 遍历出聊天室人员名称
while (it.hasNext()) {
// 聊天室成员名字
String name = StringUtils.parseResource(it.next());
listUser.add(name);
return listU
* 发送文件
* @param user
* @param filePath
public void sendFile(String user, String filePath) {
if (getConnection() == null)
// 创建文件传输管理器
FileTransferManager manager = new FileTransferManager(getConnection());
// 创建输出的文件传输
OutgoingFileTransfer transfer = manager
.createOutgoingFileTransfer(user);
// 发送文件
transfer.sendFile(new File(filePath), &You won't believe this!&);
} catch (XMPPException e) {
e.printStackTrace();
* 获取离线消息
public Map&String, List&HashMap&String, String&&& getHisMessage() {
if (getConnection() == null)
Map&String, List&HashMap&String, String&&& offlineMsgs =
OfflineMessageManager offlineManager = new OfflineMessageManager(
getConnection());
Iterator&Message& it = offlineManager.getMessages();
int count = offlineManager.getMessageCount();
if (count &= 0)
offlineMsgs = new HashMap&String, List&HashMap&String, String&&&();
while (it.hasNext()) {
Message message = it.next();
String fromUser = StringUtils.parseName(message.getFrom());
HashMap&String, String& histrory = new HashMap&String, String&();
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);
List&HashMap&String, String&& temp = new ArrayList&HashMap&String, String&&();
temp.add(histrory);
offlineMsgs.put(fromUser, temp);
offlineManager.deleteMessages();
} catch (Exception e) {
e.printStackTrace();
return offlineM
* 判断OpenFire用户的状态 strUrl :
* url格式 - :9090/plugins/presence
* /status?jid=user1@SERVER_NAME&type=xml
* 返回值 : 0 - 用户不存在; 1 - 用户在线; 2 - 用户离线
* 说明 :必须要求 OpenFire加载 presence 插件,同时设置任何人都可以访问
public int IsUserOnLine(String user) {
String url = &http://&+SERVER_HOST+&:9090/plugins/presence/status?& +
&jid=&+ user +&@&+ SERVER_NAME +&&type=xml&;
int shOnLineState = 0; // 不存在
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 shOnLineS
* 加入providers的函数 ASmack在/META-INF缺少一个smack.providers 文件
* @param pm
public void configureConnection(ProviderManager pm) {
// Private Data Storage
pm.addIQProvider(&query&, &jabber:iq:private&,
new PrivateDataManager.PrivateDataIQProvider());
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&);
// Roster Exchange
pm.addExtensionProvider(&x&, &jabber:x:roster&,
new RosterExchangeProvider());
// Message Events
pm.addExtensionProvider(&x&, &jabber:x:event&,
new MessageEventProvider());
// Chat State
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());
// Group Chat Invitations
pm.addExtensionProvider(&x&, &jabber:x:conference&,
new GroupChatInvitation.Provider());
// Service Discovery # Items
pm.addIQProvider(&query&, &http://jabber.org/protocol/disco#items&,
new DiscoverItemsProvider());
// Service Discovery # Info
pm.addIQProvider(&query&, &http://jabber.org/protocol/disco#info&,
new DiscoverInfoProvider());
// Data Forms
pm.addExtensionProvider(&x&, &jabber:x:data&, new DataFormProvider());
// MUC User
pm.addExtensionProvider(&x&, &http://jabber.org/protocol/muc#user&,
new MUCUserProvider());
// MUC Admin
pm.addIQProvider(&query&, &http://jabber.org/protocol/muc#admin&,
new MUCAdminProvider());
// MUC Owner
pm.addIQProvider(&query&, &http://jabber.org/protocol/muc#owner&,
new MUCOwnerProvider());
// Delayed Delivery
pm.addExtensionProvider(&x&, &jabber:x:delay&,
new DelayInformationProvider());
// Version
pm.addIQProvider(&query&, &jabber:iq:version&,
Class.forName(&org.jivesoftware.smackx.packet.Version&));
} catch (ClassNotFoundException e) {
// Not sure what's happening here.
pm.addIQProvider(&vCard&, &vcard-temp&, new VCardProvider());
// Offline Message Requests
pm.addIQProvider(&offline&, &http://jabber.org/protocol/offline&,
new OfflineMessageRequest.Provider());
// Offline Message Indicator
pm.addExtensionProvider(&offline&,
&http://jabber.org/protocol/offline&,
new OfflineMessageInfo.Provider());
// Last Activity
pm.addIQProvider(&query&, &jabber:iq:last&, new LastActivity.Provider());
// User Search
pm.addIQProvider(&query&, &jabber:iq:search&, new UserSearch.Provider());
// SharedGroupsInfo
pm.addIQProvider(&sharedgroup&,
&http://www.jivesoftware.org/protocol/sharedgroup&,
new SharedGroupsInfo.Provider());
// JEP-33: Extended Stanza Addressing
pm.addExtensionProvider(&addresses&,
&http://jabber.org/protocol/address&,
new MultipleAddressesProvider());
// FileTransfer
pm.addIQProvider(&si&, &http://jabber.org/protocol/si&,
new StreamInitiationProvider());
pm.addIQProvider(&query&, &http://jabber.org/protocol/bytestreams&,
new BytestreamsProvider());
// Privacy
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());
* 判断OpenFire用户的状态 strUrl :
* url格式 - :9090/plugins/presence
* /status?jid=user1@SERVER_NAME&type=xml
* 返回值 : 0 - 用户不存在; 1 - 用户在线; 2 - 用户离线
* 说明 :必须要求 OpenFire加载 presence 插件,同时设置任何人都可以访问
public int IsUserOnLine(String user) {
String url = &http://&+SERVER_HOST+&:9090/plugins/presence/status?& +
&jid=&+ user +&@&+ SERVER_NAME +&&type=xml&;
int shOnLineState = 0; // 不存在
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 shOnLineS
调用该工具类的方法很简单,用了一个单例模式,里面的方法都可以用相同的方法调用
XmppConnection.getInstance().login(username,password)
希望对大家有所帮助~
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:363773次
积分:4350
积分:4350
排名:第2816名
原创:73篇
评论:352条
(1)(1)(2)(2)(1)(4)(2)(2)(6)(6)(3)(2)(10)(6)(5)(14)(8)(2)(3)

我要回帖

更多关于 cisco packet tracer 的文章

 

随机推荐