2012-03-23 67 views
7

好吧,大家,簡單的問題,但對我來說很重要。如何從XMPP XML消息獲取自定義屬性值?

如此,其他Android客戶端在發送這個XML消息:

<message 
    id='6ymdM-19' 
    to='[email protected]/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 notification arrived! 
D/ChatAdapter(320): Extension name: received 
D/ChatAdapter(320): Extension XML: <received xmlns="urn:xmpp:receipts"></received> 
D/ChatAdapter(320): Extension string:    
[email protected] 

所以,我怎麼能得到「HVgQw-5」?

UPDATE

實際上,有一些奇怪...... 我從SMACK調試接收XML accordinh這樣的:

< 
D/SMACK(320): 05:40:28 PM RCV (1156991856): message id="6ymdM-19" 
to="[email protected]/Smack" from="[email protected]/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="[email protected]/Smack" from="[email protected]/Smack" type="chat"><subject>normal</subject><thread>cr0900</thread>**<received xmlns="urn:xmpp:receipts">**</received><active xmlns="http://jabber.org/protocol/chatstates" /></message> 

爲什麼會發生這種情況?爲什麼我會錯過「id」?

回答

10

關於ID,第一個得到擴展的句柄然後找ID,所以

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

相關討論

asmack - receiving custom XML messages

1)定義EmbeddedPacketExtension(這樣你就得到一個處理這而不是由SMACK提供的DefaultPacketExtension)

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()); 
+0

加上一個答案的第一部分。 – EagleEye 2015-12-15 12:42:46