在Android上使用Smack API获取在服务器上注册的具有在线/离线状态的所有用户的列表



我使用 Smack API 连接到 ejabberd XMPP 服务器 -

        username = "admin";
        password = "admin";
        port = "5222";
        serviceName = "davids-macbook-pro.local";
        host = "192.168.20.8";
     config = XMPPTCPConnectionConfiguration.builder()
                    .setUsernameAndPassword(username, password)
                    .setHost(host)
                    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                    .setServiceName(serviceName)
                    .setPort(Integer.parseInt(port))
                    .build();
            connection = new XMPPTCPConnection(config);
            try {
                connection.connect();
                connection.login();
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }

我想获取所有在线用户的列表。怎么可能?

当我尝试时——

Roster roster = Roster.getInstanceFor(connection);
Collection<RosterEntry> entries = roster.getEntries();
Log.i("entry", entries+"");
for (RosterEntry entry : entries) {
}

获取 的空数组

Collection<RosterEntry> entries = roster.getEntries();
     for (RosterEntry entry : entries) {
                HashMap<String, String> map = new HashMap<String,String>();
                 Presence entryPresence = roster.getPresence(entry.getUser());
                Presence.Type type = entryPresence.getType();       
                map.put("USER", entry.getName().toString());
                map.put("STATUS", type.toString());
                Log.e("USER", entry.getName().toString());
                usersList.add(map);
        }

检查状态等于"可用",则用户处于联机状态,否则用户处于脱机状态。

用户getPresence Roaster如下所示。

        Roster roster = Roster.getInstanceFor(connection);
        Collection<RosterEntry> entries = roster.getEntries();
        for (RosterEntry rosterEntry : entries){
            System.out.println("xmpp.Roster.presence "+ roster.getPresence(rosterEntry.getUser()).getType());
        }

最新更新