1
votes

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

2 Answers

2
votes

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