必须附加0313 XEP(0.2)
http://xmpp.org/extensions/attic/xep-0313-0.2.html#sect-idp616432
进入SMACK / ASMACK。
我为此创建了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());
}
};
在此实现中,我不给ArchiveListener提供任何数据包。
如果我设置过滤器:
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='[email protected]/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='[email protected]/balcony'
from='[email protected]/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='[email protected]/chamber'>
<result xmlns='urn:xmpp:mam:tmp' queryid='f27' id='28482-98726-73623'>
</result>
</message>
在SMACK日志中,我收到了完整的消息。
最佳答案
看起来像你的代码
ProviderManager pm = ProviderManager.getInstance();
pm.addExtensionProvider("result", "urn:xmpp:mam:tmp", new Archive313.Archive313Provider());
从不打电话。检查一下。通常,此代码必须放在与xmpp lib一起工作的主类的静态块中。