我无法从当前添加的用户那里获取nickName

时间:2014-07-20 19:36:51

标签: android xmpp openfire asmack

我是asmack的新手。我正在写一个聊天应用程序,当我通过向他/她发送订阅包添加用户他接受时,我检查openFire服务器nikename和其他属性对新用户是好的,并且模式都是。

但是当我试图得到朋友时,昵称是空的。

如果我调试代码昵称接收正确但在运行模式下不能?

接收朋友的代码:

 public static void getContacts(final Context ctx)
    {
    try
    {
        try
        {
              Thread.sleep(1000);
        }
        catch(Exception ex)
        {

        }
        Roster roster = connection.getRoster();

        Collection<RosterEntry> entries = roster.getEntries();
        if(globalVars.friends == null)
            globalVars.friends = new ArrayList<globalVars.UserList>();
        globalVars.friends.clear();

        for(RosterEntry entry: entries)
        {
            String user = entry.getUser();
            String username = user.split("@")[0];
            Presence presence =  roster.getPresence(entry.getUser());
            int status = R.drawable.offline;
            if(presence.getType().equals(Presence.Type.available))
                status = R.drawable.online;
            //String fromto = presence.getFrom() + "   "+presence.getTo();
            globalVars.UserList ul = new UserList(username, status, globalVars.smallImageAddress(ctx, username));

            String wathsUp = "";

            try
            {
                if(presence.getStatus() != null)
                    wathsUp = presence.getStatus();
            }
            catch(Exception ex)
            {

            }
            ul.setComment(wathsUp);
            ul.setFriend(true);
            ul.setNikName(entry.getName());

            globalVars.friends.add(ul);
        }

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

使用vCard设置用户的昵称或其他详细信息。 使用此代码从jid获取Vcard信息

VCard mVCard = new VCard();

mVCard.load(your xmppconnection,user jid);
String name = mVCard.getNickName();

答案 1 :(得分:1)

只是关于此问题的更新:VCard load()方法现已弃用。

相反,您可以使用此方法:

/**
 * retrieves an user VCard
 *
 * @param userJid the user jid
 * @return the VCard object
 */
public VCard getVCard(String userJid) {
    VCard vCard = null;
    VCardManager vCardManager = VCardManager.getInstanceFor(connection);
    boolean isSupported;
    try {
        //remove resource name if necessary
        if (!TextUtils.isEmpty(userJid) && userJid.contains("/")) {
            userJid = userJid.split("/")[0];
        }
        isSupported = vCardManager.isSupported(userJid);
        if (isSupported)  // return true
            vCard = vCardManager.loadVCard(userJid);
    } catch (SmackException.NoResponseException e) {
        e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
        e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException iAE) {
        iAE.printStackTrace();
    }

    return vCard;
}

如您所见,该方法检查用户是否理解vCard-XML格式并进行交换。如果是,则返回VCard。

然后只需从VCard中检索用户昵称。