Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1399280
  • 博文数量: 188
  • 博客积分: 1784
  • 博客等级: 上尉
  • 技术积分: 2772
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-05 22:20
个人简介

发上等愿,结中等缘,享下等福;择高处立,就平处坐,向宽处行。

文章分类

全部博文(188)

文章存档

2020年(12)

2019年(11)

2018年(4)

2017年(3)

2016年(11)

2015年(22)

2014年(19)

2013年(25)

2012年(32)

2011年(49)

分类: Android平台

2015-06-23 13:53:51

下面就一起来学习一下,如何查询分组和添加分组等!

一、查询所有分组

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


  1. "white-space:pre">  /** 
  2.      * 获取所有组 
  3.      *  
  4.      * @param roster 
  5.      * @return 所有组集合 
  6.      */  
  7.     public static List getGroups(Roster roster) {  
  8.         List grouplist = new ArrayList();  
  9.         Collection rosterGroup = roster.getGroups();  
  10.         Iterator i = rosterGroup.iterator();  
  11.         while (i.hasNext()) {  
  12.             grouplist.add(i.next());  
  13.         }  
  14.         return grouplist;  
  15.     }  


二、添加分组


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


  1. "white-space:pre">  /** 
  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.     }  



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

很简单不解释...


  1. "white-space:pre">  /** 
  2.      * 获取某个组里面的所有好友 
  3.      *  
  4.      * @param roster 
  5.      * @param groupName 
  6.      *            组名 
  7.      * @return 
  8.      */  
  9.     public static List getEntriesByGroup(Roster roster,  
  10.             String groupName) {  
  11.         List Entrieslist = new ArrayList();  
  12.         RosterGroup rosterGroup = roster.getGroup(groupName);  
  13.         Collection rosterEntry = rosterGroup.getEntries();  
  14.         Iterator i = rosterEntry.iterator();  
  15.         while (i.hasNext()) {  
  16.             Entrieslist.add(i.next());  
  17.         }  
  18.         return Entrieslist;  
  19.     }  


四、查询所有好友信息


很简单


  1. "white-space:pre">  /** 
  2.      * 获取所有好友信息 
  3.      *  
  4.      * @param roster 
  5.      * @return 
  6.      */  
  7.     public static List getAllEntries(Roster roster) {  
  8.         List Entrieslist = new ArrayList();  
  9.         Collection rosterEntry = roster.getEntries();  
  10.         Iterator i = rosterEntry.iterator();  
  11.         while (i.hasNext()) {  
  12.             Entrieslist.add(i.next());  
  13.         }  
  14.         return Entrieslist;  
  15.     }  


五、获取用户VCard信息



  1. "white-space:pre">  /** 
  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.csdn.net/h7870181/article/details/8663760


  1. "white-space:pre">  /** 
  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.   
  16.             vcard.load(connection, user+"@"+connection.getServiceName());  
  17.   
  18.             if (vcard == null || vcard.getAvatar() == null)  
  19.                 return null;  
  20.             bais = new ByteArrayInputStream(vcard.getAvatar());  
  21.   
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         if (bais == null)  
  26.             return null;  
  27.         return FormatTools.getInstance().InputStream2Drawable(bais);  
  28.     }  


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



  1. "white-space:pre">  /** 
  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.      *  
  22.      * @param roster 
  23.      * @param userName 
  24.      * @param name 
  25.      * @param groupName 
  26.      * @return 
  27.      */  
  28.     public static boolean addUser(Roster roster, String userName, String name,  
  29.             String groupName) {  
  30.         try {  
  31.             roster.createEntry(userName, name, new String[] { groupName });  
  32.             return true;  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.             return false;  
  36.         }  
  37.     }  


八、删除好友



  1. "white-space:pre">  /** 
  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.   
  14.             RosterEntry entry = roster.getEntry(userName);  
  15.             System.out.println("删除好友:" + userName);  
  16.             System.out.println("User." + roster.getEntry(userName) == null);  
  17.             roster.removeEntry(entry);  
  18.   
  19.             return true;  
  20.         } catch (Exception e) {  
  21.             e.printStackTrace();  
  22.             return false;  
  23.         }  
  24.     }  


九、查询用户 


serverDoMain 为服务器域名


  1. "white-space:pre">  /** 
  2.      * 查询用户 
  3.      *  
  4.      * @param connection 
  5.      * @param serverDomain 
  6.      * @param userName 
  7.      * @return 
  8.      * @throws XMPPException 
  9.      */  
  10.     public static List searchUsers(XMPPConnection connection,  
  11.             String serverDomain, String userName) throws XMPPException {  
  12.         List results = new ArrayList();  
  13.         System.out.println("查询开始..............." + connection.getHost()  
  14.                 + connection.getServiceName());  
  15.   
  16.         UserSearchManager usm = new UserSearchManager(connection);  
  17.   
  18.         Form searchForm = usm.getSearchForm(serverDomain);  
  19.         Form answerForm = searchForm.createAnswerForm();  
  20.         answerForm.setAnswer("userAccount"true);  
  21.         answerForm.setAnswer("userPhote", userName);  
  22.         ReportedData data = usm.getSearchResults(answerForm, serverDomain);  
  23.   
  24.         Iterator it = data.getRows();  
  25.         Row row = null;  
  26.         User user = null;  
  27.         while (it.hasNext()) {  
  28.             user = new User();  
  29.             row = it.next();  
  30.             user.setUserAccount(row.getValues("userAccount").next().toString());  
  31.             user.setUserPhote(row.getValues("userPhote").next().toString());  
  32.   
  33.             System.out.println(row.getValues("userAccount").next());  
  34.             System.out.println(row.getValues("userPhote").next());  
  35.             results.add(user);  
  36.             // 若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空  
  37.         }  
  38.         return results;  
  39.     }  


十、修改用户头像



  1. "white-space:pre">  /** 
  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.   
  12.         VCard vcard = new VCard();  
  13.         vcard.load(connection);  
  14.   
  15.         byte[] bytes;  
  16.   
  17.         bytes = getFileBytes(f);  
  18.         String encodedImage = StringUtils.encodeBase64(bytes);  
  19.         vcard.setAvatar(bytes, encodedImage);  
  20.         vcard.setEncodedImage(encodedImage);  
  21.         vcard.setField("PHOTO""image/jpg" + encodedImage  
  22.                 + ""true);  
  23.   
  24.         ByteArrayInputStream bais = new ByteArrayInputStream(vcard.getAvatar());  
  25.         FormatTools.getInstance().InputStream2Bitmap(bais);  
  26.   
  27.         vcard.save(connection);  
  28.     }  
阅读(1772) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~