How to create a roster in Smack ? I get the SmackExceptionNotLoggedunException Client is not logged in when I try to create a new roster .

My code is:

 Roster roster = Roster.getInstanceFor(connection); 

Farther:

  roster.createEntry(jid, name, groups); 

In my case it is:

  String [] groups ={"TestGroup"}; try { roster.createEntry("test2@irynas-macbook-air.local/Irynas-MacBook-Air", "test2@irynas-macbook-air.local", groups); } catch (SmackException.NotLoggedInException e) { e.printStackTrace(); } catch (SmackException.NoResponseException e) { e.printStackTrace(); } catch (XMPPException.XMPPErrorException e) { e.printStackTrace(); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } 

The first jid parameter is the id user who creates the roster ? Is the name of the roster or user? Where to take groups ? I just created an array of strings, I don’t know if this is correct, maybe I need to create something different?

  • You after all connect at connection precisely caused? - Suvitruf
  • @Suvitruf yes, connection = new XMPPTCPConnection (configBuilder.build ()); - Lucky_girl
  • I'm connection.login(...) about connection.login(...) or connection.connect(...) . - Suvitruf
  • @Suvitruf connection.connect (); connection.login (); - there is - Lucky_girl

1 answer 1

This is a working code consisting of declared variables and three methods.

1) We declare variables:

 private XMPPTCPConnectionConfiguration.Builder builder; private AbstractXMPPConnection connection; private Roster roster; 

2) Method for obtaining a builder:

 private XMPPTCPConnectionConfiguration.Builder getBuilder(String user, String password) { if (builder == null) { builder = XMPPTCPConnectionConfiguration.builder(); builder.setUsernameAndPassword(user, password); builder.setServiceName("irynas-macbook-air.local"); builder.setHost("irynas-macbook-air.local"); builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); builder.setResource("Irynas-MacBook-Air"); builder.setPort(5222); } return builder; } 

3) Method for obtaining a connection with authorization:

  public AbstractXMPPConnection getConnection(@NonNull String user, @NonNull String password) { if (connection == null) { connection = new XMPPTCPConnection(getBuilder(user, password).build()); connection.addConnectionListener(new ConnectionListener() { @Override public void connected(XMPPConnection connection) { Log.d("Custom", "ChatSystem.class -> connectionListener -> connected! " + connection.getHost()); } @Override public void authenticated(XMPPConnection connection, boolean resumed) { Log.d("Custom", "ChatSystem.class -> connectionListener -> authenticated"); } @Override public void connectionClosed() { Log.d("Custom", "ChatSystem.class -> connectionListener -> connectionClosed "); } @Override public void connectionClosedOnError(Exception e) { Log.d("Custom", "ChatSystem.class -> connectionListener -> connectionClosedOnError"); } @Override public void reconnectionSuccessful() { Log.d("Custom", "ChatSystem.class -> connectionListener -> reconnectionSuccessful"); } @Override public void reconnectingIn(int seconds) { Log.d("Custom", "ChatSystem.class -> connectionListener -> reconnectingIn"); } @Override public void reconnectionFailed(Exception e) { Log.d("Custom", "ChatSystem.class -> connectionListener -> reconnectionFailed"); } }); Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all); roster = Roster.getInstanceFor(connection); roster.setRosterLoadedAtLogin(true); roster.addRosterListener(this); roster.addRosterLoadedListener(this); } return connection; } 

4) Method for getting a list of users (roster):

 public Roster getRoster() { if (connection == null || roster == null) { return null; } roster = Roster.getInstanceFor(connection); return roster; } 

Hope this helps you. Good luck!

  • so in your example there is no creation of a new roster, there is only the receipt of an already existing roster. - Lucky_girl