Failed to get registry list using smack, openfire

I am new to smack API. I am trying to develop a chat application where I tried to install and get a presence.

When I change the user’s presence, his work is beautiful, and he is reflected on the Openfire server.

But when I try to get a User Presence, I always get the status "inaccessible", even if his presence in openfire is displayed as "available".

I use the following code to set the state.

Presence presence = new Presence(Presence.Type.available); presence.setStatus("Online, Programmatically!"); presence.setPriority(24); presence.setMode(Presence.Mode.available); user.getConnection().sendPacket(presence); 

I use the Roster class to get the following:

 Roster roster = avatar.getRoster(); Collection<RosterEntry> entries = roster.getEntries(); for(RosterEntry rosterEntry: entries) { String user = rosterEntry.getUser(); Presence presence = roster.getPresence(user); System.out.println("Presence : "+presence); // 1 System.out.println("Presence type: "+presence.getType()); // 2 System.out.println("Presence mode: "+presence.getMode()); // 3 } 

Line number 1 alwasys gives "unavailable", and lines number 2 and 3 always give zero

I can not understand the cause of this problem. Please help me solve this problem.

Thanks in advance.

+6
smack openfire
source share
5 answers

Using RosterListener is the right solution to this problem. There is no reason code should have Thread.sleep() for it to work correctly.

 Roster roster = con.getRoster(); roster.addRosterListener(new RosterListener() { // Ignored events public void entriesAdded(Collection<String> addresses) {} public void entriesDeleted(Collection<String> addresses) {} public void entriesUpdated(Collection<String> addresses) {} public void presenceChanged(Presence presence) { System.out.println("Presence changed: " + presence.getFrom() + " " + presence); } }); 

(source: http://www.igniterealtime.org/builds/smack/docs/latest/documentation/roster.html )

+8
source share

the problem is that after logging in, it will immediately take some time for users to be updated. Meanwhile, in order to log into the system and call the online buddy function, within a few seconds there should be thread.sleep (). Then online contacts will be restored. I did this and was able to restore them. after logging in

Thread.sleep (5000);

use at the beginning of the method also

+7
source share

I had the same problem and searched for a while before finding what the problem was. In fact, you do not need to do Thread.sleep (). The problem is that you do not have “permission” to receive the presence of other users.

To solve this problem, just go to Openfire admin → your user settings → Roster // Then just install the subscription to the person you want to receive for “both” (both users can view each other).

Hope this helps.

Edit: In fact, you need to add Thread.sleep () before getting the list from the connection. Without Thread.sleep () sometimes it works, sometimes not ...

+3
source share

I fixed it by adding:

  if (!roster.isLoaded()) roster.reloadAndWait(); 

after

  Roster roster = Roster.getInstanceFor(connection); 

Link: Smack 4.1.0. Android manufacturers list not showing

+1
source share

This full code

  public void getRoaster(final Callback<List<HashMap<String, String>>> callback) { final Roster roster = Roster.getInstanceFor(connection); boolean success = true; if (!roster.isLoaded()) try { roster.reloadAndWait(); } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException | InterruptedException e) { android.util.Log.e(AppConstant.PUBLIC_TAG, TAG + " " + e.getMessage()); success = false; } if (!success) { if (callback != null) { callback.onError(new Throwable()); } } Collection<RosterEntry> entries = roster.getEntries(); List<HashMap<String, String>> maps = new ArrayList<HashMap<String, String>>(entries.size()); for (RosterEntry entry : entries) { HashMap<String, String> map = new HashMap<String, String>(3); Presence presence = roster.getPresence(entry.getUser()); map.put(ROASTER_KEY, entry.getName()); map.put(ROASTER_BARE_JID, entry.getUser()); map.put(PRESENCE_TYPE, presence.isAvailable() == true ? PRESENCE_ONLINE : PRESENCE_OFFLINE); maps.add(map); } if (maps != null && maps.size() > 0 && callback != null) { callback.onSuccess(maps); } else { callback.onError(new Throwable()); } } 
0
source share

All Articles