powered by Jive Software

How to add a contact?

I’m having trouble adding a contact to the roster. I’ve tried two ways, and a combination of both, but somehow the contact gets removed from the roster immediately. Also, the contact does not receive a subscription request.

I tried this:

Presence presence = new Presence(Presence.Type.subscribe);
presence.setTo("username\\40hotmail.com@msn.mydomain.com");
presence.setFrom(conn.getUser());
conn.sendPacket(presence);

and this:

conn.getRoster().createEntry("username\\40hotmail.com@msn.mydomain.com","testname",new String[]{"Test group"});

and both, but it just doesn’t work. Googling doesn’t really help much.

What am I missing?

Hm, I think it should work. However, the “to” username is not a JID (but MSN, right?), and your conn.getUser() can return null, you need to check that (it might not be necessary to set the “from” field explicitly).

Thanks for your reply. I checked, getUser() returns the correct string. Removing the setFrom() doesn’t change anything either.

If the JID is wrong, how should I add a MSN user to the roster? I tried username%hotmail.com@msn.domain.com, but that doesn’t work. BTW, I think the contact does get added to the roster, but it’s removed immediately.

I don’t exactly know what I did wrong first, but this is how I have it working now:

//name is in the format of username\40domain.tld@msn.mydomain.tld     private void addUser(String name){
        RosterPacket.Item rosterItem = new RosterPacket.Item(name,name);         RosterPacket rosterPacket = new RosterPacket();
        rosterPacket.setType(IQ.Type.SET);
        rosterPacket.addRosterItem(rosterItem);
        rosterPacket.toXML();                conn.sendPacket(rosterPacket);         Presence presence = new Presence(Presence.Type.subscribe);
        presence.setTo(name);
        presence.setFrom(conn.getUser());         conn.sendPacket(presence);
    }