首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向我的XMPP帐户添加和订阅jabber条目?

如何向我的XMPP帐户添加和订阅jabber条目?
EN

Stack Overflow用户
提问于 2012-10-25 05:49:28
回答 1查看 7.7K关注 0票数 8

通过使用这段代码,我可以将Entry添加到Xmpp帐户。我不能得到订阅“两者”,而不是这个,我得到了none

roster.createEntry("abc@xyz.com", "abc", null);

当我订阅这个帐户时,如何使用type=both添加条目。我想知道xmpp是否具有publish-subscribe功能?

  1. 如何获得入站在场通知?
  2. 如何发送出站显示通知?

编辑:

代码语言:javascript
复制
public void Addcontact() {    
    Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
    Roster roster = m_connection.getRoster();

    if(!roster.contains("pa@ace.com")) {        try {           
            roster.createEntry("pa@ace.com", "pa", null);               
        } catch (XMPPException e) {          
            e.printStackTrace();
        }
    }else {
        Log.i("Acc = ", "contains");
    }
}

我正在添加这样的条目,但我仍然得到了存在类型=无。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-25 07:16:39

下面是我如何在我的应用程序中添加另一个朋友。

代码语言:javascript
复制
protected void doAddContactToListAsync(String address, String name,
                ContactList list) throws ImException {
            debug(TAG, "add contact to " + list.getName());
            Presence response = new Presence.Type.subscribed);
            response.setTo(address);

            sendPacket(response);

            Roster roster = mConnection.getRoster();
            String[] groups = new String[] { list.getName() };
            if (name == null) {
                name = parseAddressName(address);
            }
            try {

                roster.createEntry(address, name, groups);

                // If contact exists locally, don't create another copy
                Contact contact = makeContact(name, address);
                if (!containsContact(contact))
                    notifyContactListUpdated(list,
                            ContactListListener.LIST_CONTACT_ADDED, contact);
                else
                    debug(TAG, "skip adding existing contact locally " + name);
            } catch (XMPPException e) {
                throw new RuntimeException(e);
            }
        }

只需使用最基本的部分

代码语言:javascript
复制
Presence response = new Presence.Type.subscribed);
response.setTo(address);
sendPacket(response);

Roster roster = mConnection.getRoster();
roster.createEntry(address, name, groups);

为了侦听传入的请求,请将addPacketListener注册到您的连接

代码语言:javascript
复制
    mConnection.addPacketListener(new PacketListener() {

            @Override
            public void processPacket(Packet packet) {

                Presence presence = (Presence) packet;
                    if (presence.getType() == Type.subscribe) {
                    debug(TAG, "sub request from 1" + address);
//Implement accept or reject depend on user action. 
            mContactListManager.getSubscriptionRequestListener()
                            .onSubScriptionRequest(contact);
//or you can test with send back Presence.subscribe first and send Presence.subscribed back to requester.


                } else {// Handle other Presence type.
                    int type = parsePresence(presence);
                    debug(TAG, "sub request from " + type);
                    contact.setPresence(new Presence(type,
                            presence.getStatus(), null, null,
                            Presence.CLIENT_TYPE_DEFAULT));

                }
            }
        }, new PacketTypeFilter(Presence.class));

        mConnection.connect();

正确的顺序:

  1. User1发送订阅user2。
  2. User2将订阅和订阅发送回user1。
  3. User1将Subsribed发送到user2。

另一个这样你就可以检查question

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13062356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档