Question

I'm using asmack the latest version (asmack-android-8-source-0.8.3) in a android project and I have the following code:

connection.addPacketListener(new PacketListener()
{
    @Override
    public void processPacket(Packet p)
    {
        if(p.getPacketID().equals("v3"))
        {
            Log.e("TAG", p.toXML());
        }
    }
}, new IQTypeFilter(IQ.Type.RESULT));


Packet iq = new Packet() 
{
    @Override
    public String toXML() 
    {
        String str = "<iq type='get' id='v3'><getservertime xmlns='urn:xmpp:mrpresence'/></iq>";
        Log.e("TAG", str);
        return str;
    }
};

//sends <iq type='get' id='v3'><getservertime xmlns='urn:xmpp:mrpresence'/></iq>
connection.sendPacket(iq);

in the debugger the response it's ok and this is expected:

<iq type="result" id="v3" to="minimaal@mrserver/Smack">
    <servertime xmlns="urn:xmpp:mrpresence" utc="2013-06-28T11:45:32.380Z" local="2013-06-28T07:45:32.380Z"/>
</iq>

but p.toXML() in the package listner, the tag "servertime" it's missing:

<iq id="v3" to="minimaal@mrserver/Smack" type="result"></iq>

Any suggestions on what I'm doing wrong?

Was it helpful?

Solution

<servertime/> is a custom child element, which is not known to Smack. It is simply ignored when parsing the stanza. You need to make Smack aware of the element, by creating a new PacketExtension which needs to get registered with the ProviderManager.

You can read more about it in Smack's documentation about the Provider Architecture.

OTHER TIPS

I resolved this issue by extending IQ and IQProvider classes:

ServerTime_IQProvider.java

public class ServerTime_IQProvider implements IQProvider
{
    public static final String NAMESPACE = "urn:xmpp:mrpresence";
    public static final String ELEMENT_NAME = "servertime";
    public static final String UTC = "utc";
    public static final String LOCAL = "local";
    private Map<String, String> list = new HashMap<String, String>();

    @Override
    public IQ parseIQ(XmlPullParser parser) throws Exception
    {
        boolean stop = false;
        String name = parser.getName();

        while(false == stop)
        {
            switch (parser.getEventType())
            {
                case XmlPullParser.START_TAG:
                {
                    if(ELEMENT_NAME.equals(name))
                    {
                        list.put(UTC, parser.getAttributeValue("", UTC));
                        list.put(LOCAL, parser.getAttributeValue("", LOCAL));
                    }

                    break;
                }
                case XmlPullParser.END_TAG:
                {
                    stop = ELEMENT_NAME.equals(name);
                    break;
                }
            }
        }

        name = null;
        return new ServerTime_IQ(list);
    }
}

ServerTime_IQ.java

public class ServerTime_IQ extends IQ
{
    public static final String ID = "servertime";
    private Map<String, String> list;

    public ServerTime_IQ(Map<String, String> l)
    {
        list = l;
    }

    @Override
    public String getChildElementXML()
    {
        return null;
    }

    public String getServerTime_UTC()
    {
        return list.get(ServerTime_IQProvider.UTC);
    }

    public String getServerTime_Local()
    {
        return list.get(ServerTime_IQProvider.LOCAL);
    }
}

Then I added the ServerTime provider to the IQProvider's list:

ProviderManager.getInstance().addIQProvider(ServerTime_IQProvider.ELEMENT_NAME, ServerTime_IQProvider.NAMESPACE, new ServerTime_IQProvider());

this will make sure that the xml response from server will be parsed with my custom parser (ServerTime_IQProvider) which is aware of the custom tags, and not with asmack's default parser.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top