如何使用 Smack 4.1 创建、发送和接收 IQ 数据包
Posted
技术标签:
【中文标题】如何使用 Smack 4.1 创建、发送和接收 IQ 数据包【英文标题】:How can I create, send and receive IQ packets using Smack 4.1 【发布时间】:2015-07-24 13:03:31 【问题描述】:我需要将 IQ 数据包发送到 XMPP 服务器以检索消息存档。
如何创建、发送和接收 XMPP IQ 数据包?
谢谢
PS:这个问题似乎与How Can I create,send and receive iq packets using smack(java) 重复,其实不然。近年来,Smack API 发生了很大变化。
【问题讨论】:
【参考方案1】: 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
【讨论】:
以上是关于如何使用 Smack 4.1 创建、发送和接收 IQ 数据包的主要内容,如果未能解决你的问题,请参考以下文章
无法通过 Smack API 使用 openfire 服务器发送/接收消息
使用 Smack Api for Android 发送和接收消息