微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从XMPP XML消息中获取自定义属性值?

如何解决如何从XMPP XML消息中获取自定义属性值?

关于ID,首先获取扩展名的句柄,然后查找ID,因此

DeliveryReceipt deliveryReceiptObj =(DeliveryReceipt) message.getExtension(DeliveryReceipt.NAMESPACE);
// ID below is what you want
deliveryReceiptObj.getId();
  1. 定义您的EmbeddedPacketExtension(这样您就可以对此进行处理,而不是 DefaultPacketExtension由SMACK提供)

  2. 扩展的提供者 EmbeddedExtensionProvider

  3. registerProvider 您刚刚使用命名空间创建的

代码如下:

/**
* User: suvrat 
* Represents a <b>message delivery receipt</b> entry as specified by
* <a href="http://xmpp.org/extensions/xep-0184.html">Message Delivery Receipts</a>.
*
*/

import org.jivesoftware.smack.packet.PacketExtension;

public class DeliveryReceipt implements PacketExtension
{

    public static final String NAMESPACE = "urn:xmpp:receipts";

    private String id; /// original ID of the delivered message

    public DeliveryReceipt(String id)
    {
        this.id = id;
    }

    public String getId()
    {
        return id;
    }

    public String getElementName()
    {
        return "received";
    }

    public String getNamespace()
    {
        return NAMESPACE;
    }

    public String toXML()
    {
        return "<received xmlns='" + NAMESPACE + "' id='" + id + "'/>";
    }
}

 /**
 * User: suvrat
 * The DeliveryReceiptProvider parses DeliveryReceipt packets.
 */

 */
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.provider.EmbeddedExtensionProvider;
import org.xmlpull.v1.XmlPullParser;

import java.util.List;
import java.util.Map;

public class DeliveryReceiptProvider extends EmbeddedExtensionProvider
{

    @Override
    protected PacketExtension createReturnExtension(String currentElement, String currentNamespace,
    Map<String, String> attributeMap, List<? extends PacketExtension> content)
    {
        return new DeliveryReceipt(attributeMap.get("id"));
    }

}

   //3.) finally where ever you would like to parse packet
 ProviderManager.getInstance().addExtensionProvider("received", DeliveryReceipt.NAMESPACE, new DeliveryReceiptProvider());

解决方法

好的,简单的问题,但对我来说很重要。

因此,其他android客户端正在发送此xml消息:

<message
  id='6ymdM-19'
  to='xox@xox.xox/smack'
  type='chat'>
  <subject>normal</subject>
  <received xmlns='urn:xmpp:receipts' id='HVgQw-5'/>
</message>

我的听众大致是这样的:

private class MsgListener implements ChatStateListener {
/**
 * Constructor.
 */
public MsgListener() {
}

@Override
public void processMessage(Chat chat,org.jivesoftware.smack.packet.Message message) {
    String xmlMessage = message.toXML();
    Log.v(TAG,"XML Chat: "+xmlMessage);
    // getExtension namespace try urn:xmpp:receipts
    if(xmlMessage.contains("<request xmlns=")) {
        Log.d(TAG,"new chat message arrive! reply with RECEIVED!");
        replyReceived(message);
    } else if(xmlMessage.contains("<received xmlns=")) {
        Log.d(TAG,"RECEIVED notification arrived!");
        PacketExtension statusExtension = 
                message.getExtension("urn:xmpp:receipts");
        Log.d(TAG,"Extension name: "+statusExtension.getElementName());
        Log.d(TAG,"Extension XML: "+statusExtension.toXML());
        Log.d(TAG,"Extension string: "+statusExtension.toString());
    }

     ....
     ....
     ....
}

在这种情况下,我想在“ received”元素标签内获取属性“ id”的值。但是我在日志中看到的是这样的:

RECEIVED notification arrived!
D/ChatAdapter(320): Extension name: received
D/ChatAdapter(320): Extension XML: <received xmlns="urn:xmpp:receipts"></received>
D/ChatAdapter(320): Extension string:              
org.jivesoftware.smack.packet.DefaultPacketExtension@44f10430

那么我如何获得“ HVgQw-5”?

更新

其实有些奇怪……我从我的SMACK调试中收到了xmlordinh,如下所示:

 <
 D/SMACK(320): 05:40:28 PM RCV  (1156991856): message id="6ymdM-19"  
 to="syeikh@emass.sangkuriang.co.id/Smack" from="emu22@emass.sangkuriang.co.id/Smack"  
 type="chat"><subject>
 D/SMACK(320): 05:40:28 PM RCV  (1156991856): normal</subject><thread>cr0900</thread>
 **<received xmlns="urn:xmpp:receipts" id="HVgQw-5"/>**<active      
 xmlns="http://jabber.org/protoc
 D/SMACK(320): 05:40:28 PM RCV  (1156991856): ol/chatstates"/></message>

但是当我执行message.toXML时,它就像这样打印出来:

XML Chat: <message id="6ymdM-19" to="syeikh@emass.sangkuriang.co.id/Smack" from="emu22@emass.sangkuriang.co.id/Smack" type="chat"><subject>normal</subject><thread>cr0900</thread>**<received xmlns="urn:xmpp:receipts">**</received><active xmlns="http://jabber.org/protocol/chatstates" /></message>

为什么会这样呢?为什么我想念“身份证”?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。