Stanza for Smack 4.1.4中的自定义扩展元素

时间:2015-10-05 03:29:57

标签: smack asmack

我正在将我的android应用程序从asmack-android库移动到Smack 4.1.4。我在Smack的asmack版本中有一些PacketExtensions,它使用PacketExtension和PacketExtensionProvider类来处理。由于在Smack 4.1.4中不推荐使用PacketExtension,因此我对类和接口ExtensionElement,DataPacketExtension,ExtensionElementProvider,DefaultExtensionElement感到困惑。你们中的任何人都可以给我一个创建扩展的例子,可以添加节和解析... https://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware /smack/packet/DefaultExtensionElement.htmlhttps://www.igniterealtime.org/builds/smack / docs / latest / javadoc / org / jivesoftware /smack/provider/ExtensionElementProvider.html

2 个答案:

答案 0 :(得分:20)

Message message = new Message();
message.setStanzaId("923442621149");
message.setType(Type.chat);
message.setBody("shanraisshan");
Log.e("message --->", message.toXML().toString());

这将产生以下节

<message id='923442621149' type='chat'><body>shanraisshan</body></message>

1。 CUSTOM EXTENSION STANZA TYPE-1

为了生成自定义扩展节

下面的内容
<message id='923442621149' type='chat'><body>shanraisshan</body>
<reply xmlns='shayan:reply' rText='this is custom attribute'/>
</message>

