2

I need to send IQ packets to XMPP server for retrieving message archive.

How do i create, send and receive XMPP IQ packets?

Thanks

PS: This question may seem to be duplicate of How Can I create,send and receive iq packets using smack(java), its not. Smack API has been changed a lot in recent years.

Community
  • 1
  • 1
Vinod Patel
  • 430
  • 1
  • 7
  • 19

1 Answers1

0
     MyCustomIQ iq = new MyCustomIQ();
      iq.setType(IQ.Type.set);
     mConnection.sendIqWithResponseCallback(iq, new PacketListener() {
                @Override
                public void processPacket(Packet packet) throws SmackException.NotConnectedException {
                    Log.i("Send IQ with Response", "****** message " + packet);
                }
            }, new ExceptionCallback() {
                @Override
                public void processException(Exception exception) {
                  exception.printStackTrace();
                    Log.i("IO archjieve Exception",""+ exception.getMessage());
                }
            }, 5000);

      mConnection.sendPacket(new Presence(Presence.Type.available));
            PacketTypeFilter filter=new PacketTypeFilter(org.jivesoftware.smack.packet.Message.class);
            PacketListener myListener=new PacketListener(){
                public void processPacket(Packet packet){
                   if(((Message) packet).getType().equals(Message.Type.chat))
                    {
                        ((Message) packet).getBody();
                    }
                    else if(((Message) packet).getType().equals(Message.Type.normal))
                    {
                        DefaultPacketExtension pacExten=PacketUtil.packetExtensionfromCollection(packet.getExtensions(), "result", "urn:xmpp:mam:0");
                        String strMsg=pacExten.getValue("body");
                    }
                }
            }
             ;
           mConnection.addPacketListener(myListener, filter);


    //My Custom IQ
    class MyCustomIQ extends IQ {

            String token;


            protected MyCustomIQ() {
            super("query","urn:xmpp:mam:0");
            }



            @Override
            protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
    //  String queryId = prefix + Long.toString(new AtomicLong().incrementAndGet());
                xml.attribute("queryid",queryId);
                xml.rightAngleBracket();
                return xml;
            }


        }


//You may get the response in PacketListerener sometimes so put debug that also
Jasmine John
  • 873
  • 8
  • 12