1

In my application when I have to add a friend I usually do send subscription packets 4 times i.e

A->B (subscribe) B->A ( subscribed) B-A( subscribe) A->B ( subscribed)

After each step I see on the server the status changes immediately.

But in my application it only comes to reflect after LOGGING OUT and LOGGING in again. THE PERSON HAS TO LOGOUT ONCE AFTER HE HAS ADDED A FRIEND AND THEN ONLY THE FRIEND IS SHOWN IN HIS FRIEND LIST>

What's the problem? I have found a lot but didnot found any error :(

No error is showing in the logcat.

I have also printed the syso output after each packet is sent. It always says as NONE ( in the case of the person to whom request is sent ) and Always says TO/FROM ( in the case of the user who has sent the friend request ).. Both is not reflected untill and unless a person logs out and logs in again.

Please help me :(

Add Friend Function

public boolean addFriend(String jid) {
        String nickname = null;
        String idExtension = jid+"@abc.hostname.com";
        nickname = StringUtils.parseBareAddress(jid);
        if (!roster.contains(idExtension)) {
            try {   
                roster.createEntry(idExtension, nickname, null);
                //to subscribe the user in the entry
                Presence subscribe = new Presence(Presence.Type.subscribe);
                subscribe.setTo(idExtension);               
                connection.sendPacket(subscribe);   
                return true;

            } catch (XMPPException e) {
                System.err.println("Error in adding friend");
                return false;
            }
        } else {
            return false;
        }
    }

It will send a notification to the other user.. on allowing which this code is written :-

btn_Allow = (Button)findViewById(R.id.btn_manageNotification_ALLOW);
        btn_Allow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                //accept the friends subscription
                Presence subscribed = new Presence(Presence.Type.subscribed);
                subscribed.setTo(id);               
                connection.sendPacket(subscribed);



                mCustomProgressDialog = CustomProgressDialog.createDialog(
                        ManageNotification.this, "", "");
                mCustomProgressDialog.show();   
                mCustomProgressDialog.setCancelable(false); 
                new Thread(){
                    public void run() {

                        try {
                            sleep(5000);
                            //mXmconn.getContactList();

                            /*Presence subscribed = new Presence(Presence.Type.subscribe);
                            subscribed.setTo(id);               
                            System.out.println("The user is :"+id);
                            connection.sendPacket(subscribed);*/

                        } catch (InterruptedException e) {}                     
                        mReturnUserMenu.sendEmptyMessage(0);

                    };
                }.start();
            }
        });

same it is done again on allow again to the user who initiated the request.

Please help. The subscription status is changing on the server instantly but on app it is updating after logout once.

Here is the code which represents the lists

public  void getContactList(){

        roster = connection.getRoster();

        Collection<RosterEntry> entries = roster.getEntries();
        System.out.println("Total=="+entries.size());
        mfriendList.clear();
        mfriendPendingList.clear();
        mfriendRequestList.clear();
        for (RosterEntry entry : entries) {
            mFriendsDataClass = new FriendsDataClass();

            mFriendsDataClass.friendName = entry.getUser().toString();

            String user = entry.getUser();

            int index_of_Alpha = user.indexOf("@");
            /*System.out.println("The current working user is : "+user);
            System.out.println("His status is"+entry.getType().toString());*/
            String subID = user.substring(0, index_of_Alpha);
            Presence availability = roster.getPresence(user);
            Mode userMode = availability.getMode();

            mFriendsDataClass.availability = "";
            mFriendsDataClass.friendNickName = subID;           
            mFriendsDataClass.friendStatus = stusMsg.toString();
            mFriendsDataClass.friendState = retrieveState_mode(availability.getMode(),availability.isAvailable());
            if(entry.getType().toString().equalsIgnoreCase("to")){
                //getContactList();
                mfriendRequestList.add(mFriendsDataClass);
            }else if(entry.getType().toString().equalsIgnoreCase("from")){
                //getContactList();
                mfriendPendingList.add(mFriendsDataClass);
            }else if(entry.getType().toString().equalsIgnoreCase("both")){
                //getContactList();
                mfriendList.add(mFriendsDataClass);
            }           
        }
    }

Thanks

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • Please provide some code because its difficult to answer this using this scenario, But i would suggest you to make a refresh button which works as a login and reloads everything again, you can also save the user name and password and re-login without the user knowing about it – Girish Nair Nov 12 '12 at 04:56
  • @GirishNair What is user is chatting with someone else?? the connection will be lost then??? Is there any way to refresh the xmpp connection ? :( – Gaurav Arora Nov 12 '12 at 04:59
  • Save the chat info and try reloading. Have you checked this http://stackoverflow.com/questions/9632865/reset-the-xmpp-connection-if-the-page-gets-refreshed – Girish Nair Nov 12 '12 at 05:04
  • Yes I have checked the same. Not solved ? I am worried about this thing that why I have to logout?? Why ?? – Gaurav Arora Nov 12 '12 at 05:11
  • I dont know why but try some thing like an `invalidate()` method or something similar to that so that its updated – Girish Nair Nov 12 '12 at 06:03
  • @GirishNair You are not getting it bro. The connection itself is picking up previous subscription type. Once it is logged out and logged in again then it picks the new subscription status :( – Gaurav Arora Nov 12 '12 at 06:11

1 Answers1

2

For sending a request you have to use,

roster.createEntry("mail_id", null, null);

And for Listening the Request you have to listen using PacketListener. And check the Presence

Presence presence = (Presence) packet;
Presence presence_request = new Presence(Presence.Type.subscribed);
presence_request.setTo(presence.getFrom());
presence_request.setType(Presence.Type.subscribed);
presence_request.setFrom("current_logged_in_user");
connection.sendPacket(presence_request);
roster.createEntry(presence.getFrom(), null, null);
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • I have done the same way and its working alrite, but the problem is i have to logout each time to look at the changes :( – Gaurav Arora Nov 14 '12 at 05:21
  • I have a problem in the above solution. When I use the above code in the packet listener, then it works fine. But I want to accept the frnd request using one of my allow button in another activity then the above code does only - TO and FROM subscription and not BOTH subscription. How can I use it at ALLOW button – Gaurav Arora Mar 12 '13 at 12:28
  • I want to know friend's availability, different types of codes confused me, can you please show me the proper way to know it? – Rahul Upadhyay May 31 '13 at 11:24