Saturday, 4 April 2015

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

package com.javaproficiency.demo;

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;
    }

}

No comments:

Post a Comment