3

I'm sending custom XML messages from server to clients. An Android is a client which should know how to handle custom XML messages. The problem with asmack (which I'm using for receiving/sending messages) is that it tries to parse the message itself. An example message of the playerlist is the following:

<html xmlns="http://jabber.org/protocol/xhtml-im">
<body xmlns="http://www.w3.org/1999/xhtml">
  <players xmlns="boxer:players" command="playerlist">
    <player>test1</player>
    <player>test2</player>
  </players>
</body>
</html>

In Android, I'm adding a custom Extenstion Provider with:

ProviderManager.getInstance().addExtensionProvider("html","http://jabber.org/protocol/xhtml-im", new XMLPlayerListProvider());

And the XMLPlayerListProvider

class XMLPlayerListProvider implements PacketExtensionProvider {
  public XMLPlayerListProvider() {}

  public PacketExtension parseExtension(XmlPullParser parser) {
    Log.w("**********HHHHHHHHHHHHH***********", "0");
    boolean stop = false;
    int evtType;
    String n;
    String id = null;
    Log.w("START: "+XmlPullParser.START_TAG, "0");
    while (!stop) {
      try {
       evtType = parser.next();
       n = parser.getName();
       switch (evtType) {
          case XmlPullParser.START_TAG:
             if ("body".equals(n))
                Log.w("BODY"+n.toString(), "0"); 
             else if ("players".equals(n))
                Log.w("PLAYERS", "0");
             else if ("player".equals(n))
                Log.w("PLAYER", "0");
             break;
          case XmlPullParser.END_TAG:
             //Stop parsing when we hit </item>
             stop = "body".equals(n);
             break;
       }
      } catch(Exception e) {
        e.printStackTrace();
      }
    }

    return new XMLPlayerList();
  } 
}



class XMLPlayerList implements PacketExtension {

  public String getElementName() {
    return "aaaaa";
  }

  public String getNamespace() {
    return "aaaaa";
  }

  public String toXML() {
    return "aaaaa";
  }

}

When debugging I'm getting the following output:

W/**********HHHHHHHHHHHHH***********(  654): 0
W/START: 2(  654): 0
W/BODYbody(  654): 0
D/SMACK   (  654): 11:11:58 PM RCV  (1156379984): mand="playerlist">
D/SMACK   (  654): <player>test1</player>
D/SMACK   (  654): <player>test2</player>
D/SMACK   (  654): </players>
D/SMACK   (  654): </body></html><thread>8rOVz0</thread></message><iq
W/PLAYERS (  654): 0
W/PLAYER  (  654): 0
W/PLAYER  (  654): 0
W/RESPONSE: <message id="15" to="eee@localhost" from="admin@localhost" type="chat"><body>
test1
test2

</body><thread>

Here we can see that something strange is going on, but the PacketExtension is indeed getting called. My question now is: how can I change the XMLPlayerListProvider to receive the basic XML message without it being parsed by the default Android packet extension, which I guess is what's outputed as:

test1
test2

That should be:

  <players xmlns="boxer:players" command="playerlist">
    <player>test1</player>
    <player>test2</player>
  </players>
eleanor
  • 1,514
  • 3
  • 19
  • 40
  • 2
    Is there a particular reason that you're wrapping the message in HTML tags and using an existing XMPP namespace? Possibly the XHTML-IM parser is picking up your messages and parsing them? – Christopher Orr Jan 16 '12 at 22:22
  • It was only for testing, I've changed it now to . And I can't define my own class for that, because there's a listener "public void processMessage(Chat c, Message m) { }", which accepts Message as input parameter. – eleanor Jan 18 '12 at 18:55

1 Answers1

1

I would recommend that you not override existing providers by trying to parse existing namespaces, since that will only cause confusion and possible errors (as mentioned by @Christopher)

Try using the EmbeddedExtensionProvider to do your parsing as it takes care of most of the tag handling for you, and simplify your custom xml to what you need only. Which appears to be the players element.

Robin
  • 24,062
  • 5
  • 49
  • 58