SMACK XEP-313的实施

有必要在SMACK / ASMACK中附加0313 XEP(0.2) http://xmpp.org/extensions/attic/xep-0313-0.2.html#sect-idp616432 。 我为此创build了PacketExtension。

public class Archive313 implements PacketExtension { static final public String NAMESPACE = "urn:xmpp:mam:tmp"; static final public String ELEMENT = "result"; private String Id; private String Queryid; private String Stamp; private String To; private String From; private String Type; private String Body; public Archive313(String id, String queryid, String stamp, String to, String from, String type, String body) { Id = id; Queryid = queryid; Stamp = stamp; To = to; From = from; Type = type; Body = body; } @Override public String getElementName() { return ELEMENT; } @Override public String getNamespace() { return NAMESPACE; } @Override public String toXML() { return "<" + ELEMENT + " xmlns='" + NAMESPACE + "' queryid='" + Queryid + "' id='" + Id + "'>" + "<forwarded xmlns='urn:xmpp:forward:0'>" + "<delay xmlns='urn:xmpp:delay' stamp='" + Stamp + "'/><message to='" + To + "' from='" + From + "' type='" + Type + "'><body>" + Body + "</body></message></forwarded></result>"; } public static class Archive313Provider implements PacketExtensionProvider { @Override public PacketExtension parseExtension(XmlPullParser xmlPullParser) throws Exception { String tag_name = ""; String id = ""; String queryid = ""; String stamp = ""; String to = ""; String from = ""; String type = ""; String body = ""; while (xmlPullParser.getEventType() != XmlPullParser.END_DOCUMENT) { switch (xmlPullParser.getEventType()) { case XmlPullParser.START_TAG: tag_name = xmlPullParser.getName(); for (int i = 0; i < xmlPullParser.getAttributeCount(); i++) { if (tag_name != null && tag_name.equals("result")) { if (xmlPullParser.getAttributeName(i).equals("queryid")) { queryid = xmlPullParser.getAttributeValue(i); } if (xmlPullParser.getAttributeName(i).equals("id")) { id = xmlPullParser.getAttributeValue(i); } } if (tag_name != null && tag_name.equals("delay")) { if (xmlPullParser.getAttributeName(i).equals("stamp")) { stamp = xmlPullParser.getAttributeValue(i); } } if (tag_name != null && tag_name.equals("message") && xmlPullParser.getAttributeCount() > 2) { if (xmlPullParser.getAttributeName(i).equals("to")) { to = xmlPullParser.getAttributeValue(i); } if (xmlPullParser.getAttributeName(i).equals("from")) { from = xmlPullParser.getAttributeValue(i); } if (xmlPullParser.getAttributeName(i).equals("type")) { type = xmlPullParser.getAttributeValue(i); } } } break; case XmlPullParser.END_TAG: tag_name = xmlPullParser.getName(); break; case XmlPullParser.TEXT: if ("body".equals(tag_name)) { body = xmlPullParser.getText(); } break; default: break; } xmlPullParser.next(); } return new Archive313(id,queryid,stamp,to,from,type,body); } } } ProviderManager pm = ProviderManager.getInstance(); pm.addExtensionProvider("result", "urn:xmpp:mam:tmp", new Archive313.Archive313Provider()); mXMPPConnection.addPacketListener(archiveListener, new PacketExtensionFilter("result", "urn:xmpp:mam:tmp")); private PacketListener archiveListener = new PacketListener() { @Override public void processPacket(Packet packet) { Log.d("archiveListener", packet.toXML()); } }; 

在这个实现中,我不给Packet到archiveListener。

如果我设置filter:

 PacketFilter filter = new PacketFilter() { @Override public boolean accept(Packet packet) { if (packet.toXML().contains("urn:xmpp:mam:tmp")) { return true; } return false; } }; 

我拿到了包裹,但是没有满。

需要:

 <message id='aeb213' to='juliet@capulet.lit/chamber'> <result xmlns='urn:xmpp:mam:tmp' queryid='f27' id='28482-98726-73623'> <forwarded xmlns='urn:xmpp:forward:0'> <delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/> <message to='juliet@capulet.lit/balcony' from='romeo@montague.lit/orchard' type='chat'> <body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body> </message> </forwarded> </result> </message> 

我有:

 <message id='aeb213' to='juliet@capulet.lit/chamber'> <result xmlns='urn:xmpp:mam:tmp' queryid='f27' id='28482-98726-73623'> </result> </message> 

在SMACK日志中,我收到了完整的消息。

Solutions Collecting From Web of "SMACK XEP-313的实施"

它看起来像你的代码

 ProviderManager pm = ProviderManager.getInstance(); pm.addExtensionProvider("result", "urn:xmpp:mam:tmp", new Archive313.Archive313Provider()); 

从不打电话。 检查这个。 通常,这个代码必须放在与xmpp lib工作的主类的静态块中。

要添加任何扩展你必须做三件事情。

  1. 创build一个扩展Extension / ExtensionElement的自定义扩展类。

  2. 创build一个扩展提供程序扩展ExtensionProvider。

  3. 将自定义扩展提供程序添加到提供者pipe

注意:扩展提供程序将根据您的要求(提供程序的onParse()方法)从数据包(XML)parsing您的扩展。 每个自定义扩展必须手动parsing,并需要将自定义提供程序添加到提供程序pipe理器。