簡體   English   中英

使用Android上的Smack API獲取具有在線/離線狀態的服務器上注冊的所有用戶的列表

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

我使用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);

        }

檢查狀態等於“可用”,則用戶在線,否則用戶離線。

如下所示,用戶在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