简体   繁体   中英

How to get all user on openfire xmpp server using smack on android?

I am developing chat application in android using smack and openfire, now i am facing problem to show the list of all users so that these users can be invited for chat, i tried with roster using below code but roster entries size is always zero. While i am properly logged in. Following is my code snippet.

Roster roster =Roster.getInstanceFor(xmppConnection);
        Collection<RosterEntry> entries = roster.getEntries();
        for (RosterEntry entry : entries) {
            System.out.println(entry);
        }

This is because the roster takes a while to be loaded. You can:

  1. Use a RosterLoadedListener .
  2. Reload and wait: if (!roster.isLoaded()) roster.reloadAndWait();
Roster roster = Roster.getInstanceFor(connection_obj);
    roster.addRosterLoadedListener(new RosterLoadedListener() {
        @Override
        public void onRosterLoaded(Roster roster) {
            Set<RosterEntry> entries = roster.getEntries();
            for (RosterEntry entry : entries) {
                Log.d("XMPPChatDemoActivity", "Name: "+entry.getName());
                Log.d("XMPPChatDemoActivity", "Id: " + entry.getUser());
                Presence entryPresence =roster.getPresence(entry.getUser());
                Log.d("XMPPChatDemoActivity", "Presence Status: " +        entryPresence.getStatus());
                Log.d("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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