tags:

views:

928

answers:

3

Hi all,

I am new to smack API. I am trying to develop a chat application where I was trying for setting and getting the presence.

When I change the presence of a user, its working perfectly fine and it is getting reflected in the Openfire Server.

But when I tries to get the Presence of a user, I am always getting the status as 'unavailable' even if his presence in openfire is showing as 'available'.

I am using the following code to set the status.

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

I am using the Roster class to get the presence as follows.

Roster roster = avatar.getRoster(); Collection 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 No 1 alwasys gives 'unavailable' while line number 2 and 3 always give null

I am not able to figure out the cause of this problem. Please help me to resolve this issue.

Thanks in advance.

A: 

the problem is that after logging in immediately, it is gonna take some time for the presence of users to get updated.So between logging in and calling the online buddies function there should be a thread.sleep() for a few seconds.Then the online contacts will be retrieved. I did that and was able to retrieve them. after login use

Thread.sleep(5000);

use in the beginiing of the method also

ravi
A: 

Hello,

I had the same problem and searched for a while before finding what the problem was. In fact, you don't need to do a Thread.sleep(). The problem is that you don't have the "permission" to get the Presence of other users.

To solve the problem, just go in Openfire admin -> your user options -> Roster // Then just set the subscription of the buddy you wanna get the presence to "both" (both users can view each other presence).

Hope that is helps.

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

Towser
A: 

In my code one user receives other users presence type as error. Cant anyone help me with this one ?

http://stackoverflow.com/questions/3279057/problem-adding-buddy-with-smack-api-and-openfire-server

This is the link of my code . Any help would be greatly appreciated. Thank You.

kalkin