openfire Android学习(二)----对分组、好友和头像等一些操作

时间:2023-03-10 06:58:46
openfire Android学习(二)----对分组、好友和头像等一些操作

一、查询所有分组

通过Roster来获取所有分组,Roster可以通过connection.getRoster()来得到。

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 获取所有组
  3. *
  4. * @param roster
  5. * @return 所有组集合
  6. */
  7. public static List<RosterGroup> getGroups(Roster roster) {
  8. List<RosterGroup> grouplist = new ArrayList<RosterGroup>();
  9. Collection<RosterGroup> rosterGroup = roster.getGroups();
  10. Iterator<RosterGroup> i = rosterGroup.iterator();
  11. while (i.hasNext()) {
  12. grouplist.add(i.next());
  13. }
  14. return grouplist;
  15. }

二、添加分组

也一样通过roster来添加分组,groupName 为分组名。

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 添加一个分组
  3. *
  4. * @param roster
  5. * @param groupName
  6. * @return
  7. */
  8. public static boolean addGroup(Roster roster, String groupName) {
  9. try {
  10. roster.createGroup(groupName);
  11. return true;
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. return false;
  15. }
  16. }

三、查询某个组里面的所有好友


[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 获取某个组里面的所有好友
  3. *
  4. * @param roster
  5. * @param groupName
  6. *            组名
  7. * @return
  8. */
  9. public static List<RosterEntry> getEntriesByGroup(Roster roster,
  10. String groupName) {
  11. List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
  12. RosterGroup rosterGroup = roster.getGroup(groupName);
  13. Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
  14. Iterator<RosterEntry> i = rosterEntry.iterator();
  15. while (i.hasNext()) {
  16. Entrieslist.add(i.next());
  17. }
  18. return Entrieslist;
  19. }

四、查询所有好友信息

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 获取所有好友信息
  3. *
  4. * @param roster
  5. * @return
  6. */
  7. public static List<RosterEntry> getAllEntries(Roster roster) {
  8. List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
  9. Collection<RosterEntry> rosterEntry = roster.getEntries();
  10. Iterator<RosterEntry> i = rosterEntry.iterator();
  11. while (i.hasNext()) {
  12. Entrieslist.add(i.next());
  13. }
  14. return Entrieslist;
  15. }

五、获取用户VCard信息

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 获取用户VCard信息
  3. *
  4. * @param connection
  5. * @param user
  6. * @return
  7. * @throws XMPPException
  8. */
  9. public static VCard getUserVCard(XMPPConnection connection, String user)
  10. throws XMPPException {
  11. VCard vcard = new VCard();
  12. vcard.load(connection, user);
  13. return vcard;
  14. }

六、获取用户头像信息

通过Vcard来获取用户头像信息,可以把 InputStream 转换为自己想要的类型,InputStream 转Drawable

这篇文章里可以找到  http://blog.****.net/h7870181/article/details/8663760

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 获取用户头像信息
  3. *
  4. * @param connection
  5. * @param user
  6. * @return
  7. */
  8. public static Drawable getUserImage(XMPPConnection connection, String user) {
  9. ByteArrayInputStream bais = null;
  10. try {
  11. VCard vcard = new VCard();
  12. // 加入这句代码,解决No VCard for
  13. ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
  14. new org.jivesoftware.smackx.provider.VCardProvider());
  15. vcard.load(connection, user+"@"+connection.getServiceName());
  16. if (vcard == null || vcard.getAvatar() == null)
  17. return null;
  18. bais = new ByteArrayInputStream(vcard.getAvatar());
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. if (bais == null)
  23. return null;
  24. return FormatTools.getInstance().InputStream2Drawable(bais);
  25. }

七、添加好友(有、无分组)

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 添加好友 无分组
  3. *
  4. * @param roster
  5. * @param userName
  6. * @param name
  7. * @return
  8. */
  9. public static boolean addUser(Roster roster, String userName, String name) {
  10. try {
  11. roster.createEntry(userName, name, null);
  12. return true;
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. return false;
  16. }
  17. }
  18. /**
  19. * 添加好友 有分组
  20. *
  21. * @param roster
  22. * @param userName
  23. * @param name
  24. * @param groupName
  25. * @return
  26. */
  27. public static boolean addUser(Roster roster, String userName, String name,
  28. String groupName) {
  29. try {
  30. roster.createEntry(userName, name, new String[] { groupName });
  31. return true;
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. return false;
  35. }
  36. }

八、删除好友

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 删除好友
  3. *
  4. * @param roster
  5. * @param userName
  6. * @return
  7. */
  8. public static boolean removeUser(Roster roster, String userName) {
  9. try {
  10. if (userName.contains("@")) {
  11. userName = userName.split("@")[0];
  12. }
  13. RosterEntry entry = roster.getEntry(userName);
  14. System.out.println("删除好友:" + userName);
  15. System.out.println("User." + roster.getEntry(userName) == null);
  16. roster.removeEntry(entry);
  17. return true;
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. return false;
  21. }
  22. }

九、查询用户

serverDoMain 为服务器域名

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 查询用户
  3. *
  4. * @param connection
  5. * @param serverDomain
  6. * @param userName
  7. * @return
  8. * @throws XMPPException
  9. */
  10. public static List<User> searchUsers(XMPPConnection connection,
  11. String serverDomain, String userName) throws XMPPException {
  12. List<User> results = new ArrayList<User>();
  13. System.out.println("查询开始..............." + connection.getHost()
  14. + connection.getServiceName());
  15. UserSearchManager usm = new UserSearchManager(connection);
  16. Form searchForm = usm.getSearchForm(serverDomain);
  17. Form answerForm = searchForm.createAnswerForm();
  18. answerForm.setAnswer("userAccount", true);
  19. answerForm.setAnswer("userPhote", userName);
  20. ReportedData data = usm.getSearchResults(answerForm, serverDomain);
  21. Iterator<Row> it = data.getRows();
  22. Row row = null;
  23. User user = null;
  24. while (it.hasNext()) {
  25. user = new User();
  26. row = it.next();
  27. user.setUserAccount(row.getValues("userAccount").next().toString());
  28. user.setUserPhote(row.getValues("userPhote").next().toString());
  29. System.out.println(row.getValues("userAccount").next());
  30. System.out.println(row.getValues("userPhote").next());
  31. results.add(user);
  32. // 若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空
  33. }
  34. return results;
  35. }

十、修改用户头像

[java] view
plain
copyopenfire Android学习(二)----对分组、好友和头像等一些操作openfire Android学习(二)----对分组、好友和头像等一些操作
  1. /**
  2. * 修改用户头像
  3. *
  4. * @param connection
  5. * @param f
  6. * @throws XMPPException
  7. * @throws IOException
  8. */
  9. public static void changeImage(XMPPConnection connection, File f)
  10. throws XMPPException, IOException {
  11. VCard vcard = new VCard();
  12. vcard.load(connection);
  13. byte[] bytes;
  14. bytes = getFileBytes(f);
  15. String encodedImage = StringUtils.encodeBase64(bytes);
  16. vcard.setAvatar(bytes, encodedImage);
  17. vcard.setEncodedImage(encodedImage);
  18. vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>" + encodedImage
  19. + "</BINVAL>", true);
  20. ByteArrayInputStream bais = new ByteArrayInputStream(vcard.getAvatar());
  21. FormatTools.getInstance().InputStream2Bitmap(bais);