SMACK XEP-313实施

时间:2015-03-26 15:27:41

标签: java android xmpp smack asmack

需要附上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());
    }
};

在此实现中,我不会将任何Packet提供给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='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日志中,我收到了完整的消息。

2 个答案:

答案 0 :(得分:3)

看起来像你的代码

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

从不打电话。检查一下。通常,此代码必须放在使用xmpp lib的主类的静态块中。

答案 1 :(得分:0)

要添加任何扩展程序,您必须做三件事。

  1. 创建扩展Extension / ExtensionElement的自定义扩展类。

  2. 创建扩展ExtensionProvider的扩展程序提供程序。

  3. 将自定义扩展程序提供程序添加到提供程序管理器。

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