下面就一起来学习一下,如何查询分组和添加分组等!
一、查询所有分组
通过Roster来获取所有分组,Roster可以通过connection.getRoster()来得到。
-
"white-space:pre">
-
-
-
-
-
-
public static List getGroups(Roster roster) {
-
List grouplist = new ArrayList();
-
Collection rosterGroup = roster.getGroups();
-
Iterator i = rosterGroup.iterator();
-
while (i.hasNext()) {
-
grouplist.add(i.next());
-
}
-
return grouplist;
-
}
二、添加分组
也一样通过roster来添加分组,groupName 为分组名。
-
"white-space:pre">
-
-
-
-
-
-
-
public static boolean addGroup(Roster roster, String groupName) {
-
try {
-
roster.createGroup(groupName);
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
三、查询某个组里面的所有好友
很简单不解释...
-
"white-space:pre">
-
-
-
-
-
-
-
-
public static List getEntriesByGroup(Roster roster,
-
String groupName) {
-
List Entrieslist = new ArrayList();
-
RosterGroup rosterGroup = roster.getGroup(groupName);
-
Collection rosterEntry = rosterGroup.getEntries();
-
Iterator i = rosterEntry.iterator();
-
while (i.hasNext()) {
-
Entrieslist.add(i.next());
-
}
-
return Entrieslist;
-
}
四、查询所有好友信息
很简单
-
"white-space:pre">
-
-
-
-
-
-
public static List getAllEntries(Roster roster) {
-
List Entrieslist = new ArrayList();
-
Collection rosterEntry = roster.getEntries();
-
Iterator i = rosterEntry.iterator();
-
while (i.hasNext()) {
-
Entrieslist.add(i.next());
-
}
-
return Entrieslist;
-
}
五、获取用户VCard信息
-
"white-space:pre">
-
-
-
-
-
-
-
-
public static VCard getUserVCard(XMPPConnection connection, String user)
-
throws XMPPException {
-
VCard vcard = new VCard();
-
vcard.load(connection, user);
-
return vcard;
-
}
六、获取用户头像信息
通过Vcard来获取用户头像信息,可以把 InputStream 转换为自己想要的类型,InputStream 转Drawable
这篇文章里可以找到 http://blog.csdn.net/h7870181/article/details/8663760
-
"white-space:pre">
-
-
-
-
-
-
-
public static Drawable getUserImage(XMPPConnection connection, String user) {
-
ByteArrayInputStream bais = null;
-
try {
-
VCard vcard = new VCard();
-
-
ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
-
new org.jivesoftware.smackx.provider.VCardProvider());
-
-
vcard.load(connection, user+"@"+connection.getServiceName());
-
-
if (vcard == null || vcard.getAvatar() == null)
-
return null;
-
bais = new ByteArrayInputStream(vcard.getAvatar());
-
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
if (bais == null)
-
return null;
-
return FormatTools.getInstance().InputStream2Drawable(bais);
-
}
七、添加好友(有、无分组)
-
"white-space:pre">
-
-
-
-
-
-
-
-
public static boolean addUser(Roster roster, String userName, String name) {
-
try {
-
roster.createEntry(userName, name, null);
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
-
-
-
-
-
-
-
-
-
-
-
public static boolean addUser(Roster roster, String userName, String name,
-
String groupName) {
-
try {
-
roster.createEntry(userName, name, new String[] { groupName });
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
八、删除好友
-
"white-space:pre">
-
-
-
-
-
-
-
public static boolean removeUser(Roster roster, String userName) {
-
try {
-
if (userName.contains("@")) {
-
userName = userName.split("@")[0];
-
}
-
-
RosterEntry entry = roster.getEntry(userName);
-
System.out.println("删除好友:" + userName);
-
System.out.println("User." + roster.getEntry(userName) == null);
-
roster.removeEntry(entry);
-
-
return true;
-
} catch (Exception e) {
-
e.printStackTrace();
-
return false;
-
}
-
}
九、查询用户
serverDoMain 为服务器域名
-
"white-space:pre">
-
-
-
-
-
-
-
-
-
public static List searchUsers(XMPPConnection connection,
-
String serverDomain, String userName) throws XMPPException {
-
List results = new ArrayList();
-
System.out.println("查询开始..............." + connection.getHost()
-
+ connection.getServiceName());
-
-
UserSearchManager usm = new UserSearchManager(connection);
-
-
Form searchForm = usm.getSearchForm(serverDomain);
-
Form answerForm = searchForm.createAnswerForm();
-
answerForm.setAnswer("userAccount", true);
-
answerForm.setAnswer("userPhote", userName);
-
ReportedData data = usm.getSearchResults(answerForm, serverDomain);
-
-
Iterator
it = data.getRows();
-
Row row = null;
-
User user = null;
-
while (it.hasNext()) {
-
user = new User();
-
row = it.next();
-
user.setUserAccount(row.getValues("userAccount").next().toString());
-
user.setUserPhote(row.getValues("userPhote").next().toString());
-
-
System.out.println(row.getValues("userAccount").next());
-
System.out.println(row.getValues("userPhote").next());
-
results.add(user);
-
-
}
-
return results;
-
}
十、修改用户头像
-
"white-space:pre">
-
-
-
-
-
-
-
-
public static void changeImage(XMPPConnection connection, File f)
-
throws XMPPException, IOException {
-
-
VCard vcard = new VCard();
-
vcard.load(connection);
-
-
byte[] bytes;
-
-
bytes = getFileBytes(f);
-
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(connection);
-
}
阅读(1812) | 评论(0) | 转发(0) |