4

this question is asked before here and here but i dont get answer from there. I'm developing application in which i'm using pubsub and xmpp, for android i'm using smack library. i want to send custom payload to the node and on receiving custom payload how to parse and display it in to list? now i am able to send and receive message but it is just a small example from documentaion. here is my example,

String msg = "room pubsub test";

SimplePayload payload = new SimplePayload("message", "pubsub:test:message", "<message xmlns='pubsub:test:message'><body>" + msg + "</body></message>");

PayloadItem<SimplePayload> item = new PayloadItem<>(null, payload);

node.publish(item);

and when i receive item

node.addItemEventListener(new ItemEventListener() {
           @Override
           public void handlePublishedItems(ItemPublishEvent items) {
                  System.out.println("======================handlePublishedItems==============================");
                  System.out.println(items.getItems());
           }
}

and the output i'm getting is

[org.jivesoftware.smackx.pubsub.PayloadItem | 
Content [<item id='5E277A9C33A58'>
<message xmlns='pubsub:test:message'>
<body xmlns='pubsub:test:message'>room pubsub test</body><
/message>
</item>]]

i want to send custom payload like the time at which message is sent, who have sent this message etc. so, how can i send custom payload? and how to parse it and show it to user?

gaurang
  • 2,217
  • 2
  • 22
  • 44

1 Answers1

4

Send custom payload for time and sender using below code:

 SimplePayload payload = new SimplePayload("message", "pubsub:test:message", "<message xmlns='pubsub:test:message'><body>" + textMessage + "</body><from>"+Sender+"</from><time>"+time+"</time></message>");

For parsing you can use XmlPullParser and parse response using Tag name

pooja londhe
  • 144
  • 2
  • can you give me example for XmlPullParser and parse response using Tag name? – gaurang Sep 27 '17 at 06:44
  • @gaurang you can refer this link for parsing [link]https://github.com/42cc/p2psafety/blob/master/p2psafety-android/p2psafety/src/ua/p2psafety/services/XmppService.java – pooja londhe Sep 27 '17 at 06:56
  • can you tell me how to retrieve id from this pubsub test1user1 – gaurang Sep 28 '17 at 07:15
  • 1
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp = factory.newPullParser(); xpp.setInput(new StringReader(xml)); xpp.getAttributeValue(null,"id"); – pooja londhe Sep 28 '17 at 09:03
  • it return null try { factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); xpp = factory.newPullParser(); xpp.setInput(new StringReader(xml)); String id = xpp.getAttributeValue(null,"id"); Log.d("idddddddddddd",id); } catch (XmlPullParserException e) { e.printStackTrace(); } – gaurang Sep 28 '17 at 10:11