1

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.

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
  • The server side developer should make it in JSON format , You can't do it from your android app – Zahidul Islam Jul 08 '16 at 07:18
  • There is no server side development @ZahidulIslam. We just have a database in xampp and Openfire provides everything..!! – Janki Gadhiya Jul 08 '16 at 07:20
  • Do you really want to receive the message in JSON? Or do you just want to extract the JSON within the `

    ` element (`'"Date":"8 Jul 2016","Time":"0:40p.m.","body":" vhklv",...'`) of the message stanza?

    – Flow Jul 08 '16 at 13:27
  • I want to receive JSON..!! – Janki Gadhiya Jul 09 '16 at 03:34
  • @jankigadhiya can you please help me http://stackoverflow.com/questions/41627599/how-to-configure-push-mode-configuration-in-xmpp-with-fcm-refresh-token-using-sm – Vishal Patoliya ツ Jan 18 '17 at 06:04
  • @jankigadhiya http://stackoverflow.com/questions/41630978/how-to-use-smack-4-1-for-how-to-send-info-query-packet-to-xmpp-server help me if possible – Vishal Patoliya ツ Jan 18 '17 at 06:37

2 Answers2

1

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

Community
  • 1
  • 1
User
  • 4,023
  • 4
  • 37
  • 63
  • Actually this is not what I am looking for i don't want to convert XML to JSON but i need my openfire to respond with JSON directly. I will give this a try if it is not possible directly. Thank you for the effort..!! – Janki Gadhiya Jul 08 '16 at 07:51
  • Sorry... But I've already mention the link of original post and I've just pick up the example only. – User Jul 08 '16 at 07:58
  • @Khan can you help me http://stackoverflow.com/questions/41627599/how-to-configure-push-mode-configuration-in-xmpp-with-fcm-refresh-token-using-sm – Vishal Patoliya ツ Jan 18 '17 at 06:13
  • @VishalPatoliyaツ I've seen your question but seriously I've no idea how smack works. – User Feb 08 '17 at 07:38
1

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:

  • if you have a performance issue, you can look maybe for Ejabber
  • If you need a Json, maybe Prosody IM has a plugin
  • If you don't want to broke your head with XmlPullParser, give a try to Bubbler in alternative to Smack.
  • If you just don't feel confident with XML, just implement a "toJson" for each Stanza type so you'll have something like
    (thanking @Khan)

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.

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
MrPk
  • 2,862
  • 2
  • 20
  • 26
  • i haven't written openfire. I have just installed it on my PC. Can please give me something (some setting kind of thing) which will set my openfire to talk in JSON. If that is impossible then i will switch to your alternatives. Thank you..!! – Janki Gadhiya Jul 08 '16 at 09:37
  • it's impossible without a massive rewrite of Openfire to talk in Json, but as I suggested, if the problem it's just to have Json on client, just extend custom Stanzas (message, Iq and presence) to write a Json and use it. – MrPk Jul 08 '16 at 12:41