Check if a user exist on server in XMPP with (a)sm

2019-04-26 10:42发布

问题:

I'm working on a chat application using asmack as a library and on android platform. Is there is any way that before adding friend in Roster can I check that friend exist on server or not?

回答1:

I Found the answer :

UserSearchManager search = new UserSearchManager(mXMPPConnection);
Form searchForm = search
    .getSearchForm("search." + mXMPPConnection.getServiceName());

Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", user);
ReportedData data = search
    .getSearchResults(answerForm, "search." + mXMPPConnection.getServiceName());

if (data.getRows() != null) {
    for (ReportedData.Row row: data.getRows()) {
        for (String value: row.getValues("jid")) {
            Log.i("Iteartor values......", " " + value);
        }
    }
    Toast.makeText(_service, "Username Exists", Toast.LENGTH_SHORT).show();
}

if Server has not any entery with that specified name then Itearator it has no value and code will not go inside while(it.hasNext)..