2
votes

I am creating a java fx application for openfire chat client. i am using smack 4.1 rc1 to connect to the server. i am able to to connect to server send presence information to others and send messages to other users as well. however i am not able to iterate through the roster.

when i get roster object and debug it its shows a hash map of 3 roster entries that means the roster is getting loaded in roster object. however when i use roster.getentries method to store it into the Collection of roster entries it shows 0 object. even the roster.getentriescount() method returns 0 though i can see the roster user names in the debug view

try {
    config = XMPPTCPConnectionConfiguration.builder()
            .setUsernameAndPassword(mUserName+ "@" + Domain, mPassword)
            .setServiceName(HostName)
            .setHost(HostName)
            .setPort(PortName)
            .setResource(Resource)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .build();
    mXmppConnection = new XMPPTCPConnection(config);

    mXmppConnection.connect();
    mXmppConnection.login();
   // Presence presence=new Presence();
    Presence presence ;
    if(mPresence) presence = new Presence(Presence.Type.available);
    else presence = new Presence(Presence.Type.unavailable);
    presence.setStatus("On Smack");
    XMPPConnection conn=(XMPPConnection) mXmppConnection;
    Chat chat = ChatManager.getInstanceFor(mXmppConnection).createChat
        ("monika@ipaddress");
    chat.sendMessage("Howdy from smack!");

    // Send the packet (assume we have a XMPPConnection instance called "con").
    mXmppConnection.sendPacket(presence);

    System.out.println("Connected successfully");
    Roster roster = Roster.getInstanceFor(conn);
    Collection<RosterEntry> entries = roster.getEntries();

    int i=0;
    for (RosterEntry entry : entries) {
        System.out.println(entry);
                i++;
    }
    System.out.println("Rosters Count - "+ i+ roster.getEntryCount());

has any one encountered the same problem before?

3
Is not see how you set connection parameters, maybe here is the problem. However, take a look to stackoverflow.com/questions/11214684/smack-presence-doesnt-work.Mihai8

3 Answers

3
votes

You may have to check if roster is loaded before calling getEntries.

Roster roster = Roster.getInstanceFor(connection);

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

Collection <RosterEntry> entries = roster.getEntries();
1
votes

thanks for Deepak Azad

Here full code

public void getRoaster(final Callback<Collection<RosterEntry>> callback) {
    final Roster roster = Roster.getInstanceFor(connection);
    if (!roster.isLoaded())
    try {
        roster.reloadAndWait();
    } catch (SmackException.NotLoggedInException | SmackException.NotConnectedException | InterruptedException e) {
        e.printStackTrace();
    }

    Collection<RosterEntry> entries = roster.getEntries();
    for (RosterEntry entry : entries) {
        android.util.Log.d(AppConstant.PUBLIC_TAG, entry.getName());
    }
}
0
votes

I have just solved this problem. I'm using OpenFire as XMPP server. I checked the field "Subscription" in the users in roster and it was "None". After change it by "Both", it worked, and entries are being fetched.

Hope it helps!