如何启用流管理asmack android?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (40)

基本上我为此使用sm:3。问题是,当我设置:

XMPPConnection.DEBUG_ENABLED=true;

这是asmack调试打印的内容:

<r xmlns='urn:xmpp:sm:3'/><message from='test1@pinmessage.com/Smack' to='test1@pinmessage.com' id='CQUe6-5'><received xmlns='urn:xmpp:receipts' id='CQUe6-4'/></message><r xmlns='urn:xmpp:sm:3'/>

这是我从packetFilter获得的内容:

<message id="CQUe6-5" to="test1@pinmessage.com" from="test1@pinmessage.com/Smack"><received xmlns='urn:xmpp:receipts' id='CQUe6-4'/></message>

我还通过添加以下代码为此配置了提供程序管理器:

        addSimplePacketExtension("sm", URN_SM_3);
        addSimplePacketExtension("r", URN_SM_3);
        addSimplePacketExtension("a", URN_SM_3);
        addSimplePacketExtension("enabled", URN_SM_3);
        addSimplePacketExtension("resumed", URN_SM_3);
        addSimplePacketExtension("failed", URN_SM_3);


private static final String URN_SM_3 = "urn:xmpp:sm:3";
    private static void addSimplePacketExtension(final String name, final String namespace) {
        Log.e("adding simple packet extension", namespace+"---"+name);
        ProviderManager.getInstance().addExtensionProvider(name, namespace,
                new PacketExtensionProvider() {
            public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
                StreamHandlingPacket packet = new StreamHandlingPacket(name, namespace);
                Log.e("Stream ahndling packet ","------>"+packet.toXML());
                int attributeCount = parser.getAttributeCount();
                for (int i = 0 ; i < attributeCount ; i++) {
                    packet.addAttribute(parser.getAttributeName(i), parser.getAttributeValue(i));
                }
                return (PacketExtension) packet;
            }
        });
    }


static class StreamHandlingPacket extends Packet {
        private String name;
        private String namespace;
        Map<String, String> attributes;

        StreamHandlingPacket(String name, String namespace) {
            this.name = name;
            this.namespace = namespace;
            attributes = Collections.emptyMap();
        }

        public void addAttribute(String name, String value) {
            if (attributes == Collections.EMPTY_MAP)
                attributes = new HashMap<String, String>();
            attributes.put(name, value);
        }

        public String getAttribute(String name) {
            return attributes.get(name);
        }

        public String getNamespace() {
            return namespace;
        }

        public String getElementName() {
            return name;
        }

        public String toXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<").append(getElementName());

            // TODO Xmlns??
            if (getNamespace() != null) {
                buf.append(" xmlns=\"").append(getNamespace()).append("\"");
            }
            for (String key : attributes.keySet()) {
                buf.append(" ").append(key).append("=\"").append(StringUtils.escapeForXML(attributes.get(key))).append("\"");
            }
            buf.append("/>");
            Log.e("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&","&&&&&&&&&&&&&&&&&&&&&&&&&&&&=>"+buf.toString());
            return buf.toString();
        }

    }

提问于
用户回答回答于

我无法为你提供解决方案,因为我遇到了类似的问题,但我可以从第二个链接告诉你,UnknownPacket扩展了Packet AND实现PacketExtension,这就是为什么你必须在返回语句中将你的StreamHandlingPacket转换为(PacketExtension)你的parseExtension方法。

你能够捕获<enabled xmlns='urn:xmpp:sm:3' etc>XMPP服务器在发送时返回的xml <enable xmlns='urn:xmpp:sm:3' etc>吗?如果是这样,那将是您的问题的开始。

用户回答回答于

我建议使用运行在Android上的Smack 4.1,支持XEP-198流管理。

扫码关注云+社区