Getting all users from an openfire server using sm

2020-02-13 06:23发布

i have a openfire server running on my localhost and i am successfully able to send and receive messages to registered users. however i am not been able to get all users from server. i am logged in with user that doesn't have a administration access. so do i need to give any permission on server side?

The code i am using for getting all users is..

   if ( xmpp.getConnection()== null || !xmpp.getConnection().isConnected())
        return;

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

        for (ReportedData.Row row : data.getRows())
        {
           arrayList.add(row.getValues("Username").toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

i tried some solutions that shows to use Roster class, however that is also not helping me. Can anyone show what i am doing wrong or if i need to give any permission as i am not logged in as admin? The error i am getting is..

org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: remote-server-not-found 

Thanks :)

3条回答
做个烂人
2楼-- · 2020-02-13 06:59

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();
    }
}
查看更多
The star\"
3楼-- · 2020-02-13 07:07

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.

查看更多
一夜七次
4楼-- · 2020-02-13 07:08

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);
       }
    }
}
查看更多
登录 后发表回答