Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xmpp

smack

asmack

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?

like image 406
nitin tyagi Avatar asked Jan 08 '13 06:01

nitin tyagi


1 Answers

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

like image 167
nitin tyagi Avatar answered Sep 20 '22 13:09

nitin tyagi