1
votes

I am using asmack library to develop a whatsapp style chat application.I am able to connect and create accounts.I wanted to implement the "Add chat" functionality of whatsapp,where we select friends from a list of phone numbers which are registered.For this,i wanted to do a usersearch on my server for those numbers that i had in my phone,and then add them to my roster once i clicked them. I can login and register users on my ejabberd 2.11 server easily. But i cannot retrieve the list of already registered users from it. I used this code for searching:

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

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

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

This worked PERFECTLY on my OPENFIRE server..but for my ejabberd2 server..i had to modify it to:

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

answerForm.setAnswer("user",username);//passed my jid in username i.e example@mydomain


System.out.println("search form");
ReportedData data = search.getSearchResults(answerForm,"vjud."+conn.getServiceName());

if(!data.getRows().isEmpty())
{
List<Row> hue = data.getRows();Iterator<Row> it=hue.iterator();
while(it.hasNext())
{
Row row = it.next();
List<String> hue2 = row.getValues("jid");Iterator<String> iterator=hue2.iterator();
if(iterator.hasNext())
{
String value = iterator.next().toString();
System.out.println(value);
}

}

}
else{
System.out.println("its null");
}

The above code always returned a null form.So what arguments should i pass in answerForm.setAnswer(); for ejabberd? Please see that "answerForm.setAnswer("Username",true); answerForm.setAnswer("search",user);" worked for openfire.. but ejabberd doesnt have a boolean type Username field..so what should i pass?

1
Please format your question, right now it's terrible to read.Flow
@Flow sorry im new ..if you could help?how should i search for users in ejabberd server?Rahul Virdi
your first code gives this error...tell me how to resolve it "No response from server on status set"....!!!Rishabh Srivastava
The first code works with openfire server.But i would suggest a better approach now.The usersearch method is very slow.A better approach is suggested in the answer @RishabhSrivastavaRahul Virdi

1 Answers

0
votes

The solution to this problem is not to use any usersearch, but implement a direct connectivity to a database containing the userids. Here's how it's done:

  1. For openfire, there is an option to connect to a mysql database. Install mysql and connect your db.
  2. When a user creates an account, it will be listed in the database which you linked.
  3. Now the actual search part will make use of php, since android cannot make direct connectivity to mysql db.
  4. On the server pc, install wamp server. Make a php script that queries your db for the userid values that it gets from the client requesting it (From your client you can make a php post of all the mobile numbers, compare it with the numbers on the server db, and then send back a list of existing numbers to your client.

I hope this solves your problem.