转载:http://blog.csdn.net/wklken/archive/2011/06/01/6460112.aspx
上一篇主要是会话的管理
继续,这是显示用户列表方面的
1. 用户列表
Smack主要使用Roster进行列表管理的
connection.getRoster();
-
-
-
-
-
- public static List getGroups(Roster roster) {
- List groupsList = new ArrayList();
- Collection rosterGroup = roster.getGroups();
- Iterator i = rosterGroup.iterator();
- while (i.hasNext())
- groupsList.add(i.next());
- return groupsList;
- }
-
-
-
-
-
-
- public static List getEntriesByGroup(Roster roster,
- String groupName) {
- List EntriesList = new ArrayList();
- RosterGroup rosterGroup = roster.getGroup(groupName);
- Collection rosterEntry = rosterGroup.getEntries();
- Iterator i = rosterEntry.iterator();
- while (i.hasNext())
- EntriesList.add(i.next());
- return EntriesList;
- }
-
-
-
-
-
-
- public static List getAllEntries(Roster roster) {
- List EntriesList = new ArrayList();
- Collection rosterEntry = roster.getEntries();
- Iterator i = rosterEntry.iterator();
- while (i.hasNext())
- EntriesList.add(i.next());
- return EntriesList;
- }
这里注意下,与gtalk通讯,貌似gtalk是没有分组的,汗,所以使用第三个方法直接取
当然,还要处理,若是刚注册用户,一个组都没有的,需要默认两个组,我的好友及黑名单
黑名单的消息,一律杀掉,不会接受处理
2. 用户头像的获取
使用VCard,很强大,具体自己看API吧
可以看看VCard传回来XML的组成,含有很多信息的
-
-
-
-
-
-
-
- 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;
- }
3. 组操作和用户分组操作
主要是建立删除分组,用户添加到分组等操作
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
-
- 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;
- }
-
- }
4. 用户查询
本来是用户操作的,分组和增删在3里讲了,这里主要是查询操作
查询用户
- public static List searchUsers(XMPPConnection connection,String serverDomain,String userName) throws XMPPException
- {
- List results = new ArrayList();
- 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
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);
-
- }
-
- return results;
- }
以上查询貌似是多字段查询,即用户多个属性中某一个符合即作为查询结果
实际是可以实现根据某一特定字段查询的,如用户名,或昵称,这里笼统了,若需扩展去查看下API重写下
阅读(1144) | 评论(0) | 转发(0) |