在android中使用xmpp从服务器获取所有注册用户

时间:2014-05-28 10:16:59

标签: android xmpp asmack

我正在为我的Android应用程序使用openfire服务器。我想让所有注册用户从服务器到我的Android应用程序。我正在使用xmpp asmack库。

这是我正在尝试的代码

UserSearchManager search = new UserSearchManager(connection);

        Form searchForm,answerForm;
        try {
            searchForm = search.getSearchForm("search."+connection.getServiceName());
            answerForm = searchForm.createAnswerForm();  
            answerForm.setAnswer("Username", true);  

            answerForm.setAnswer("search", "abcd");  

            org.jivesoftware.smackx.ReportedData data;

            data = search.getSearchResults(answerForm,"search."+connection.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);
                }

            }
    } catch (XMPPException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

请建议任何方法

1 个答案:

答案 0 :(得分:2)

不知怎的,我找到了答案

public List getAllUser(){

List<String> l = new ArrayList<String>();

UserSearchManager search = new UserSearchManager(connection);
try {
    Form searchForm = search.getSearchForm("search." + connection.getServiceName());
    Form answerForm = searchForm.createAnswerForm();

    answerForm.setAnswer("Username", true);
    answerForm.setAnswer("search", "*");

    System.out.println("search form");
    ReportedData data = search.getSearchResults(answerForm, "search."+connection.getHost());



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

        }else{
            System.out.println("its null");
        }
} catch (XMPPException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    System.out.println("not search form");
}
return l;

}

列表l = new ArrayList();

    l = con.getAllUser();


    for(int i=0;i<l.size();i++)
        System.out.println(l.get(i));