Possible bug in XMPPConnection

Sorry for my english is very poor. I use Smack 4.1.3

public class RosterGetInstanceFor {

protected static ConnectionListener connectionListener = new ConnectionListener() {

@Override

public void authenticated(XMPPConnection connection, boolean arg1) {

if (connection != null && connection.isAuthenticated()) {

Roster roster = Roster.getInstanceFor(connection);

System.out.println(“roster.getGroups().size() =”

  • roster.getGroups().size());

System.out.println(“roster.getEntries().size()=”

  • roster.getEntries().size());

}

}

@Override

public void connected(XMPPConnection arg0) {

// TODO Auto-generated method stub

}

@Override

public void connectionClosed() {

// TODO Auto-generated method stub

}

@Override

public void connectionClosedOnError(Exception arg0) {

// TODO Auto-generated method stub

}

@Override

public void reconnectingIn(int arg0) {

// TODO Auto-generated method stub

}

@Override

public void reconnectionFailed(Exception arg0) {

// TODO Auto-generated method stub

}

@Override

public void reconnectionSuccessful() {

// TODO Auto-generated method stub

}

};

public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException {

XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration

.builder();

builder.setUsernameAndPassword(“reader”,“reader”);

builder.setServiceName(“wpw-pc”);

builder.setHost(“wpw-pc”);

builder.setCompressionEnabled(false);

builder.setResource(“Hyperbola”);

builder.setSecurityMode(SecurityMode.disabled);

AbstractXMPPConnection connection = new XMPPTCPConnection(builder.build());

connection.addConnectionListener(connectionListener);

connection.connect();

connection.login();

Thread.sleep(3000); // must

Roster roster = Roster.getInstanceFor(connection);

System.out.println(“roster.getGroups().size() =”

  • roster.getGroups().size());

System.out.println(“roster.getEntries().size()=”

  • roster.getEntries().size());

Thread.sleep(5000);

connection.disconnect();

}

I run the above code,output is:

roster.getGroups().size() =0

roster.getEntries().size()=0

roster.getGroups().size() =2

roster.getEntries().size()=4

How can I get groups and entries in function authenticated?

Also, I must add the code : Thread.sleep(3000)

If I remove this line ,output is :

roster.getGroups().size()=0

roster.getEntries().size()=0

roster.getGroups().size()=0

roster.getEntries().size()=0

Thanks and Regards,

You may want to add a “addRosterLoadedListener”.