搜索在Android中使用XMPP / UserSearchManager现有用户 [英] Search for existing user using xmpp/UserSearchManager in Android

查看:192
本文介绍了搜索在Android中使用XMPP / UserSearchManager现有用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过<一个了href="http://stackoverflow.com/questions/7149928/how-to-uses-smack-api-usersearchmanager-with-local-server">this1, <一href="http://stackoverflow.com/questions/5910219/problem-using-usersearch-in-xmpp-with-asmack">this2和 this3 但仍无法找到问题的解决方案。

我可以知道我的问题是我在做什么在这个code是:

  1. 以输入来自文本框和
  2. 在传递价值,以检查是否在文本框中提供的用户名存在与否
  3. 当我点击查看按钮只有功能被称为显示,不执行搜索。

下面是我的code我这样做的远。

 公共类注册扩展活动实现OnClickListener {
    私人最终静态字符串SERVER_HOST =10.0.2.2;
    私人最终静态INT SERVER_PORT = 5222;
    私有静态最后字符串变量= NULL;
    私人的ProviderManager时;
    私人的EditText的用户名;
    私人按钮BTN;

    私人XMPPConnection连接;
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.registration);

        尝试 {

            initConnection();
        }赶上(XMPPException E){
            e.printStackTrace();
        }

        用户名=(EditText上)this.findViewById(R.id.txtregusername);

        BTN =(按钮)this.findViewById(R.id.btncheck);
        btn.setOnClickListener(本);
}

    @覆盖
    公共无效的onClick(视图v){
        开关(v.getId())
        {
        案例R.id.btncheck:
            尝试 {
                检查();
            }赶上(XMPPException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

        打破;

        }

    }

    私人无效检查()抛出XMPPException {
         Toast.makeText(这一点,功能叫,Toast.LENGTH_SHORT).show();

        pm.addIQProvider(查询,胡言乱语:IQ:搜索,新UserSearch.Provider());
        UserSearchManager搜索=新UserSearchManager(连接);

        形成searchForm = search.getSearchForm(搜索。+ connection.getServiceName());
        形成answerForm = searchForm.createAnswerForm();
        answerForm.setAnswer(用户名,真正的);
        Toast.makeText(这一点,username.getText()的toString(),Toast.LENGTH_SHORT。).show();
        answerForm.setAnswer(搜索,username.getText()的toString());
        ReportedData数据= search.getSearchResults(answerForm,搜索+ connection.getServiceName());

    如果(data.getRows()!= NULL)
        {
             Toast.makeText(这一点,用户名存在,Toast.LENGTH_SHORT).show();
        }
        其他
        {
             Toast.makeText(这一点,用户名可用,Toast.LENGTH_SHORT).show();

        }

    }


    私人无效initConnection()抛出XMPPException {
     ConnectionConfiguration配置=新ConnectionConfiguration(SERVER_HOST,SERVER_PORT);
      config.setSecurityMode(SecurityMode.enabled);
      config.setSASLAuthenticationEnabled(真正的);
      连接=新XMPPConnection(配置);

      尝试 {

       connection.connect();
       }

    赶上(XMPPException E){
          Log.e(TAG,错误连接到XMPP服务器);
          e.printStackTrace();
          }


    }

}
 

解决方案

试试这个它解决了我的问题。如果它解决您的问题,请接受这样的回答:

  UserSearchManager搜索=新UserSearchManager(mXMPPConnection);

            形成searchForm = search.getSearchForm(搜索。+ mXMPPConnection.getServiceName());

            形成answerForm = searchForm.createAnswerForm();
            answerForm.setAnswer(用户名,真正的);

            answerForm.setAnswer(搜索,用户);

            org.jivesoftware.smackx.ReportedData数据= search.getSearchResults(answerForm,搜索+ mXMPPConnection.getServiceName());

        如果(data.getRows()!= NULL)
            {
                迭代器&LT;行&GT;它= data.getRows();
                而(it.hasNext())
                {
                    鳞次栉比= it.next();
                    迭代器迭代器= row.getValues​​(JID);
                    如果(iterator.hasNext())
                    {
                        。字符串值= iterator.next()的toString();
                        Log.i(Iteartor值......,+值);
                    }
                    //Log.i("Iteartor价值观......,+值);
                }
                 Toast.makeText(_Service,用户名存在,Toast.LENGTH_SHORT).show();
                 );
            }
 

如果服务器没有与指定名称的任何entery然后Itearator它有没有价值,code也不会进去,而(it.hasNext)..

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


    }   

}

解决方案

try this it solve my problem . if it solve your problem please accept as a answer :

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

这篇关于搜索在Android中使用XMPP / UserSearchManager现有用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