English 中文(简体)
在安康使用Xmpp/UserSearchManager的现有用户搜索
原标题:Search for existing user using xmpp/UserSearchManager in Android

我通过this1,this 2

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


    }   

}
问题回答

引证。 它解决了我的问题。

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).

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签