Get Roster List From XMPP IM with Smack API in Java Application

In previous post we have learned how to login using smack api . In this tutorial we will java xmpp client example for get roster list From xmpp server with Smack API using java . Before start this tutorial you need openfire server setupĀ . If you do not have idea about openfie , then read first how to setup openfire xmpp server . Now we will see how to get roster list of login user .

Smack Java Client Example

We will login using username and password in XMPP server after that we will retrieve roster list of user with the help of getRoster() method .

import java.util.Collection;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

public class GETRoster {
public static void main(String[] args) {
GETRoster getRoster = new GETRoster();
XMPPConnection connection = getRoster.Connect();

// Login User
getRoster.loginUser(connection);

// Get Roster Of Login User
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
System.out.println("name=" + entry.getName() + "userName="
+ entry.getUser());
}
connection.disconnect();
}

public void loginUser(XMPPConnection connection){
try {
connection.login("userName","Password");
} catch (XMPPException e) {

e.printStackTrace();
}
}

/**
*
* @return XMPP Connection
*/

public XMPPConnection Connect() {

ConnectionConfiguration config = new ConnectionConfiguration(
"localhost", 5222);

/*
* ConnectionConfiguration config = new ConnectionConfiguration(
* "192.163.2.200", 5222);
*/
XMPPConnection connection = new XMPPConnection(config);
try {
connection.connect();
} catch (XMPPException e) {

e.printStackTrace();
}
return connection;
}

}

We have discussed how to get roster list of user in XMPP server . if you are looking xmpp smack-android tutorial , then openfire tutorial is helpful for you. This tutorial have example for spark , openfire and smack api.

Leave a Reply

Your email address will not be published. Required fields are marked *

− 3 = 3