美文网首页
XMPP系列之Smack(4.1.3)(二)获取好友分组

XMPP系列之Smack(4.1.3)(二)获取好友分组

作者: 猿田 | 来源:发表于2017-01-13 16:35 被阅读526次

    XMPP系列之Smack(4.1.3)(一)登录服务器
    XMPP系列之Smack(4.1.3 )(三)获取已加入的聊天室列表
    XMPP系列之Smack(4.1.3 )(四)创建聊天室
    接着上一篇,要实现类似QQ好友分组的功能很简单,从服务器读取到好友分组以及各个分组内的成员。
    首先还是要通过连接管理器单例来获得连接类,前一章已经讲解了如何配置并初始化XMPPTCPConnection,在这里要说明一下如何创建这个单例管理类,我们通过加双重锁的方式来创建这个类,方法如下

    public static XMPPConnectionManager getInstance() {   
         if (connectionManager == null) {        
              synchronized (XMPPConnectionManager.class) {
                if (connectionManager == null) {
                    connectionManager = new XMPPConnectionManager();
                }                           
              }   
       }
        return connectionManager;
    }
    

    这样可以保证在加锁的情况下防止对象为空。

    通过管理类获取连接对象

    public XMPPTCPConnection getConnection() {
        if (connection == null) {
            throw new RuntimeException("请先初始化连接");
        }
        return connection;
    }
    

    服务器上所有的注册用户都通过Roster来获得,获取名单对象Roster的方法和之前有所差异,之前是通过new一个Roster对象,现在通过一个Roster单例获取

    public static synchronized Roster getInstanceFor(XMPPConnection connection) {
        Roster roster = INSTANCES.get(connection);
        if (roster == null) {
            roster = new Roster(connection);        
            INSTANCES.put(connection, roster);
        }
        return roster;
    }
    

    我们直接注入一个连接对象即可

    Roster roster = Roster.getInstanceFor(connection);
    

    如果有延迟还可以来一个判断条件

    if (!roster.isLoaded()) {
        try {
            roster.reloadAndWait();
        } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException e) {
            e.printStackTrace();
        }
    }
    

    有了名单对象后就可以获取所有的分组以及所有的好友了,首先获取服务器上创建的所有分组:

    Collection<RosterGroup> rosterEntries = roster.getGroups();
    

    接下来通过遍历来取得我们需要的Group对象

    for (RosterGroup rosterGroup : rosterEntries) {
        String groupName = rosterGroup.getName();
        int count = rosterGroup.getEntryCount();
        FriendGroup mainGroup = new FriendGroup();
        mainGroup.setName(groupName);
        mainGroup.setCount(String.valueOf(count));
        groupList.add(mainGroup);
    

    这里我创建了一个model(FriendGroup)来取得我所需要的组名和组内的好友数量,然后通过rosterGroup获取到组内的好友

    List<RosterEntry> rosterEntryList = rosterGroup.getEntries();
    List<FriendEntity> tempChildList = new ArrayList<>();
    for (int i = 0; i < rosterEntryList.size(); i++) {
        RosterEntry rosterEntry = rosterEntryList.get(i);
        FriendEntity mainChild = new FriendEntity();
        Presence presence = roster.getPresence(rosterEntry.getUser());
        Log.e("状态", "presence=" + presence);
        Log.e("状态", "presence status=" + presence.getStatus());
        Log.e("状态", "presence mode=" + presence.getMode());
        if (presence.isAvailable()) {
            mainChild.setPresence("[在线]");
        } else {
            mainChild.setPresence("[离线]");
        }
        mainChild.setName(rosterEntry.getName());
        mainChild.setJid(rosterEntry.getUser());
        tempChildList.add(mainChild);
    }
    childList.add(tempChildList);
    

    获取好友在线状态是通过获取到的Presence对象来判断的,下面是smack源码描述:

    Paste_Image.png

    到此获取分组及好友就搞定了,如何显示大家各显神通吧!
    下一章会讲述如何获取聊天室也就是QQ的群列表!再见!!!

    相关文章

      网友评论

          本文标题:XMPP系列之Smack(4.1.3)(二)获取好友分组

          本文链接:https://www.haomeiwen.com/subject/rmdsbttx.html