How can I create, send and receive iq packages using smack (java)

I am connected to a server (Xmpp) but cannot send or receive packets in my psi client

Here is a 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"); 
+4
source share
3 answers

GetChildElementXML () returns the tag. If you use Smack, you do not need to write your own IQ implementation unless it is a user request. For your case, to request a list, use RosterPacket .

+2
source

If you have a custom request and 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 your own custom query, and you use Smack to set up the rest of your IQ, for example. setting type. Note that Smack fills in the "from" for you based on the JID you are logged into.

+2
source
 //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 
0
source

Source: https://habr.com/ru/post/1299619/


All Articles