简体   繁体   中英

How Can I create,send and receive iq packets using smack(java)

I am connected to server(Xmpp) but unable to send and receive packets at my psi client

Here is snippet of my code

POSClientIQ posclientiq = new POSClientIQ();
    posclientiq.connectXMPPServer();
    posclientiq.processMessage();
   }

   public void processMessage()
   {  try{

      final  IQ iq1 = new IQ() {
     public String getChildElementXML() {
      return "<iq type='get' from ='sam'><query xmlns='jabber:iq:roster'></query></iq>";
    }
  };

  iq1.setType(IQ.Type.GET);
 // PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(iq1.getPacketID()));
  connection.sendPacket(iq1);

  System.out.println("Message send");

The getChildElementXML() returns the tag. If you are using Smack then you don't need to write your own IQ implementation unless it is a custom query. For your case, to query the roster use RosterPacket .

If you have a custom query and you would like to use your IQ implementation then:

final IQ iq = new IQ() {
public String getChildElementXML() { 
return "<query xmlns='http://jabber.org/protocol/disco#info'/>"; // here is your query
//this returns "<iq type='get' from='User@YourServer/Resource' id='info1'> <query xmlns='http://jabber.org/protocol/disco#info'/></iq>";
 }};
// set the type
iq.setType(IQ.Type.GET);
// send the request
connection.sendPacket(iq); 

As you can see you have here your custom query and you use Smack to set the rest of your IQ eg setting the type. Please note that Smack fills the "from" for you based on the JID your are logged into.

//To retrieve archive msges from server..

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 in that also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM