0

Environment:

  • Ejabberd Version : 16.04
  • Smack-android-4.1.0

I'm trying to create a chat application which has a feature to search the available users by name, age etc. However, when I use the below code it says, the server doesn't have any search services in the server.

public void serachServiceList() {

    try {
        UserSearchManager searchManager = new UserSearchManager(connection);
        List<String> services = searchManager.getSearchServices();

        if (services == null || services.size() < 1) {
            System.out.println("No services");
        } else {
            for (String service : services) {
                Log.e("SERVICE", service);
            }

        }
    } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException e) {
        e.printStackTrace();
    }
}

Aren't there any default search service in XMPP server? Do I have to install a search service separately? I searched but I could not find any search plugins for smack. Would be glad to hear your solutions/ideas on this. Thanks in advance.

1 Answer 1

1

I also develop same functionality but i use VCard to serach user instead of SearchManager.

First of all need to add all contact in roster and using vcard you can get or set user avatar,get set user presence ,store user status like whatsapp etc,search user using JID etc

Here is my code search user from Vcard

Search user ():

public UserProfile search(String jid) throws SmackInvocationException {


    if (vCardHelper == null) {
        return null;
    }

    VCard vCard = vCardHelper.loadVCard(jid);
    String nickname = vCard.getNickName();

    return nickname == null ? null : new UserProfile(jid, vCard);
}

UserProfile is a model class which contains below values with garter setter

private String nickname;
private String jid;
private String status;
private byte[] avatar;

getNickName():

public String getNickname(String jid) throws SmackInvocationException {
        VCard vCard = vCardHelper.loadVCard(jid);

        return vCard.getNickName();
    }

loadVCard():

public VCard loadVCard(String jid) throws SmackInvocationException {
    VCard vCard = new VCard();
    try {
        vCard.load(con, jid);

        return vCard;
    } catch (Exception e) {
        throw new SmackInvocationException(e);
    }
}

Below code for get list of search user from server

-here i saw a simple example here i create AsyncTask this task put in doInBackground

try {
    UserProfile user = SmackHelper.getInstance(context).search(username);
    if (user != null) {
        if (user.getUserName().equals(PreferenceUtils.getUser(context))) {
            user.setType(UserProfile.TYPE_MYSELF);
        } else {
            Cursor c = context.getContentResolver().query(ContactTable.CONTENT_URI, new String[]{ContactTable._ID},
                    ContactTable.COLUMN_NAME_JID + " = ?", new String[] {user.getJid()}, null);
            if (c.moveToFirst()) {
                user.setType(UserProfile.TYPE_CONTACT);
            } else {
                user.setType(UserProfile.TYPE_NOT_CONTACT);
            }
        }
    }

    return Response.success(user);
} catch(SmackInvocationException e) {
     Log.e(String.format("search user error %s", e.toString()), e);

    return Response.error(e);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.