其中回复是自定义扩展程序,其中包含

  1. 元素(回复)
  2. 命名空间(shayan:回复)
  3. 默认的xmpp名称空间列表位于Official XMPP website

    执行以下步骤

    <强> 1。在项目中添加ReplyExtension.java

    <强> ReplyExtension.java

    package com.xmpp.extensions;
    
    import org.jivesoftware.smack.packet.DefaultExtensionElement;
    import org.jivesoftware.smack.packet.ExtensionElement;
    import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
    import org.jivesoftware.smack.util.XmlStringBuilder;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Shayan Rais (http://shanraisshan.com)
     * created on 9/7/2016
     */
    public class ReplyExtension implements ExtensionElement  {
    
        public static final String NAMESPACE = "shayan:reply";
        public static final String ELEMENT = "reply";
    
        String rText = null;
    
        static final String ATTRIBUTE_REPLY_TEXT = "rText";
    
        @Override
        public String getElementName() {
            return ELEMENT;
        }
    
        @Override
        public String getNamespace() {
            return NAMESPACE;
        }
    
        @Override
        public XmlStringBuilder toXML() {
            XmlStringBuilder xml = new XmlStringBuilder(this);
            xml.attribute(ATTRIBUTE_REPLY_TEXT, getReplyText());
            xml.closeEmptyElement();
            return xml;
        }
    
    //__________________________________________________________________________________________________
        public void setReplyText(String _rText) {
            rText = _rText;
        }
    
        public String getReplyText() {
            return rText;
        }
    
    //__________________________________________________________________________________________________
        public static class Provider extends   EmbeddedExtensionProvider<ReplyExtension> {
            @Override
            protected ReplyExtension createReturnExtension(String    currentElement, String currentNamespace, Map<String, String> attributeMap,    List<? extends ExtensionElement> content) {
                ReplyExtension repExt = new ReplyExtension();
                repExt.setReplyText(attributeMap.get(ATTRIBUTE_REPLY_TEXT));
                return repExt;
            }
        }
    }
    

    <强> 2。在您的提供者管理器中注册ReplyExtension

    ProviderManager.addExtensionProvider(ReplyExtension.ELEMENT, ReplyExtension.NAMESPACE, new ReplyExtension.Provider());
    

    发送讯息

    您可以使用以下代码生成 自定义扩展节TYPE-1

    Message message = new Message();
    message.setStanzaId("923442621149");
    message.setType(Type.chat);
    message.setBody("shanraisshan");
    //adding custom reply extension
    ReplyExtension repExt = new ReplyExtension();
    repExt.setReplyText("this is custom attribute");
    message.addExtension(repExt);
    Log.e("message --->", message.toXML().toString());
    

    收到讯息期间

    现在,在接收 自定义扩展程序节 期间,您需要转换扩展程序以获取属性值。

    //check for message with reply extension
    ExtensionElement packetExtension =  message.getExtension(ReplyExtension.NAMESPACE);
    ReplyExtension repExt = (ReplyExtension)packetExtension;
    if(repExt!=null) {
        Log.e("--->", " ---  LOG REPLY EXTENSION ---");
        Log.e("--->", repExt.toXML() + "");
        Log.e("--->", repExt.getReplyText() + ""); //this is custom attribute
    }
    

    <强> _______________________________________________________

    2。 CUSTOM EXTENSION STANZA TYPE-2

    为了生成自定义扩展节

    下面的内容
    <message id='923442621149' type='chat'><body>shanraisshan</body>
    <reply xmlns='shayan:reply'><rText>this is custom attribute</rText></reply>
    </message>
    

    发送讯息

    您可以使用以下代码生成 自定义扩展程序节TYPE-2

    Message message = new Message();
    message.setStanzaId("923442621149");
    message.setType(Type.chat);
    message.setBody("shanraisshan");
    //adding custom reply extension
    DefaultExtensionElement repExt = new DefaultExtensionElement("reply", "shayan:reply");
    repExt.setValue("rText", "this is custom attribute");
    message.addExtension(repExt);
    Log.e("message --->", message.toXML().toString());
    

    收到讯息期间

    DefaultExtensionElement repExt = (DefaultExtensionElement) message.getExtension("shayan:reply");
    if(repExt!=null) {
        Log.e("--->", " ---  LOG REPLY EXTENSION ---");
        Log.e(getClass().getSimpleName(), repExt.getValue("rText"));
    }
    

答案 1 :(得分:3)

最后想出来......这是解决方案......

import org.jivesoftware.smack.packet.DefaultExtensionElement;

public class IM_FileSharing_Extension extends DefaultExtensionElement implements
    IM_Commons_Extension_FileSharing {

private String fileUrl;
private String fileType;
private String base64preview;
private String fileId;
private String fileSize;

public IM_FileSharing_Extension(String fileUrl, String fileType,
        String base64preview, String fileId, String fileSize) {
    super(FILE_TAG, XMLNS);
    this.fileUrl = fileUrl;
    this.fileType = fileType;
    this.base64preview = base64preview;
    this.fileId = fileId;
    this.fileSize = fileSize;
}

@Override
public String toXML() {

    StringBuilder sb = new StringBuilder("<" + FILE_TAG + " xmlns=\""
            + XMLNS + "\" ");
    sb.append(FILE_URL + "=\"" + fileUrl + "\" ");
    sb.append(FILE_ID + "=\"" + fileId + "\" ");
    sb.append(FILE_TYPE + "=\"" + fileType + "\" ");
    sb.append(FILE_SIZE + "=\"" + fileSize + "\">");
    sb.append("<" + FILE_PREVIEW_TAG + ">" + base64preview + "</"
            + FILE_PREVIEW_TAG + ">");
    sb.append("</" + FILE_TAG + ">");

    return sb.toString();
}

public String getFileUrl() {
    return fileUrl;
}

public void setFileUrl(String fileUrl) {
    this.fileUrl = fileUrl;
}

public String getBase64preview() {
    return base64preview;
}

public void setBase64preview(String base64preview) {
    this.base64preview = base64preview;
}

public String getFileId() {
    return fileId;
}

public void setFileId(String fileId) {
    this.fileId = fileId;
}

public String getFileType() {
    return fileType;
}

public void setFileType(String fileType) {
    this.fileType = fileType;
}

public String getFileSize() {
    return fileSize;
}

public void setFileSize(String fileSize) {
    this.fileSize = fileSize;
}

}

上述扩展的提供者如下......

import java.io.IOException;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class IM_FileSharingExtension_Provider extends
    ExtensionElementProvider<IM_FileSharing_Extension> implements
    IM_Commons_Extension_FileSharing {
static final String TAG = "file_extension";

@Override
public IM_FileSharing_Extension parse(XmlPullParser parser, int initialDepth)
        throws XmlPullParserException, IOException, SmackException {

    IM_FileSharing_Extension fileExtension = null;

    boolean stop = false;

    String n = null;

    int evtType;

    String fileUrl = null;
    String fileType = null;
    String fileId = null;
    String fileSize = null;
    while (!stop) {

        evtType = parser.getEventType();
        n = parser.getName();
        Log.d(TAG, "n:" + n + " evt:" + evtType);
        switch (evtType) {

        case XmlPullParser.START_TAG:

            if (FILE_TAG.equals(n)) {

                fileUrl = parser.getAttributeValue("", FILE_URL);

                fileType = parser.getAttributeValue("", FILE_TYPE);

                fileId = parser.getAttributeValue("", FILE_ID);

                fileSize = parser.getAttributeValue("", FILE_SIZE);

                evtType = parser.next();

            }

            if (FILE_PREVIEW_TAG.equals(parser.getName())) {
                String basePreview = parser.nextText();
                fileExtension = new IM_FileSharing_Extension(fileUrl,
                        fileType, basePreview, fileId, fileSize);
            }

            evtType = parser.next();

            break;

        case XmlPullParser.END_TAG:

            if (parser.getName().equals(FILE_TAG)) {
                return fileExtension;
            }
            evtType = parser.next();

        }

    }
    return null;
}

}

应该在提供者管理器中添加如下....

ProviderManager.addExtensionProvider(
            IM_Commons_Extension_FileSharing.FILE_TAG,
            IM_Commons_Extension_FileSharing.XMLNS,
            new IM_FileSharingExtension_Provider());