Search code examples
androidxmppsmack

how to use smack 4.1 for how to send info query packet to xmpp server?


how to send info query packet to xmpp server, in other words how to send "..." to server to query some information?

<iq type='set' id='123'>
 <push xmlns='p1:push'>
   <keepalive max="30"/>
   <session duration="60"/>
   <body send="all" groupchat="true" from="jid"/>
   <status type="xa">Text Message when in push mode</status>
   <offline>false</offline>
   <notification>
       <type>applepush</type>
       <id>DeviceToken</id>
   </notification>
   <appid>application1</appid>
 </push>
</iq>

Solution

  • The iq headers and the namespace,element are handled or filled in the xml by smack itself. A sample IQ packet in xml and its implementation by extending IQ packet are given below.

    <iq to='jid@domain.in' id='khz0k-13678' type='get'><elementName xmlns='http://jabber.org/protocol/muc#something'><item affiliation="member"/></elementName></iq>
    
    public class IQGetSomething extends IQ {
    public static final String ELEMENT = "elementName";
    public static final String NAMESPACE = "http://jabber.org/protocol/muc#something";
    String memberType;
    
    public IQGetSomething() {
        super(ELEMENT, NAMESPACE);
        setType(Type.get);
    }
    
    public String getMemberType() {
        return memberType;
    }
    
    public void setMemberType(String memberType) {
        this.memberType = memberType;
    }
    
    
    @Override
    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
        xml.rightAngleBracket();
        xml.append("<item affiliation=\"").escape(memberType).append("\"/>");
        return xml;
    }
    }