2012-09-17 11 views
0

這是從XMPP服務器端發送的XML,我想要收到,以便我可以將它發送到我的XML解析器。當使用android的asmack XMPP庫時,無法從消息TAG讀取自定義屬性?

<message to="[email protected]/smack" chat_id="73392" 
     custom_packet="true" user_id="44" manager_id="39" time_stamp="0" website_id="0" 
     visitor_name="John" end_time="False" xml:lang="en-us" type="groupchat" 
     from="[email protected]/39"> 
     <body>Hello</body> 
     <x xmlns="http://jabber.org/protocol/muc#user"> 
     <status xmlns="" code="0"/> 
     </x></message> 

這是一個示例XML,我收到。 當我使用p.toXML(); // Packet p

<message to="[email protected]/Smack" 
    from="[email protected]/Visitor1171" type="groupchat"> 
    <body>Hello</body> 
    <delay xmlns="urn:xmpp:delay"></delay> 
    <x xmlns="jabber:x:delay" stamp="20120917T05:57:19" 
    from="[email protected]/4732abb5"> 
    </x></message> 

我剛剛開始使用XMPP服務器。因此,任何指導意見都將得到讚賞。

+0

永遠不要爲XMPP頂級元素創建自己的屬性。另見[XEP-134 2.1](http://xmpp.org/extensions/xep-0134.html#xmpp)。而是添加一個擴展名'/' – Flow

回答

-2
do { 
    ParseEvent event=parser.read(); 
    ParseEvent pe; 

    switch(event.getType()){ 
     case Xml.START_TAG: 
     if (event.getName().toString().equals("message")){ 
       int xx=event.getAttributeCount(); 

       String _s2=event.getAttribute("to").getValue(); 
       if(_s2=="" || _s2==null){ 
        _s2="N/A"; 
       } 

       String _s3=event.getAttribute("from").getValue();  
       if(_s3=="" || _s3==null){ 
        _s3="N/A"; 
       } 

       String _s4=event.getAttribute("type").getValue(); 
       if(_s4=="" || _s4==null){ 
        _s4="N/A"; 
       } 

       String _s1=_s2+"~~"+_s3+"~~"+_s4; 
       m_result.add(new BeanClassName(_s1));     
     } 
     (...) 
    } 
} 

您可以通過添加開始標籤每一個案例,然後設置在bean類值看你attribues。

+0

這個bean類在哪裏。我的意思是在哪個Asmack源代碼包中? –

+0

@SherazKhilji因此,是否需要我爲Decalcare一個豆類課程進行ü –

+0

是的請。我是XMPP的新手,所以請 –

4

你不能在Smack(因此aSmack)中做到這一點,而無需修改源代碼。它只會解析一個標準的消息節,所以你所有的自定義屬性都將被忽略。 XMPP中的正確方法是創建標準數據包的擴展,而不是修改它們。如果你有過被從服務器發送的內容控制,那麼你應該你的方法改爲添加自定義擴展的消息,從而改變這個

<message to="[email protected]/smack" chat_id="73392" 
custom_packet="true" user_id="44" manager_id="39" time_stamp="0" 
website_id="0" visitor_name="John" end_time="False" xml:lang="en-us" 
type="groupchat" from="[email protected]/39"> 
    <body>Hello</body> 
    <x xmlns="http://jabber.org/protocol/muc#user"> 
     <status xmlns="" code="0"/> 
    </x> 
</message> 

這個

<message to="[email protected]/smack" chat_id="73392" xml:lang="en-us" 
type="groupchat" from="[email protected]/39"> 
    <body>Hello</body> 
    <x xmlns="http://jabber.org/protocol/muc#user"> 
     <status xmlns="" code="0"/> 
    </x> 
    <custom xmlns="my:namespace:custom" user_id="44" manager_id="39" time_stamp="0" 
website_id="0" visitor_name="John" end_time="False"/> 
</message> 

然後你可以輕鬆編寫自己的提供程序解析自定義數據包的擴展和簡單地通過調用

MyExtension customStuff = message.getExtension("my:namespace:custom"); 

您可以檢查出檢索您的自定義對象(由解析器創建)很容易寫你的提供者。

+0

Robin在這裏顯示。你問的是錯誤的問題。但是,請注意您使用的名稱空間URI, 「my:」不是您控制的註冊的URI前綴。如果您不打算制定標準,則可以將您控制的http:// URL用作確保唯一的URI。 –

+0

Thankyou回覆。所以我必須修改服務器端代碼。我無法從Message Stanza中提取屬性。 –

+0

我通過修改源代碼解決了這個問題。有關如何執行此操作的更多詳細信息以下是鏈接。 http://stackoverflow.com/questions/12475122/adding-custom-attributes-in-message-tag-in-xmpp-packet-using-asmack-for-android –

相關問題