5

I am new to the use of smack library and making one chatting application. I have made upto much extent and at this step i want to ask two questions.

  1. when i add a friend the friend got added in my list but there is not any notification sent to the FRIEND whom i have added, How to achieve the same. I have added the code below.

  2. The second thing i want to ask is that how can I check whether the user which I am going to add is a part or member of the app or not ( mean it is on the server or not). So that the user who is not registered to the app should not be added in the friends list.

here is the code

public static boolean addFriend(String jid) {
            String nickname = null;
            nickname = StringUtils.parseBareAddress(jid);
            RosterEntry entry4 = roster.getEntry("samsad");
            if (!roster.contains(jid)) {
                try {
                    Presence subscribe = new Presence(Presence.Type.subscribe);
                    subscribe.setTo(jid);               
                    connection.sendPacket(subscribe);               
                    roster.createEntry(jid, nickname, null);
                      // Send a roster entry (any) to user2
                    RosterExchangeManager REM = new RosterExchangeManager(connection);
                    REM.send(entry4, jid);
                    return true;
                } catch (XMPPException e) {
                    System.err.println("Error in adding friend");
                    return false;
                }
            } else {
                return false;
            }
        }

Roster Exchange manager running in the service in background

/**Remotr Exchange Manager*/
             RosterExchangeManager rem = new RosterExchangeManager(connection);
              // Create a RosterExchangeListener that will iterate over the received roster entries
              RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
                  public void entriesReceived(String from, Iterator remoteRosterEntries) {
                      notification("Receive==4");
                      while (remoteRosterEntries.hasNext()) {
                          try {
                              // Get the received entry
                              RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) remoteRosterEntries.next();
                              // Display the remote entry on the console
                              System.out.println(remoteRosterEntry);
                              // Add the entry to the user2's roster
                              roster.createEntry(
                                  remoteRosterEntry.getUser(),
                                  remoteRosterEntry.getName(),
                                  remoteRosterEntry.getGroupArrayNames());
                              notification("Receive==1");   
                          }
                          catch (XMPPException e) {
                              e.printStackTrace();
                          }
                      }
                  }
              };
              rem.addRosterListener(rosterExchangeListener);
        }
        else{
            showToast("Connection lost-",0);
        }
    }
4

1 回答 1

1

1、问题是你必须先注册一个PacketListenerPresence.Type.subscribe才能连接到服务器。我在这里回答的所有添加和接受朋友的过程

2,您可以使用UserSearch类搜索特定用户,如果在服务器上找不到用户,则可以假设用户未在服务器上注册。

于 2012-10-31T04:43:24.297 回答