In last article we have learn how to login on xmpp server using smack api . Now In this article we will learn how to add friends( roster ) in buddy using smack api.For add roster in xmpp first you should make two account. See It how to resister on xmpp server if you do not know how to register user on xmpp server. after that you can send roster request using below code .
package com.javaproficiency.demo;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
public class AddRoster {
public static void main(String[] args) {
AddRoster addRoster = new AddRoster();
XMPPConnection connection = addRoster.Connect();
try {
// user yogesh login on xmpp server
connection.login(“yogesh@www.javaproficiency.com”,”123″);
System.out.println(“Login”);
Roster roster = connection.getRoster();
// yogesh send roster request to shivam
roster.createEntry(“shivam@www.javaproficiency.com”,”myking”,null);
System.out.println(” send roster request”);
connection.disconnect();
} catch (XMPPException e) {
e.printStackTrace();
}
}
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;
}
}
I am a regular reader of your blog. the blog is very interesting and will be much useful for us.
SEO Company in Chennai
Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.
SEO Company in Chennai