Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3431964
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: 嵌入式

2011-07-25 13:35:34

转载:http://blog.csdn.net/wklken/archive/2011/06/01/6460112.aspx

上一篇主要是会话的管理

继续,这是显示用户列表方面的

1.       用户列表

Smack主要使用Roster进行列表管理的

connection.getRoster();

  1. /**  
  2.      * 返回所有组信息   
  3.      *   
  4.      * @return List(RosterGroup)  
  5.      */    
  6.     public static List getGroups(Roster roster) {    
  7.         List groupsList = new ArrayList();    
  8.         Collection rosterGroup = roster.getGroups();    
  9.         Iterator i = rosterGroup.iterator();    
  10.         while (i.hasNext())    
  11.             groupsList.add(i.next());    
  12.         return groupsList;    
  13.     }    
  14.     
  15.     /**  
  16.      * 返回相应(groupName)组里的所有用户  
  17.      *   
  18.      * @return List(RosterEntry)  
  19.      */    
  20.     public static List getEntriesByGroup(Roster roster,    
  21.             String groupName) {    
  22.         List EntriesList = new ArrayList();    
  23.         RosterGroup rosterGroup = roster.getGroup(groupName);    
  24.         Collection rosterEntry = rosterGroup.getEntries();    
  25.         Iterator i = rosterEntry.iterator();    
  26.         while (i.hasNext())    
  27.             EntriesList.add(i.next());    
  28.         return EntriesList;    
  29.     }    
  30.     
  31.     /**  
  32.      * 返回所有用户信息   
  33.      *   
  34.      * @return List(RosterEntry)  
  35.      */    
  36.     public static List getAllEntries(Roster roster) {    
  37.         List EntriesList = new ArrayList();    
  38.         Collection rosterEntry = roster.getEntries();    
  39.         Iterator i = rosterEntry.iterator();    
  40.         while (i.hasNext())    
  41.             EntriesList.add(i.next());    
  42.         return EntriesList;    
  43.     }    

这里注意下,与gtalk通讯,貌似gtalk是没有分组的,汗,所以使用第三个方法直接取

 

当然,还要处理,若是刚注册用户,一个组都没有的,需要默认两个组,我的好友及黑名单

黑名单的消息,一律杀掉,不会接受处理

2.       用户头像的获取

使用VCard,很强大,具体自己看API

可以看看VCard传回来XML的组成,含有很多信息的

  1. /**  
  2.      * 获取用户的vcard信息  
  3.      * @param connection  
  4.      * @param user  
  5.      * @return  
  6.      * @throws XMPPException  
  7.      */    
  8.     public static VCard getUserVCard(XMPPConnection connection, String user) throws XMPPException    
  9.     {    
  10.         VCard vcard = new VCard();    
  11.         vcard.load(connection, user);    
  12.             
  13.         return vcard;    
  14.     }    
  15.     
  16. 获取头像使用    
  17.     /**  
  18.      * 获取用户头像信息  
  19.      */    
  20.     public static ImageIcon getUserImage(XMPPConnection connection, String user) {    
  21.         ImageIcon ic = null;    
  22.         try {    
  23.             System.out.println("获取用户头像信息: "+user);    
  24.             VCard vcard = new VCard();    
  25.             vcard.load(connection, user);    
  26.                 
  27.             if(vcard == null || vcard.getAvatar() == null)    
  28.             {    
  29.                 return null;    
  30.             }    
  31.             ByteArrayInputStream bais = new ByteArrayInputStream(    
  32.                     vcard.getAvatar());    
  33.             Image image = ImageIO.read(bais);    
  34.         
  35.                 
  36.             ic = new ImageIcon(image);    
  37.             System.out.println("图片大小:"+ic.getIconHeight()+" "+ic.getIconWidth());    
  38.             
  39.         } catch (Exception e) {    
  40.             e.printStackTrace();    
  41.         }    
  42.         return ic;    
  43.     }    

3.       组操作和用户分组操作

