分享

Android基于XMPP Smack Openfire下学习开发IM(二)对分组、好友和头像等一些操作

 leomuyi 2014-01-08

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

一、查询所有分组

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

  1. <span style="white-space:pre">  </span>/** 
  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 为分组名。

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

四、查询所有好友信息

很简单

  1. <span style="white-space:pre">  </span>/** 
  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信息

  1. <span style="white-space:pre">  </span>/** 
  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. <span style="white-space:pre">  </span>/** 
  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. <span style="white-space:pre">  </span>/** 
  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. <span style="white-space:pre">  </span>/** 
  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. <span style="white-space:pre">  </span>/** 
  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.   
  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<Row> 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. <span style="white-space:pre">  </span>/** 
  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", "<TYPE>image/jpg</TYPE><BINVAL>" + encodedImage  
  22.                 + "</BINVAL>", true);  
  23.   
  24.         ByteArrayInputStream bais = new ByteArrayInputStream(vcard.getAvatar());  
  25.         FormatTools.getInstance().InputStream2Bitmap(bais);  
  26.   
  27.         vcard.save(connection);  
  28.     }  





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多