Search for users on Ejabberd2 using smack, fails

2019-06-25 15:56发布

Server: ejabberd2 XMPP: smack 3.3.1

The goal is to design a chat engine. The user, by entering a username should be able to find out if the entered username is registered on the server.

The Smack Javadocs can be found here

  • I am using UserSearch classes found in the Smack library.
  • I can connect to the server.
  • I can login using a username and password.
  • I can add/sign up new users into the server.

I want to find if a username already exists on the server. The code that I have used and its respective error that I got are given below.

UserSearchManager search = new UserSearchManager(connection);
Form searchForm = search.getSearchForm("search."+connection.getServiceName());
Form answerForm = searchForm.createAnswerForm(); 
answerForm.setAnswer("Username", true);  
answerForm.setAnswer("search", name);

ReportedData data = search.getSearchResults(answerForm,"search."+connection.getServiceName());

The above code outputs the following error:

service-unavailable(503)
at org.jivesoftware.smackx.search.UserSearch.getSearchForm(UserSearch.java:84)
at org.jivesoftware.smackx.search.UserSearchManager.getSearchForm(UserSearchManager.java:73)
at com.phanism.www.XmppConnect.searchUser(XmppConnect.java:74)
at com.phanism.www.XmppConnect.main(XmppConnect.java:97)

1条回答
ら.Afraid
2楼-- · 2019-06-25 16:36

You have not set the username you want to search.

erSearchManager search = new UserSearchManager(connection);
Form searchForm = search.getSearchForm("search."+connection.getServiceName());
Form answerForm = searchForm.createAnswerForm(); 
answerForm.setAnswer("Username", true);  
answerForm.setAnswer("search", name);//Here username must be added name replace by "amith"

Then it will work.. :)

查看更多
登录 后发表回答