2

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


    }   

}
Community
  • 1
  • 1
Shalawei
  • 57
  • 1
  • 9
  • 1
    What is ... the problem?! :) Can you post the exception log, or describe the problem? – Jack Nov 21 '11 at 15:09
  • As i have also mention in 3 no point, the problem is search is not performed I dont know where the problem is. I am trying this since 5 hours to find the bug. – Shalawei Nov 21 '11 at 15:11
  • why don't you just rely that the createUser (whatever is called) functoin, will throw an exception, if user already exists, than you catch the exception. Why would you need to explicitly check if user already exists? – hovanessyan Nov 21 '11 at 15:22
  • @hovanessyan: I appreciate your suggestion..but i am sorry if i am wrong i have tried this try{ AccountManager manager = conn.getAccountManager(); manager.createAccount(usename,password);} catch(...) but could get any result so..can u show the details of this please – Shalawei Nov 21 '11 at 15:28
  • 1
    Have you checked if you establish connection to server at all? Can you query the server for something and get a result back? Maybe can you possibly try to login and disconnect to the server with existing user, and monitor the logs of xmpp (is it Openfire) to see if the communication works? – hovanessyan Nov 21 '11 at 15:29
  • Yah I have checked the connection...where can i go an write a query? i could not understand u? – Shalawei Nov 21 '11 at 15:33
  • do you have access to the server logs? what does it happen on the server side when you execute check()? – hovanessyan Nov 21 '11 at 15:36
  • @hovanessyan: org.jivesoftware.openfire.IQRouter - User tried to authenticate with this server using an unknown receipient: ..this is the information of the log.. – Shalawei Nov 21 '11 at 16:45
  • ok is 10.0.2.2 == search.ramesh-pc. What is the hostname (fqdm) of your server? This looks more like a setup problem, rather than wrong programming code. – hovanessyan Nov 21 '11 at 17:40
  • I am not sure how the search works in xmpp, but when you're connecting, you're not supplying a username to your connection. Than you use this connection to create UserSearchManager. Check documentation if you need any specific permissions to initiate a search (e.g. user to be registered in XmppDomain). – hovanessyan Nov 21 '11 at 17:43
  • before user register user have to check if the username is used or not so how can user register before doing that..10.0.2.2 is the local server and search.ramesh-pc is the search domain... – Shalawei Nov 21 '11 at 17:57
  • your code is "client code", so if the API is decent enough, it will throw you an error, that a user with this username, already exists (e.g. some of those error codes http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smack/packet/XMPPError.html) – hovanessyan Nov 21 '11 at 21:50

2 Answers2

2

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

Pang
  • 9,564
  • 146
  • 81
  • 122
nitin tyagi
  • 1,176
  • 1
  • 19
  • 52
-1

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;
        }
shyam.y
  • 1,441
  • 1
  • 16
  • 17