android-通过Asmack发送图像时创建消息
作者:互联网
通过XMPP发送图像时遇到一个问题.下面是我完成的代码片段.我不知道代码有什么问题.
Message msg = new Message(emailId, Message.Type.chat);
ImageTransferExtension transfer = new ImageTransferExtension();
transfer.setImageName(mImageUri.getEncodedPath());
transfer.setUserRecipient(emailId);
msg.addExtension(transfer);
这是ImagesTransferExtension代码段
编辑:
public class ImageTransferExtension implements PacketExtension {
private String userRecipient;
private String ftpUrl;
private String httpUrl;
private String id;
private String typeOfMessage;
private String fieldName;
private String duration;
@Override
public String getElementName() {
// TODO Auto-generated method stub
return "x";
}
@Override
public String getNamespace() {
// TODO Auto-generated method stub
return "jabber:client";
}
@Override
public String toXML() {
StringBuilder builder = new StringBuilder();
builder.append("<" + getElementName() + " xmlns=\"" + getNamespace()
+ "\">");
if (ftpUrl != null) {
builder.append("<ftpUrl>").append(ftpUrl).append("</ftpUrl>");
}
if (httpUrl != null) {
builder.append("<httpUrl>").append(httpUrl).append("</httpUrl>");
}
if (typeOfMessage != null) {
builder.append("<typeOfMessage>").append(typeOfMessage)
.append("</typeOfMessage>");
}
if (fieldName != null) {
builder.append("<fieldName>").append(fieldName)
.append("</fieldName>");
}
if (duration != null) {
builder.append("<duration>").append(duration).append("</duration>");
}
builder.append("</" + getElementName() + ">");
return builder.toString();
}
public String getUserRecipient() {
return userRecipient;
}
public void setUserRecipient(String userRecipient) {
this.userRecipient = userRecipient;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我不知道在命名空间和元素中传递什么.我浏览了该文档,但没有找到解决方案.
解决方法:
您可以为自定义数据包使用任何名称和名称空间,但是传出数据包的XML流应该是有效且格式正确的XML文档.
在您的示例中,我看到了未绑定的ftpurl,httpurl,imageName节点.任何好的XML / XMPP解析器都将引发错误,因为流的格式不正确.常见做法是-将所有自定义节点包装在一个顶级节点中并定义名称空间,如下所示:
<i xmlns="my:image:transfer">
<httpurl>http://my.image.jpg</httpurl>
<ftpurl>ftp://my/image.jpg</ftpurl>
</i>
这将导致XML解析器对待所有自定义XML节点,因为它们来自“ my:image:transfer”命名空间,并且XML流将有效.
在“图像传输扩展名”的上下文中,您正在尝试重新发明“带外” XMPP文件传输,它具有著名的XMPP扩展名-http://xmpp.org/extensions/xep-0066.html
具有OOB扩展名的数据包如下所示:
<message from='stpeter@jabber.org/work'
to='MaineBoy@jabber.org/home'>
<body>Yeah, but do you have a license to Jabber?</body>
<x xmlns='jabber:x:oob'>
<url>http://www.jabber.org/images/psa-license.jpg</url>
<desc>Jabber license</desc>
</x>
</message>
这种有效负载的Smack PacketExtension应该看起来像:
public class OutOfBandData implements PacketExtension {
String description;
String url;
@Override
public String getElementName() {
return "x";
}
@Override
public String getNamespace() {
return "jabber:x:oob";
}
@Override
public String toXML() {
StringBuilder builder = new StringBuilder();
builder.("<" + 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;
}
这离您的实现不远,但是其他XMPP客户端理解您的“图像传输”的机会正在增加.
标签:asmack,android,xmpp 来源: https://codeday.me/bug/20191013/1910553.html