receiving custom XML messages
I've spend some time to add smack.providers in the android device, which is picked up by my application just fine. I've added the default iqProviders and extensionProviders, but I've also added my custom extensionProvider, which is the following:
<extensionProvider>
<elementName>players</elementName>
<namespace>boxer:players</namespace>
<className>company.games.boxer.PlayerListProvider</className>
</extensionProvider>
Let me also introduce the PlayerListProvider class, which is currently there just to see if it will get called (which it doesn't) - I'll implement it fully when I'll know it gets called, therefore at least that part of functionality works:
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.provider.EmbeddedExtensionProvider;
import android.util.Log;
@SuppressWarnings("deprecation")
class PlayerListProvider extends EmbeddedExtensionProvider {
protected PacketExtension createReturnExtension(String currentElement, String currentNamespace, Map<String,String> attributeMap, List<? extends PacketExtension> content) {
Log.w("***** AAAAAAAAAAAAAAAAAAAA *******", "0");
return new XMLPlayerList();
}
}
class XMLPlayerList implements PacketExtension {
public String getElementName() {
return "aaaaa";
}
public String getNamespace() {
return "aaaaa";
}
public String toXML() {
return "aaaaa";
}
}
And I'm getting the following message when I run the client android app:
<message to="eee@localhost" type="chat" id="9" from="admin@localhost">
<body>
<players xmlns="boxer:players" command="playerlist">
<player>test1</player>
<player>test2</player>
</players>
</body>
<thread>3P0i00</thread>
</message>
My question now is, why isn't the PlayerListProvider (EmbeddedExtensionProvider) called upon receiving the message. The message contains the tag and has the namespace of boxer:player, as I specified in smack.providers.
Any thoughts?
After reading about similar issues here on SO, I came across this question/answer and this blog post about another way (part 1) (part 2) to implement the custom message sending/receiving.
Have you considered using PacketExtensionProvider instead of EmbeddedExtensionProvider?
It's explained in more detail here, if you are interested in trying it out in place of the EmbeddedExtensionProvider
. It might not be exactly what you are looking for... (it appears like it takes more of a manual-parsing approach,) but it might get your PlayerListProvider
class recognized (via extending the PEPEvent class.)
上一篇: 为什么要选择Java类的接口?
下一篇: 接收定制的XML消息