序列化器 SimpleXML 仅发送第一行
我在尝试使用 POST 在 Android 和 servlet 之间发送文件 xml 时遇到问题。我正在使用 (Simple XML) 进行序列化。
我的 servlet 对 Android 进行响应:
Serializer serial = new Persister();
OutputStream o = response.getOutputStream();
MyXML myXML = new MyXML();
myXML.setMyElement("test");
serial.write(myXML, o);
它应该像这样将我的 xml 直接发送到客户端,
<MyXML>
<MyElement>test</MyElement>
</MyXML>
但它只发送第一行。然后,在 Android 端会出现此异常,因为它无法获取带有 Element 的第二行。
WARN/System.err(490): org.simpleframework.xml.core.ElementException: Element 'MyElement' does not have a match in class java.lang.Class at line -1
我无法理解为什么当我使用 OutputStream 执行此操作时它只序列化第一行,因为当我保存文件而不发送它时它可以工作,
Serializer serial = new Persister();
File file = new File("MyPath");
MyXML myXML = new MyXML();
myXML.setMyElement("test");
serial.write(myXML, file);
我需要这样做而不是使用字节,只是为了避免设置响应内容长度。
非常感谢,
编辑:添加 MyXML.class
有 MyXML.class,
package part.myApp;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="MyXML")
public class MyXML{
@Element(name="MyElement")
private String a;
public void setMyElement(String a){
this.a=a;
}
public String getMyElement() {
return a;
}
}
谢谢。
I have a problem trying to send a file xml between Android and a servlet with POST. I'm using (Simple XML) for the serializing.
My servlet do the response to Android:
Serializer serial = new Persister();
OutputStream o = response.getOutputStream();
MyXML myXML = new MyXML();
myXML.setMyElement("test");
serial.write(myXML, o);
It's supposed to send my xml directly to the client like this,
<MyXML>
<MyElement>test</MyElement>
</MyXML>
But it only sends the first line . Then, on the Android side gets this exception because it can't get the second line with the Element.
WARN/System.err(490): org.simpleframework.xml.core.ElementException: Element 'MyElement' does not have a match in class java.lang.Class at line -1
I can't understand why it only serialize the first line when i'm doing it with OutputStream because it works when i'm saving on files without sending it,
Serializer serial = new Persister();
File file = new File("MyPath");
MyXML myXML = new MyXML();
myXML.setMyElement("test");
serial.write(myXML, file);
I need to do it like that and not with bytes, just to avoid to set the response content length.
Many thanks,
EDIT: Adding MyXML.class
There is MyXML.class,
package part.myApp;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="MyXML")
public class MyXML{
@Element(name="MyElement")
private String a;
public void setMyElement(String a){
this.a=a;
}
public String getMyElement() {
return a;
}
}
Thanks.
“a”上的私人访问可能会出现问题。 使用 POJO 选项:
让我知道这是否有效为你。
Private access on 'a' might be a problem. Use the POJO options:
Let me know if that works for you.