powered by Jive Software

How to search for a user or get full list of users on openfire using smack

Hi,

Here’s what I’m using:

Openfire 3.10.2

Smack 4.1.2

Environment: Android

I’m trying to search for a registered user, but I kept failing to do so. I’ve tried so many different combinations. I also tried on an older version of smack.

Here’s my lastest modified code:

UserSearchManager manager = new UserSearchManager(connection);
  try {
   String searchFormString = "search." + connection.getServiceName();
   Log.d("***", "SearchForm: " + searchFormString);
   Form searchForm = manager.getSearchForm(searchFormString);
   Form answerForm = searchForm.createAnswerForm();    UserSearch userSearch = new UserSearch();
   answerForm.setAnswer("Username", true);
   answerForm.setAnswer("search", user);    ReportedData results = userSearch.sendSearchForm(connection, 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 (XMPPException e) {
  e.printStackTrace();
   } catch (SmackException.NotConnectedException e) {
  e.printStackTrace();
   } catch (SmackException.NoResponseException e) {
  e.printStackTrace();
   }

I would get this error

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

on line 5, Form searchForm = manager.getSearchForm(searchFormString);

I don’t know if I missed configure something on openfire server, or do I need to setup something before I start searching.

My workaround idea was to get the full list of registered users and then search from there, but I’m not sure how to achieve that.

Once I logged in, the roster only consists of my friends and groups.

Could someone point me to the right direction?

Thanks,

Late reply, but I got a similar problem. The rror you get is because of this line:

String searchFormString = “search.” + connection.getServiceName();

The “search.” is not used by every server. There is a method where you can get the used search string in smack:

Collection services = search.getSearchServices(); //search is UserSearchManager

I ended up with getting the search string by:

String[] nn = services.toArray(new String[services.size()]);

for (int i = 0; i < nn.length; i++) {

searchString = nn[i].toString();

}

Usually, you get only on String so the array has only one item. But that I don´t exactly know, so some pople use

searchString = nn[0].toString();

instead of the loop. Anyway, I also was not successful because I allways get an empty array for ReportedData. But I get the searchForm with all searchFields. The next problem is here, where I get for both an IllegalArgumentException because the fields don´t exist on the form:

answerForm.setAnswer(“Username”, true);

answerForm.setAnswer(“search”, user);

instead of this, I have seen that the search fields don´t have these values on for example securejabber.me or draugr,de servers. Maybe this is different on other servers. I used:

answerForm.setAnswer(“user”,jabberId) //jabberId should be a valid jabber id like user@example.com

Now, Everything works, but a get an empty ReportedData. Maybe you have mor luck and it will be great if you share your solution if you are successful.