`

XMPP——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是没有分组的,汗,所以使用第三个方法直接取

 

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

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

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

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

 

 

 

[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.     }  

  

 

 

4.     用户查询

本来是用户操作的,分组和增删在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.     }  

 

 

 

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

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

 1.       修改自身状态

包括上线,隐身,对某人隐身,对某人上线

 

  1. public static void updateStateToAvailable(XMPPConnection connection)  
  2.     {  
  3.         Presence presence = new Presence(Presence.Type.available);  
  4.         connection.sendPacket(presence);  
  5.      }  
  6.       
  7.     public static void updateStateToUnAvailable(XMPPConnection connection)  
  8.     {  
  9.         Presence presence = new Presence(Presence.Type.unavailable);  
  10.         connection.sendPacket(presence);  
  11.         }  
  12.       
  13.     public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)  
  14.     {  
  15.         Presence presence = new Presence(Presence.Type.unavailable);  
  16.         presence.setTo(userName);  
  17.         connection.sendPacket(presence);  
  18.     }  
  19.     public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)  
  20.     {  
  21.         Presence presence = new Presence(Presence.Type.available);  
  22.         presence.setTo(userName);  
  23.         connection.sendPacket(presence);  
  24.   
  25.     }  

 

 

2.       心情修改

 

  

  1. /** 
  2.      * 修改心情 
  3.      * @param connection 
  4.      * @param status 
  5.      */  
  6.     public static void changeStateMessage(XMPPConnection connection,String status)  
  7.     {  
  8.         Presence presence = new Presence(Presence.Type.available);  
  9.         presence.setStatus(status);  
  10.         connection.sendPacket(presence);  
  11.       
  12.     }  

 

 

3.       修改用户头像

有点麻烦,主要是读入图片文件,编码,传输之

 

 

  1. public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException  
  2.     {  
  3.       
  4.         VCard vcard = new VCard();  
  5.         vcard.load(connection);  
  6.           
  7.             byte[] bytes;  
  8.             
  9.                 bytes = getFileBytes(f);  
  10.                 String encodedImage = StringUtils.encodeBase64(bytes);  
  11.                 vcard.setAvatar(bytes, encodedImage);  
  12.                 vcard.setEncodedImage(encodedImage);  
  13.                 vcard.setField("PHOTO""<TYPE>image/jpg</TYPE><BINVAL>"  
  14.                         + encodedImage + "</BINVAL>"true);  
  15.                   
  16.                   
  17.                 ByteArrayInputStream bais = new ByteArrayInputStream(  
  18.                         vcard.getAvatar());  
  19.                 Image image = ImageIO.read(bais);  
  20.                 ImageIcon ic = new ImageIcon(image);  
  21.                    
  22.              
  23.             
  24.             vcard.save(connection);  
  25.              
  26.     }  
  27.       
  28.     private static byte[] getFileBytes(File file) throws IOException {  
  29.         BufferedInputStream bis = null;  
  30.         try {  
  31.             bis = new BufferedInputStream(new FileInputStream(file));  
  32.             int bytes = (int) file.length();  
  33.             byte[] buffer = new byte[bytes];  
  34.             int readBytes = bis.read(buffer);  
  35.             if (readBytes != buffer.length) {  
  36.                 throw new IOException("Entire file not read");  
  37.             }  
  38.             return buffer;  
  39.         } finally {  
  40.             if (bis != null) {  
  41.                 bis.close();  
  42.             }  
  43.         }  
  44. }  

 

 

4.  补充,用户状态的监听

 

即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有smack实现的监听器

 

 

 

 

 

 

 

 

  1. final Roster roster = Client.getRoster();  
  2.           
  3.         roster.addRosterListener(  
  4.                 new RosterListener() {  
  5.   
  6.                     @Override  
  7.                     public void entriesAdded(Collection<String> arg0) {  
  8.                         // TODO Auto-generated method stub  
  9.                         System.out.println("--------EE:"+"entriesAdded");  
  10.                     }  
  11.   
  12.                     @Override  
  13.                     public void entriesDeleted(Collection<String> arg0) {  
  14.                         // TODO Auto-generated method stub  
  15.                         System.out.println("--------EE:"+"entriesDeleted");  
  16.                     }  
  17.   
  18.                     @Override  
  19.                     public void entriesUpdated(Collection<String> arg0) {  
  20.                         // TODO Auto-generated method stub  
  21.                         System.out.println("--------EE:"+"entriesUpdated");  
  22.                     }  
  23.   
  24.                     @Override  
  25.                     public void presenceChanged(Presence arg0) {  
  26.                         // TODO Auto-generated method stub  
  27.                         System.out.println("--------EE:"+"presenceChanged");  
  28.                     }     
  29.                       
  30.                 });  
  31.               

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics