How to get custom attribute value from an XMPP XML message?

前端 未结 3 1846
梦如初夏
梦如初夏 2021-01-05 11:30

Ok guys, simple question but pretty important to me.

so, other android client are sending this xml msg:



        
相关标签:
3条回答
  • 2021-01-05 12:08

    You can't achieve it without modifying the source code. And the proper approach in XMPP is to create extensions to the standard packets, not modify them. You can refer to Cannot Read Custom Attributes From Message TAG when using asmack XMPP library for android?, which provides the details.

    0 讨论(0)
  • 2021-01-05 12:09

    About the ID, first get a handle on extension then look for ID, so

    DeliveryReceipt deliveryReceiptObj =(DeliveryReceipt) message.getExtension(DeliveryReceipt.NAMESPACE);
    // ID below is what you want
    deliveryReceiptObj.getId();
    

    Related discussion: asmack - receiving custom XML messages

    1. define your EmbeddedPacketExtension ( so you get a handle on this instead of DefaultPacketExtension provided by SMACK )

    2. A provider that extends EmbeddedExtensionProvider

    3. registerProvider you just created with Namespace

    code follows:

    /**
    * 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());
    
    0 讨论(0)
  • 2021-01-05 12:32

    There exists another way to get attribute value, which defines the method UserInfo4XMPP by implementing ExtensionElement:

    import org.jivesoftware.smack.packet.ExtensionElement;
    public class UserInfo4XMPP implements ExtensionElement{
        public static final String NAMESPACE = "urn:xmpp:receipts";
        private String elementName = "received ";
        private String id = "";    
        @Override
        public String getElementName() {
            return elementName;
        }
            @Override
        public CharSequence toXML() {  
            return "<received xmlns='" + NAMESPACE + "' id='" + id + "'/>";
        }
        @Override
        public String getNamespace() {
            return NAMESPACE;
        }
        public String getId()
        {
            return id;
        }
    }
    

    The function body is similar to DeliveryReceiptProvider.

    0 讨论(0)
提交回复
热议问题