How to add a friend

I wonder how to confirm a request of adding friends.

As I know now ,

roster = Roster.getInstanceFor(con);


roster.createEntry(targetUser, nickname, new String[] {“friends”});

will create a roster in the database, and send a stanza request to the targetUser.

And my stanza listener is as follows:

StanzaListener callback = new StanzaListener() {


public void processPacket(Stanza stanza) throws NotConnectedException {

// TODO Auto-generated method stub

String head = con.getUser() + " : ";

System.out.println(head + “Got the presence stanza”);

Presence subscribed = new Presence(Presence.Type.subscribed);


System.out.println(head + subscribed.getFrom());



System.out.println(head + “Subscribed sent”);

if( ! roster.contains(stanza.getFrom())) {

System.out.println(head + “Friend added.”);



} else {

System.out.println(head + "Subscribed received from " + stanza.getFrom());

if(roster.contains(stanza.getFrom())) {

System.out.println(head + “OK”);



System.out.println(head + “-------------------------”);



It does not work, I wonder when the targetUser receives the stanza request , how to send a callback presence(or something else) to confirm or approve or agree?

How to add a friend?