Muhammad Hewedy
Muhammad Hewedy

Reputation: 30076

Get Nickname/Username using jid from Samck

Using Smack, how I can get Nickname/Username from JID using Smake.

Note, the JID is not in my roaster.

Thanks.

Upvotes: 1

Views: 1858

Answers (3)

Adil
Adil

Reputation: 812

we can get nick name using JID but we must add all users in VCARD

String nickname = smackHelper.getNickname(from);


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

    return vCard.getNickName();
}


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);
    }
}

Upvotes: 0

Wasim K. Memon
Wasim K. Memon

Reputation: 6067

May be its too late but for others who are facing this issue From smack:4.2.0

You can get all parts separately using JID.

if you have JID like abcd@xyzPc- 599

String local = XmppStringUtils.parseLocalpart(userObject.getUserId().toString());

Log.d(TAG, local); // print abcd

String domain = XmppStringUtils.parseDomain(userObject.getUserId().toString());

Log.d(TAG, domain); // print xyzPc-599

You can check code for XmppStringUtils.

Hope will help to someone...

Upvotes: 1

James W
James W

Reputation: 410

Use the String Utils of Smack to parse the name portion of the JID

Smack Docs String Utils

String name = StringUtils.parseName(JID);

WHen you say it's not in your Roster, how are you being passed the JID? Are you getting it from your message?

Upvotes: 0

Related Questions