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”?
加上一个答案的第一部分。 – EagleEye 2015-12-15 12:42:46