[英]Convert String to XMPP Stanza using Smack Android
or 要么
Q) Generate XMPP Stanza From String. Q)从String生成XMPP Stanza。
Q) Cast String into XMPP Stanza. Q)将字符串转换为XMPP Stanza。
By using Smack library in Android, 在Android中使用Smack库,
Message message = new Message();
message.setStanzaId("123");
message.setFrom("923442621149");
message.setType(Message.Type.chat);
message.setBody("shanraisshan");
final String msgString = message.toXML().toString();
Log.e("message --->", msgString);
the above code generated following stanza 上面的代码生成后面的节
msgString: msgString:
<message from='923442621149' id='123' type='chat'><body>shanraisshan</body></message>
I have save this msgString into my database. 我已将此msgString保存到我的数据库中。
Now, What I wanted to do is, on retrieving this string back from database 现在,我想要做的是,从数据库中检索此字符串
Since Message is a child class of Stanza , I tried the below code: 由于Message是Stanza的子类,我尝试了以下代码:
Stanza stanza = new Stanza() {
@Override
public CharSequence toXML() {
return msgString;
}
};
Log.e("stanza XML --->", stanza.toXML().toString());
Log.e("stanza getFrom() ->", stanza.getFrom() + ":");
Log.e("stanza getStanzaId() ->", stanza.getStanzaId() + ":");
The Console Log prints follows 控制台日志打印如下
stanza XML --->: <message from='923442621149' id='123' type='chat'><body>shanraisshan</body></message>
stanza getFrom() ->: null:
stanza getStanzaId() ->: OtU0i-29:
I am unable to understand, why 我无法理解,为什么
stanza.toXML().toString() prints the right stanza while stanza.toXML()。toString()打印右侧节
stanza.getFrom() is null instead of 923442621149 stanza.getFrom()为null而不是923442621149
stanza.getStanzaId() is OtU0i-29 instead of 123 stanza.getStanzaId()是OtU0i-29而不是123
Plus, on casting Stanza to Message , produces ClassCastException 另外,在将Stanza转换为Message时,会产生ClassCastException
Message castedMsg = (Message)stanza;
produces 产生
java.lang.ClassCastException:
How can I convert msgString 我该如何转换msgString
msgString = "<message from='923442621149' id='123' type='chat'><body>shanraisshan</body></message>";
into org.jivesoftware.smack.packet. 进入org.jivesoftware.smack.packet。 Message class?
消息类?
After going through Smack Library source code on Github , I found out that the library is using PacketParserUtils.java method's parseStanza() for casting String to Stanza. 在Github上浏览Smack Library源代码后 ,我发现该库正在使用PacketParserUtils.java方法的parseStanza()将String转换为Stanza。
String msgString = "<message from='923442621149' id='123' type='chat'><body>shanraisshan</body></message>";
Message message = (Message)PacketParserUtils.parseStanza(msgString);
Log.e("message XML->", message.toXML().toString());
Log.e("message getFrom()->", message.getFrom() + ":"); //923442621149:
Log.e("message getStanzaId()->", message.getStanzaId() + ":"); //123:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.