7

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?

Flow
  • 23,572
  • 15
  • 99
  • 156
nitin tyagi
  • 1,176
  • 1
  • 19
  • 52

1 Answers1

7

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

richardaum
  • 6,651
  • 12
  • 48
  • 64
nitin tyagi
  • 1,176
  • 1
  • 19
  • 52
  • 1
    Note that the xmpp connection session must be authorized first. For example - if you're registering a new account - you couldn't use this method to check username availability. – ılǝ Feb 26 '14 at 02:38
  • 1
    @nitin tyagi answerForm.setAnswer("Username", true); In this case what is user value in answerForm.setAnswer("search", user); It it username or other. – Mahi Apr 02 '14 at 15:13
  • @MahiSingh do you already know if user is the username or the jabberid? – Tijme Jun 22 '14 at 13:08
  • 1
    @FinnWea ya i know it is jabberid – Mahi Jun 23 '14 at 06:43
  • if i dont have an account .and i want to create one and check for its avialibility then what / – Sagar Nayak Apr 20 '16 at 10:59
  • @Sagar Nayak: You can only catch the error message. If an account already exists, an error is thrown. The problem with that answer is, you can check if the jabber id exists, but you need a valid connection, you need to be authorized and that´s only possible if you have an account. But the error will not tell you why, so on xmpp you are left alone... – Opiatefuchs Sep 30 '16 at 03:53
  • additional info to answer: It´s not allowed or supported on every server. It could throw a XMPPException if service for search is not available. – Opiatefuchs Sep 30 '16 at 04:20