简体   繁体   English

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

[英]Getting list of all users registered on server with online/offline status using Smack API on android

Im connecting to an ejabberd XMPP server using 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();
            }

I want to get a list of all online users. 我想获取所有在线用户的列表。 How is it possible ? 这怎么可能 ?

When I try - 当我尝试-

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

}

Getting a null array for 获取一个空数组

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);

        }

check STATUS is equals to 'available' then the user is online otherwise user is Offline. 检查状态等于“可用”,则用户在线,否则用户离线。

User getPresence in Roaster as below. 如下所示,用户在RoastergetPresence

        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());
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM