AnsweredAssumed Answered

How to create a custom IQ packet

Question asked by calvin chan on Aug 17, 2015
Latest reply on Aug 17, 2015 by calvin chan

I try to create a custom IQ packet to register push token on Android.

Here is my custom IQ class

class MyCustomIQ extends IQ {
    
    String token;


    protected MyCustomIQ(String token) {
        super("query", "urn:xmpp:registernoti");
        this.token = token;
    }


    @Override
    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        
        xml.rightAngleBracket();
        xml.element("token", token);
        xml.element("devicetpye", "android");


        return xml;
    }
}

 

And here how I create IQ

 

MyCustomIQ iq = new MyCustomIQ(pushToken);

 

After I created the IQ packet, I tried to use `toString()` to show the XML in logcat, but it turn to error below. An I am using Smack API version:4.1.3 (4.1.3 2015-07-15)

java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
        at org.jivesoftware.smack.util.LazyStringBuilder.length(LazyStringBuilder.java:72)
        at org.jivesoftware.smack.util.LazyStringBuilder.toString(LazyStringBuilder.java:100)
        at org.jivesoftware.smack.util.XmlStringBuilder.toString(XmlStringBuilder.java:344)
        at org.jivesoftware.smack.packet.Stanza.toString(Stanza.java:412)
        at org.jivesoftware.smack.AbstractXMPPConnection.callConnectionAuthenticatedListener(AbstractXMPPConnection.java:1165)
        at org.jivesoftware.smack.AbstractXMPPConnection.afterSuccessfulLogin(AbstractXMPPConnection.java:539)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection.afterSuccessfulLogin(XMPPTCPConnection.java:357)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginNonAnonymously(XMPPTCPConnection.java:427)
        at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:457)
        at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:432)

 

Thanks!

Outcomes