Android aSmack - 如何为文件发送制作自定义邮件扩展名?

时间:2015-06-13 06:08:34

标签: android xmpp asmack

我正在尝试为聊天应用程序实现脱机文件扩展名。我正在使用PacketExtension和EmbeddedExtensionProvider来添加和解析自定义扩展。我可以看到在发送消息时添加了值和标签,但是在接收端,这些都没有。我做错了什么?

发送讯息:

public class FileExtension implements PacketExtension {

public static final String ELEMENT = "custom";
public static final String NAMESPACE = "jabber2:x2:oob2";

String description;
String url;

@Override
public String getElementName() {
    return "custom";
}

@Override
public String getNamespace() {
    return "jabber2:x2:oob2";
}

@Override
public String toXML() {

    StringBuilder builder = new StringBuilder();
    builder.append("<" + getElementName() + " xmlns=\"" + getNamespace()
            + "\">");
    if (url != null) {
        builder.append("<url>").append(url).append("</url>");
    }
    if (description != null) {
        builder.append("<desc>").append(description).append("</desc>");
    }
    builder.append("</" + getElementName() + ">");
    return builder.toString();
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getUrl() {
    return url;
}

public void setUrl(String imageUrl) {
    this.url = imageUrl;
}

public static class FileExtensionProvider extends EmbeddedExtensionProvider {

    @Override
    protected PacketExtension createReturnExtension(String currentElement,
            String currentNamespace, Map<String, String> attributeMap,
            List<? extends PacketExtension> content) {

        System.out.println("EmbeddedExtensionProvider");
        return new FileExtension();
    }
}
}

接收消息:

ProviderManager.getInstance().addExtensionProvider(
            FileExtension.ELEMENT, FileExtension.NAMESPACE,
            new FileExtension.FileExtensionProvider());

数据包扩展和提供程序管理器代码:

FileExtension fileExt = new FileExtension("URL");
    fileExt.setDescription("Description");
    fileExt.setUrl("URL");

    msg.addExtension(fileExt);

注册提供商:

FileExtension fileExt = (FileExtension) message
                            .getExtension("jabber2:x2:oob2");

                    Log.i("File Extension description:", fileExt.toXML());

发送消息时:

background_linear.xml

收到消息后:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true" >
        <shape android:shape="rectangle">
             <solid android:color="required_color_here"/>
       </shape>
</item>

2 个答案:

答案 0 :(得分:3)

以下是使用aSmack进行自定义扩展的完整解决方案:

第一,我必须创建PacketExtension:

public class ExtensionFile implements PacketExtension {

    public static final String ELEMENT = "file";
    public static final String NAMESPACE = "file:namespace";

    private String fileURL;
    private String fileSize;

    @Override
    public String getElementName() {
        return ELEMENT;
    }

    @Override
    public String getNamespace() {
        return NAMESPACE;
    }

    @Override
    public String toXML() {

        StringBuilder builder = new StringBuilder();
        builder.append("<" + getElementName() + " xmlns=\"" + getNamespace()
            + "\">");
        if (fileURL != null) {
            builder.append("<fileURL>").append(fileURL).append("</fileURL>");
        }
        if (fileSize != null) {
            builder.append("<fileSize>").append(fileSize).append("</fileSize>");
        }
        builder.append("</" + getElementName() + ">");
        return builder.toString();
    }

    public void setFileURL(String fileURL) {
        this.fileURL = fileURL;
    }

    public String getFileURL() {
        return fileURL;
    }

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

    public String getFileSize() {
        return fileSize;
    }
}

然后提供者解析这个数据包:

public class ExtensionProviderFile implements PacketExtensionProvider {

@Override
public PacketExtension parseExtension(XmlPullParser parser)
        throws Exception {

    ExtensionFile fileExt = new ExtensionFile();

    int eventType = parser.next();

    while (eventType == XmlPullParser.START_TAG) {

        String name = parser.getName();

        switch (parser.getEventType()) {

            case XmlPullParser.START_TAG:
                if (name.equals("fileURL")) {

                    fileExt.setFileURL(parser.nextText());
                } else if (name.equals("fileSize")) {

                    fileExt.setFileSize(parser.nextText());
                }
        }

        eventType = parser.next();
    }

    return fileExt;
}
}

发送带文件扩展名的邮件:

private void sendMessage(String recipient, String message){

    Message msg = new Message(recipient,Message.Type.chat);
    msg.setBody(message);

    //Create extension and set values
    ExtensionFile fileExt = new ExtensionFile();
    fileExt.setFileURL("http://www.example.com/upload_files/"+ abc.jpg);
    fileExt.setFileSize(fileSize);

    //add extension to message
    msg.addExtension(fileExt);

    //send message
    xmppConnection.sendPacket(msg);
}

要接收此消息实施PacketListener并覆盖processPacket()

@Override
public void processPacket(Packet packet) {

    Message message = (Message) packet;

    //Get extension from message
    ExtensionFile extFile = (ExtensionFile) message
                    .getExtension("file:namespace");

    if (extFile != null) {

        //Get values from extension
        String filePath = extFile.getFileURL();
        String fileSize = extFile.getFileSize();

        System.out.println("Message :" + message.getBody() + " File url:" + filePath + " File size:" + fileSize);
    }
}

并在连接到xmpp服务器之前添加此提供:

ProviderManager.getInstance().addExtensionProvider(
            ExtensionFile.ELEMENT, ExtensionFile.NAMESPACE,
            new ExtensionProviderFile());

答案 1 :(得分:1)

在最新版本的Smack 4.1.7中,删除了PacketExtensionProvider(see this)。因此可以在扩展ExtensionElementProvider之后创建ExtensionProviderFile。请检查以下代码 -

public class ExtensionProviderFile extends ExtensionElementProvider {

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

        ChatExtension extensionFile = new ChatExtension();

        int eventType = parser.next();

        while (eventType == XmlPullParser.START_TAG){
            String name = parser.getName();

            switch (parser.getEventType()) {

                case XmlPullParser.START_TAG:

                    if (name.equals("fileName")) {
                        extensionFile.setFileName(parser.nextText());
                    }

                    else if (name.equals("fullName")) {
                        extensionFile.setFullName(parser.nextText());
                    }

            }

            eventType = parser.next();
        }

        return extensionFile;
    }
}

希望这会对某人有所帮助。