Smack[3]用户列表,头像,组操作,用户操作
- 用户列表
Smack主要使用Roster进行列表管理的
connection.getRoster();
- /**
- * 返回所有组信息 <RosterGroup>
- *
- * @return List(RosterGroup)
- */
- public static List<RosterGroup> getGroups(Roster roster) {
- List<RosterGroup> groupsList = new ArrayList<RosterGroup>();
- Collection<RosterGroup> rosterGroup = roster.getGroups();
- Iterator<RosterGroup> i = rosterGroup.iterator();
- while (i.hasNext())
- groupsList.add(i.next());
- return groupsList;
- }
- /**
- * 返回相应(groupName)组里的所有用户<RosterEntry>
- *
- * @return List(RosterEntry)
- */
- public static List<RosterEntry> getEntriesByGroup(Roster roster,
- String groupName) {
- List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
- RosterGroup rosterGroup = roster.getGroup(groupName);
- Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
- Iterator<RosterEntry> i = rosterEntry.iterator();
- while (i.hasNext())
- EntriesList.add(i.next());
- return EntriesList;
- }
- /**
- * 返回所有用户信息 <RosterEntry>
- *
- * @return List(RosterEntry)
- */
- public static List<RosterEntry> getAllEntries(Roster roster) {
- List<RosterEntry> EntriesList = new ArrayList<RosterEntry>();
- Collection<RosterEntry> rosterEntry = roster.getEntries();
- Iterator<RosterEntry> i = rosterEntry.iterator();
- while (i.hasNext())
- EntriesList.add(i.next());
- return EntriesList;
- }
这里注意下,与gtalk通讯,貌似gtalk是没有分组的,汗,所以使用第三个方法直接取
当然,还要处理,若是刚注册用户,一个组都没有的,需要默认两个组,我的好友及黑名单
黑名单的消息,一律杀掉,不会接受处理
- 用户头像的获取
使用VCard,很强大,具体自己看API吧
可以看看VCard传回来XML的组成,含有很多信息的
- /**
- * 获取用户的vcard信息
- * @param connection
- * @param user
- * @return
- * @throws XMPPException
- */
- public static VCard getUserVCard(XMPPConnection connection, String user) throws XMPPException
- {
- VCard vcard = new VCard();
- vcard.load(connection, user);
- return vcard;
- }
- 获取头像使用
- /**
- * 获取用户头像信息
- */
- public static ImageIcon getUserImage(XMPPConnection connection, String user) {
- ImageIcon ic = null;
- try {
- System.out.println("获取用户头像信息: "+user);
- VCard vcard = new VCard();
- vcard.load(connection, user);
- if(vcard == null || vcard.getAvatar() == null)
- {
- return null;
- }
- ByteArrayInputStream bais = new ByteArrayInputStream(
- vcard.getAvatar());
- Image image = ImageIO.read(bais);
- ic = new ImageIcon(image);
- System.out.println("图片大小:"+ic.getIconHeight()+" "+ic.getIconWidth());
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ic;
- }
- 组操作和用户分组操作
主要是建立删除分组,用户添加到分组等操作
- /**
- * 添加一个组
- */
- public static boolean addGroup(Roster roster,String groupName)
- {
- try {
- roster.createGroup(groupName);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 删除一个组
- */
- public static boolean removeGroup(Roster roster,String groupName)
- {
- return false;
- }
- /**
- * 添加一个好友 无分组
- */
- public static boolean addUser(Roster roster,String userName,String name)
- {
- try {
- roster.createEntry(userName, name, null);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 添加一个好友到分组
- * @param roster
- * @param userName
- * @param name
- * @return
- */
- public static boolean addUser(Roster roster,String userName,String name,String groupName)
- {
- try {
- roster.createEntry(userName, name,new String[]{ groupName});
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- /**
- * 删除一个好友
- * @param roster
- * @param userName
- * @return
- */
- public static boolean removeUser(Roster roster,String userName)
- {
- try {
- if(userName.contains("@"))
- {
- userName = userName.split("@")[0];
- }
- RosterEntry entry = roster.getEntry(userName);
- System.out.println("删除好友:"+userName);
- System.out.println("User: "+(roster.getEntry(userName) == null));
- roster.removeEntry(entry);
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- 用户查询
本来是用户操作的,分组和增删在3里讲了,这里主要是查询操作
查询用户
- public static List<UserBean> searchUsers(XMPPConnection connection,String serverDomain,String userName) throws XMPPException
- {
- List<UserBean> results = new ArrayList<UserBean>();
- System.out.println("查询开始..............."+connection.getHost()+connection.getServiceName());
- UserSearchManager usm = new UserSearchManager(connection);
- Form searchForm = usm.getSearchForm(serverDomain);
- Form answerForm = searchForm.createAnswerForm();
- answerForm.setAnswer("Username", true);
- answerForm.setAnswer("search", userName);
- ReportedData data = usm.getSearchResults(answerForm, serverDomain);
- Iterator<Row> it = data.getRows();
- Row row = null;
- UserBean user = null;
- while(it.hasNext())
- {
- user = new UserBean();
- row = it.next();
- user.setUserName(row.getValues("Username").next().toString());
- user.setName(row.getValues("Name").next().toString());
- user.setEmail(row.getValues("Email").next().toString());
- System.out.println(row.getValues("Username").next());
- System.out.println(row.getValues("Name").next());
- System.out.println(row.getValues("Email").next());
- results.add(user);
- //若存在,则有返回,UserName一定非空,其他两个若是有设,一定非空
- }
- return results;
- }