1

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);
        }
kashif181
  • 315
  • 2
  • 11
  • Look at my answer at this post http://stackoverflow.com/questions/36471325/getting-all-users-from-an-openfire-server-using-smack-android/39868514#39868514 – Zain Ullah Muhammad Oct 05 '16 at 08:09

2 Answers2

2

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();
volatilevar
  • 1,626
  • 14
  • 16
0
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());
            }
        }
    });
Vijay Makwana
  • 506
  • 5
  • 14
  • Add some explanation with answer for how this answer help OP in fixing current issue – ρяσѕρєя K Jan 02 '17 at 12:46
  • This is because when you tried to access the roster entries it has nothing you have to add the listener to it. So it will notify you and you can access the entries of the roster when it is loaded with roster entries. – Vijay Makwana Jan 03 '17 at 07:51