Getting all users from an openfire server using smack android

帅比萌擦擦* 提交于 2019-11-29 12:53:27
zain ullah

This is how I am getting all users from openfire

You actually have to pass wildcard(*) for the username

Here's the working code

Utils.getConnection() - my xmpp connection

public static void getAllXmppUsers()
{
    try {
    UserSearchManager manager = new UserSearchManager(Utils.getConnection());
    String searchFormString = "search." + Utils.getConnection().getServiceName();
    Log.d("***", "SearchForm: " + searchFormString);
    Form searchForm = null;

        searchForm = manager.getSearchForm(searchFormString);

    Form answerForm = searchForm.createAnswerForm();

    UserSearch userSearch = new UserSearch();
    answerForm.setAnswer("Username", true);
    answerForm.setAnswer("search", "*");

    ReportedData results = userSearch.sendSearchForm(Utils.getConnection(), answerForm, searchFormString);
    if (results != null) {
        List<ReportedData.Row> rows = results.getRows();
        for (ReportedData.Row row : rows) {
            Log.d("***", "row: " + row.getValues("Username").toString());
        }
    } else {
        Log.d("***", "No result found");
    }

    } catch (SmackException.NoResponseException e) {
        e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
        e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }
}
mubeen

Try this code. I tweak this code from this answer

UserSearchManager usm= new UserSearchManager(xmpp.getConnection());
Form searchForm = usm.getSearchForm("search." +xmpp.getConnection().getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", userName);
ReportedData data = usm
    .getSearchResults(answerForm, "search." + xmpp.getConnection().getServiceName());

if (data.getRows() != null) {
    for (ReportedData.Row row: data.getRows()) {
       for (String jid:row.getValues("jid")) {
        System.out.println(jid);
       }
    }
}

Smack is used to create a client. A client is used by one user. A user typically does not have access to all users of the server. Users do have contact lists, or rosters though, where you an add other users.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!