Reputation: 4510
I am developing an android chatting app with the use of Openfire
as a support server for XMPP and smack library as a android implementation of XMPP.
Things are going well. Till i find this received message from another user. The format is like this :
<message to="rajesh2@peacock-hp" id="0mpqe-10" type="chat" from="rajesh1@peacock-hp/Smack">
<body>{"Date":"8 Jul 2016","Time":"0:40p.m.","body":" vhklv","isMine":true,"msgid":"909-08","receiver":"rajesh2","sender":"rajesh1","senderName":"rajesh1"}</body>
<thread>06ed73bb-21ad-4276-80cb-0ea4fc9d9dfb</thread>
</message>
My listener which is receiving messages :
private class MMessageListener implements ChatMessageListener {
public MMessageListener(Context contxt) {
}
@Override
public void processMessage(final org.jivesoftware.smack.chat.Chat chat,
final Message message) {
Log.i("MyXMPP_MESSAGE_LISTENER", "Xmpp message received: '"
+ message);
}
}
My Question is : Can i receive this message in JSON format instead of XML ??
As I am learning smack and xmpp please guide me if i am wrong at some places. correct me if any one of you find me wrong.
Upvotes: 1
Views: 1269
Reputation: 2930
Json it's not the reply format for Openfire. Of course, you can rewrite all Openfire to "talk" in Json, but to me has no sense.
What I suggest to you:
MyMessage extends Message
public String toJson()
{
JSONObject xmlJSONObj = XML.toJSONObject(this.toXML());
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
}
and you'll be able to use a Json.
Upvotes: 1
Reputation: 4211
You can convert messages to JSON format through a project on Github.
Example :
public class Main {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
Output is:
{
"test": {
"attrib": "moretest",
"content": "Turn this to JSON"
}
}
Credit goes to Quickest way to convert XML to JSON in Java
Upvotes: 1