Search for existing user using xmpp/UserSearchMana

2019-01-26 22:31发布

问题:

I have gone through this1, this2 and this3 but still cannot find the solution of the problem.

I could know where my problem is what I am doing in this code is:

  1. Take the input from textbox and
  2. Pass that value to check if the username supplied in the textbox exists or not
  3. Whenever I click check button only "Function called" is displayed and is not performing search.

Here is my code what I done so far.

public class Registration extends Activity implements OnClickListener {
    private final static String SERVER_HOST = "10.0.2.2";
    private final static int SERVER_PORT = 5222;
    private static final String TAG = null;
    private ProviderManager pm;
    private EditText username;
    private Button btn;

    private XMPPConnection connection;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);

        try {

            initConnection();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

        username = (EditText) this.findViewById(R.id.txtregusername);

        btn=(Button) this.findViewById(R.id.btncheck);
        btn.setOnClickListener(this);      
}

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
        case R.id.btncheck:
            try {
                check();
            } catch (XMPPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        break;

        }

    }

    private void check() throws XMPPException{ 
         Toast.makeText(this,"Function called",Toast.LENGTH_SHORT).show();

        pm.addIQProvider("query","jabber:iq:search", new UserSearch.Provider());
        UserSearchManager search = new UserSearchManager(connection);  

        Form searchForm = search.getSearchForm("search."+connection.getServiceName());  
        Form answerForm = searchForm.createAnswerForm();  
        answerForm.setAnswer("Username", true);  
        Toast.makeText(this,username.getText().toString(),Toast.LENGTH_SHORT).show();
        answerForm.setAnswer("search", username.getText().toString());  
        ReportedData data = search.getSearchResults(answerForm,"search."+connection.getServiceName());  

    if(data.getRows() != null)
        {
             Toast.makeText(this,"Username Exists",Toast.LENGTH_SHORT).show();
        }
        else
        {
             Toast.makeText(this,"Username Available",Toast.LENGTH_SHORT).show();

        }

    }


    private void initConnection() throws XMPPException{
     ConnectionConfiguration config=new ConnectionConfiguration(SERVER_HOST,SERVER_PORT);
      config.setSecurityMode(SecurityMode.enabled);
      config.setSASLAuthenticationEnabled(true);
      connection=new XMPPConnection(config);

      try {    

       connection.connect();
       }

    catch (XMPPException e) {     
          Log.e(TAG, "Error connection to XMPP Server");     
          e.printStackTrace(); 
          } 


    }   

}

回答1:

Try this. It solves my problem.

UserSearchManager search = new UserSearchManager(mXMPPConnection);  

Form searchForm = search.getSearchForm("search."+mXMPPConnection.getServiceName());

Form answerForm = searchForm.createAnswerForm();  
answerForm.setAnswer("Username", true);  

answerForm.setAnswer("search", user);  

org.jivesoftware.smackx.ReportedData data = search.getSearchResults(answerForm,"search."+mXMPPConnection.getServiceName());  

if(data.getRows() != null)
{
    Iterator<Row> it = data.getRows();
    while(it.hasNext())
    {
        Row row = it.next();
        Iterator iterator = row.getValues("jid");
        if(iterator.hasNext())
        {
            String value = iterator.next().toString();
            Log.i("Iteartor values......"," "+value);
        }
        //Log.i("Iteartor values......"," "+value);
    }
    Toast.makeText(_service,"Username Exists",Toast.LENGTH_SHORT).show();
    );
}

If Server has not any entery with that specified name then Itearator it has no value and code will not go inside while(it.hasNext).



回答2:

Hi you need to use ProviderManager first, otherwise search will not work properly.Below is my working code which works perfectly, Hope it will help:

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.PrivacyList;
import org.jivesoftware.smack.PrivacyListManager;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.GroupChatInvitation;
import org.jivesoftware.smackx.PrivateDataManager;
import org.jivesoftware.smackx.ReportedData.Row;
import org.jivesoftware.smackx.bytestreams.socks5.provider.BytestreamsProvider;
import org.jivesoftware.smackx.packet.ChatStateExtension;
import org.jivesoftware.smackx.packet.LastActivity;
import org.jivesoftware.smackx.packet.OfflineMessageInfo;
import org.jivesoftware.smackx.packet.OfflineMessageRequest;
import org.jivesoftware.smackx.packet.SharedGroupsInfo;
import org.jivesoftware.smackx.packet.VCard;
import org.jivesoftware.smackx.provider.AdHocCommandDataProvider;
import org.jivesoftware.smackx.provider.DataFormProvider;
import org.jivesoftware.smackx.provider.DelayInformationProvider;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
import org.jivesoftware.smackx.provider.MUCAdminProvider;
import org.jivesoftware.smackx.provider.MUCOwnerProvider;
import org.jivesoftware.smackx.provider.MUCUserProvider;
import org.jivesoftware.smackx.provider.MessageEventProvider;
import org.jivesoftware.smackx.provider.MultipleAddressesProvider;
import org.jivesoftware.smackx.provider.RosterExchangeProvider;
import org.jivesoftware.smackx.provider.StreamInitiationProvider;
import org.jivesoftware.smackx.provider.VCardProvider;
import org.jivesoftware.smackx.provider.XHTMLExtensionProvider;
import org.jivesoftware.smackx.search.UserSearch;
import org.jivesoftware.smackx.search.UserSearchManager;
    public static List<String> getUserListBySearch(XMPPConnection mXMPPConnection, String searchString){
            ProviderManager.getInstance().addIQProvider("query","jabber:iq:search", new UserSearch.Provider());
            List<String> l = new ArrayList<String>();
            try {
                UserSearchManager search = new UserSearchManager(mXMPPConnection);  

                Form searchForm = search.getSearchForm("search."+mXMPPConnection.getServiceName());
                Form answerForm = searchForm.createAnswerForm();  
                answerForm.setAnswer("Username", true);  
                answerForm.setAnswer("search", searchString);  
                org.jivesoftware.smackx.ReportedData data = search.getSearchResults(answerForm,"search."+mXMPPConnection.getServiceName());  

                if(data.getRows() != null)
                {
                    Iterator<Row> it = data.getRows();
                    while(it.hasNext())
                    {
                        Row row = it.next();
                        System.out.println(row);
                        Iterator iterator = row.getValues("jid");
                        if(iterator.hasNext())
                        {
                            String value = iterator.next().toString();
                            l.add(value);
                            System.out.println("Iteartor values......"+value);
                        }
                        //Log.i("Iteartor values......"," "+value);
                    }
                    System.out.println("UserName Exists");

                }
            } catch (Exception e) {
                System.out.println("Exception in Loading user search"+e);
            }
            return l;
        }