Smack[3]用户列表,头像,组操作,用户操作

    1. 用户列表

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

    connection.getRoster();

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

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

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

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

    1. 用户头像的获取

    使用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.     }  
      
    1. 组操作和用户分组操作

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

    [c-sharp] view plaincopyprint?
     
    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.     }  
      
    1. 用户查询

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

    查询用户

    1. public static List<UserBean> searchUsers(XMPPConnection connection,String serverDomain,String userName) throws XMPPException  
    2.     {  
    3.         List<UserBean> results = new ArrayList<UserBean>();  
    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<Row> 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.     }  
原文地址:https://www.cnblogs.com/jiayonghua/p/3765907.html