新建信息同时通过Asmack发送图片 [英] Create message while sending Image through Asmack
问题描述
目前同时通过XMPP.Below发送图像,我面临的一个问题是,我也做了code段。我不知道什么是错的code。
There is an issue that I am facing while sending Images through XMPP.Below is the code snippet that i have done. I don't know what is wrong with the code .
Message msg = new Message(emailId, Message.Type.chat);
ImageTransferExtension transfer = new ImageTransferExtension();
transfer.setImageName(mImageUri.getEncodedPath());
transfer.setUserRecipient(emailId);
msg.addExtension(transfer);
这里是ImagesTransferExtension code段
And here is the ImagesTransferExtension code snippet
编辑:
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;
}
}
我不知道要传递的命名空间和元素是什么。我经历了文档,但没有找到解决办法。
I don't know what to pass in the Namespace and the Element. I went through the document but found no solution.
推荐答案
您可以使用任何名称和命名空间为您的自定义数据包,但你发出的数据包的XML流应该是有效的和格式良好的XML文档。 在你的榜样,我看到绑定ftpurl,httpurl,imageName节点。任何好的XML / XMPP解析器将抛出错误,因为没有良好的信息流。通常的做法是 - 包装所有你自定义节点在一个顶级节点,并定义命名空间,像这样的:
You can use any name and namespace for your custom packets, but XML stream of your outgoing packets should be valid and well-formed XML document. In your example I see unbound ftpurl, httpurl, imageName nodes. Any good XML/XMPP parser will throw error, because your stream is not well-formed. Common practice is - wrap all you custom nodes in one top-level node and define namespace, like this:
<i xmlns="my:image:transfer">
<httpurl>http://my.image.jpg</httpurl>
<ftpurl>ftp://my/image.jpg</ftpurl>
</i>
这将导致XML解析器来治疗,因为他们来自于我的:图片:转移所有的自定义XML节点。命名空间和XML流将是有效的
This will cause XML parser to treat all your custom XML nodes as they come from "my:image:transfer" namespace and XML stream will be valid.
在你的图像传送扩展的背景下 - 你正在试图重塑出带外XMPP的文件传输,这已众所周知的XMPP扩展 - 的 http://xmpp.org/extensions/xep-0066.html
In the context of your "Image transfer extension" - you are trying to reinvent "Out-of-Band" XMPP File Transfer, which has well-known XMPP Extension - http://xmpp.org/extensions/xep-0066.html
与OOB扩展包看起来像:
Packets with OOB extension look like that:
<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>
啪PacketExtension这种类型的有效载荷应该是这样的:
Smack PacketExtension for this type of payload should look like:
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客户了解你的图像传送正在成长。
This is not far from your implementation, but chances where other XMPP clients understand your "Image Transfer" are growing.
这篇关于新建信息同时通过Asmack发送图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!