简体   繁体   中英

Getting all users from an openfire server using smack android

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM