1

I wonder how to confirm a request of adding friends.

Smack Version 4.1.1

As I know now ,

roster = Roster.getInstanceFor(con);
roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
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() {
        @Override
        public void processPacket(Stanza stanza) throws NotConnectedException {
            // TODO Auto-generated method stub
            String head = con.getUser() + " : ";
            System.out.println(head + "Got the presence stanza");
            if(((Presence)stanza).getType().equals(Presence.Type.subscribe)) {
                Presence subscribed = new Presence(Presence.Type.subscribed);
                subscribed.setTo(stanza.getFrom());
                System.out.println(head + subscribed.getFrom());
                subscribed.setFrom(stanza.getTo());
                con.sendStanza(subscribed);

                if( ! roster.contains(stanza.getFrom())) {
                    System.out.println(head + "Friend added.");
                    addFriend(stanza.getFrom(),"computer");
                }

            } else {
                System.out.println(head + "Subscribed received from " + stanza.getFrom());
                if(roster.contains(stanza.getFrom())) {
                    System.out.println(head + "OK");
                }
            }
        }
    };

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?

Kaine.Yan
  • 11
  • 3
  • http://stackoverflow.com/questions/12812855/android-add-friend-using-smack – Chathz Oct 13 '15 at 05:33
  • I found the resolution too. But still I don't konw how to change my code, because the version mentioned is lower and my version is 4.1.1. Higher version is quite different – Kaine.Yan Oct 13 '15 at 06:24

1 Answers1

0

Try this:

public void addFriend(User usuario) {
        if (APDApplication.connection != null && APDApplication.connection.isConnected()) {
            try {
                Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
                Roster roster = Roster.getInstanceFor(APDApplication.connection);

                if (!roster.contains(usuario.getChatId())) {
                    roster.createEntry(usuario.getChatId() + "@jabber/Smack", usuario.getName(), null);
                } else {
                    logManager.error("Contacto ya existente en la libreta de direcciones");
                }

            } catch (SmackException.NotLoggedInException e) {
                e.printStackTrace();
            } catch (SmackException.NoResponseException e) {
                e.printStackTrace();
            } catch (SmackException.NotConnectedException e) {
                e.printStackTrace();
            } catch (XMPPException.XMPPErrorException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Hope this helps you!!