Exit to add friend to xmpp smack

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

A-> B (subscribe) B-> A (signed) BA (subscribe) A-> B (signed)

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

But in my application, it appears only after you log into LOGGING OUT and LOGGING again. A MAN SHOULD BE EXIT ONE TIME AFTER HE JOINS A FRIEND, AND ONLY A FRIEND IS KNOWN TO A FRIEND>

What is the problem? I found a lot, but did not find any error :(

Logcat does not display an error.

I also printed syso output after sending each package. It always says NONE (in the case of the person to whom the request is sent) and always says TO / FROM (in the case of the user who sent the request to a friend). Both are not reflected until the person logs in and logs back in.

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; } } 

He will send a notification to another user .. about permission to write this code: -

 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(); } }); 

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

Please, help. The subscription status instantly changes on the server, but in the application it is updated after logging out once.

Here is the code that 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

+1
source share
1 answer

To send a request that you must use

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

And for the Listening Request, which you must listen using the PacketListener . And check out 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); 
+2
source

Source: https://habr.com/ru/post/1440463/


All Articles