【问题标题】:I can not get nickName from users that currently added我无法从当前添加的用户那里获取昵称
【发布时间】:2014-09-11 07:31:25
【问题描述】:

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

谁能帮帮我?

【问题讨论】:

    标签: android xmpp openfire asmack


    【解决方案1】:

    只是关于这个问题的更新:VCardload() 方法现已弃用。

    你可以使用这个方法:

    /**
     * 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 中检索用户昵称。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-01-21
        • 2020-12-28
        • 2011-05-08
        • 2020-08-05
        • 2019-03-24
        • 1970-01-01
        • 1970-01-01
        • 2021-09-15
        • 2013-10-15
        相关资源
        最近更新 更多