代码之家  ›  专栏  ›  技术社区  ›  MohammadReza Vahedi

我无法从当前添加的用户获取昵称

  •  2
  • MohammadReza Vahedi  · 技术社区  · 11 年前

    我是新来的。我正在编写一个聊天应用程序,当我通过向用户发送订阅包并让他接受来添加用户时,我检查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 回复  |  直到 11 年前
        1
  •  3
  •   Pieter Goosen Andrew Luo    11 年前

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

    VCard mVCard = new VCard();
    
    mVCard.load(your xmppconnection,user jid);
    String name = mVCard.getNickName();
    
        2
  •  2
  •   joao2fast4u    9 年前

    关于这件事的最新消息: 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中检索用户昵称。