Not able to retrieve the node item from pubsub service in smack

I have a Message object in java wgich I am coverting to payload:
PayloadItem topicPayload=new PayloadItem(msg);
and publishing the topicPayload.

This is my payload:

<Message xmlns="http://jabber.org/protocol/pubsub"><sender>username</sender><receiver>receiver</receiver><password>password123</password><body>Message_body</body></Message>

But when I am trying to get the items from the node using node.getItems(), It gives me following exception:

java.lang.IllegalArgumentException: Could not determine namespace from XML payload
at org.jivesoftware.smack.util.StringUtils.requireNotNullOrEmpty(StringUtils.java:449) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.SimplePayload.(SimplePayload.java:61) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.ItemProvider.parse(ItemProvider.java:62) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.ItemProvider.parse(ItemProvider.java:40) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.Provider.parse(Provider.java:43) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseExtensionElement(PacketParserUtils.java:918) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:1001) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:995) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.EmbeddedExtensionProvider.parse(EmbeddedExtensionProvider.java:101) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.EmbeddedExtensionProvider.parse(EmbeddedExtensionProvider.java:82) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.Provider.parse(Provider.java:43) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseExtensionElement(PacketParserUtils.java:918) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:989) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.addExtensionElement(PacketParserUtils.java:984) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.PubSubProvider.parse(PubSubProvider.java:46) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smackx.pubsub.provider.PubSubProvider.parse(PubSubProvider.java:34) ~[smack-extensions-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.provider.Provider.parse(Provider.java:43) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseIQ(PacketParserUtils.java:616) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.util.PacketParserUtils.parseStanza(PacketParserUtils.java:157) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.AbstractXMPPConnection.parseAndProcessStanza(AbstractXMPPConnection.java:1084) ~[smack-core-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$1300(XMPPTCPConnection.java:155) ~[smack-tcp-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1144) ~[smack-tcp-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$1000(XMPPTCPConnection.java:1092) ~[smack-tcp-4.3.4.jar:4.3.4]
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:1112) ~[smack-tcp-4.3.4.jar:4.3.4]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_222]

Why is namespace required and how to insert it in payload?

  1. Why does your Message have a PubSub namespace? If you use custom, proprietary extension elements, don’t use the protocols namespace but your own instead.
  2. Do you have an implementation of Provider to deserialize your <Message/> class?
  3. If you want to send messages, why do you use PubSub?
  4. If you are only interested in sending simple text content, take a look at SimplePayload.

The namespace is needed, as the receiving device needs to know how to handle the payload, ie. how to deserialize it and know its meaning.

I’d suggest you to take a look at the PubSub specification (especially the examples).

1 Like

Thanks. First point worked for me.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.