主要是建立删除分组,用户添加到分组等操作

  1. /**  
  2.      * 添加一个组  
  3.      */    
  4.     public static boolean addGroup(Roster roster,String groupName)    
  5.     {    
  6.         try {    
  7.             roster.createGroup(groupName);    
  8.             return true;    
  9.         } catch (Exception e) {    
  10.             e.printStackTrace();    
  11.             return false;    
  12.         }    
  13.     }    
  14.         
  15.     /**  
  16.      * 删除一个组  
  17.      */    
  18.     public static boolean removeGroup(Roster roster,String groupName)    
  19.     {    
  20.         return false;    
  21.     }    
  22.         
  23.     /**  
  24.      * 添加一个好友  无分组  
  25.      */    
  26.     public static boolean addUser(Roster roster,String userName,String name)    
  27.     {    
  28.         try {    
  29.             roster.createEntry(userName, name, null);    
  30.             return true;    
  31.         } catch (Exception e) {    
  32.             e.printStackTrace();    
  33.             return false;    
  34.         }    
  35.     }    
  36.     /**  
  37.      * 添加一个好友到分组  
  38.      * @param roster  
  39.      * @param userName  
  40.      * @param name  
  41.      * @return  
  42.      */    
  43.     public static boolean addUser(Roster roster,String userName,String name,String groupName)    
  44.     {    
  45.         try {    
  46.             roster.createEntry(userName, name,new String[]{ groupName});    
  47.             return true;    
  48.         } catch (Exception e) {    
  49.             e.printStackTrace();    
  50.             return false;    
  51.         }    
  52.     }    
  53.         
  54.     /**  
  55.      * 删除一个好友  
  56.      * @param roster  
  57.      * @param userName  
  58.      * @return  
  59.      */    
  60.     public static boolean removeUser(Roster roster,String userName)    
  61.     {    
  62.         try {    
  63.                 
  64.             if(userName.contains("@"))    
  65.             {    
  66.                 userName = userName.split("@")[0];    
  67.             }    
  68.             RosterEntry entry = roster.getEntry(userName);    
  69.             System.out.println("删除好友:"+userName);    
  70.             System.out.println("User: "+(roster.getEntry(userName) == null));    
  71.             roster.removeEntry(entry);    
  72.                 
  73.             return true;    
  74.         } catch (Exception e) {    
  75.             e.printStackTrace();    
  76.             return false;    
  77.         }    
  78.             
  79.     }    

4.     用户查询

本来是用户操作的,分组和增删在3里讲了,这里主要是查询操作

查询用户

  1. public static List searchUsers(XMPPConnection connection,String serverDomain,String userName) throws XMPPException    
  2.     {    
  3.         List results = new ArrayList();    
  4.         System.out.println("查询开始..............."+connection.getHost()+connection.getServiceName());    
  5.             
  6.         UserSearchManager usm = new UserSearchManager(connection);    
  7.             
  8.             
  9.         Form searchForm = usm.getSearchForm(serverDomain);    
  10.         Form answerForm = searchForm.createAnswerForm();    
  11.         answerForm.setAnswer("Username"true);    
  12.         answerForm.setAnswer("search", userName);    
  13.         ReportedData data = usm.getSearchResults(answerForm, serverDomain);    
  14.              
  15.          Iterator it = data.getRows();    
  16.          Row row = null;    
  17.          UserBean user = null;    
  18.          while(it.hasNext())    
  19.          {    
  20.              user = new UserBean();    
  21.              row = it.next();    
  22.              user.setUserName(row.getValues("Username").next().toString());    
  23.              user.setName(row.getValues("Name").next().toString());    
  24.              user.setEmail(row.getValues("Email").next().toString());    
  25.              System.out.println(row.getValues("Username").next());    
  26.              System.out.println(row.getValues("Name").next());    
  27.              System.out.println(row.getValues("Email").next());    
  28.              results.add(user);    
  29.              //若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空    
  30.          }    
  31.              
  32.          return results;    
  33.     }    

以上查询貌似是多字段查询,即用户多个属性中某一个符合即作为查询结果

实际是可以实现根据某一特定字段查询的,如用户名,或昵称,这里笼统了,若需扩展去查看下API重写下

  

阅读(1082) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~