我正在使用简单的 xml 库http://simple.sourceforge.net/来反序列化来自套接字输入流的对象。虽然在简单的java应用程序中同样的情况工作正常,但在android中我得到了,之后
MyObject obj = serializer.read(MyObject.class,socket.getInputStream())
org.simpleframework.xml.stream.NodeException:文档没有根元素
这是为什么??
我正在使用简单的 xml 库http://simple.sourceforge.net/来反序列化来自套接字输入流的对象。虽然在简单的java应用程序中同样的情况工作正常,但在android中我得到了,之后
MyObject obj = serializer.read(MyObject.class,socket.getInputStream())
org.simpleframework.xml.stream.NodeException:文档没有根元素
这是为什么??
你还没有收到数据。尝试设置日志并检查数据是否存在
由于我在这里的最后一篇文章被删除了,我的 xml 再次被删除:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<masterclass>
<question className="Cosmos">
<body>What year was the first step on the Moon?</body>
<answer valid="0">1972</answer>
<answer valid="0">1971</answer>
<answer valid="0">1970</answer>
<answer valid="1">1969</answer>
</question>
<question className="Cosmos">
<body>Who were the first men to walk on the Moon?</body>
<answer valid="1">Neil Armstrong</answer>
<answer valid="0">Michael Collins</answer>
<answer valid="0">Eugene Cernan</answer>
<answer valid="1">Edwin Aldrin</answer>
</question>
<question className="History">
<body>When did Christening of Poland took place?</body>
<answer valid="0">1025</answer>
<answer valid="1">966</answer>
<answer valid="0">866</answer>
<answer valid="0">916</answer>
</question>
<question className="Math">
<body>Stone weighs 2kg and half stone. How much stone weighs?</body>
<answer valid="0">2.5kg</answer>
<answer valid="0">3kg</answer>
<answer valid="1">4kg</answer>
<answer valid="0">5kg</answer>
</question>
</masterclass>
以及解组 xml 文件的代码
// download successed
try {
Serializer xmlSer = new Persister();
InputStream fis = new FileInputStream(xmlFilePath);
BufferedReader in = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
MasterClass mc = xmlSer.read(MasterClass.class, in);
MasterClass mc = xmlSer.read(MasterClass.class, str);
Toast.makeText(context.get(), ""+mc.getQuestionList().size(),
Toast.LENGTH_LONG).show();
} catch (Exception ee) {
Log.e("MasterClass", "xml serialize problem", ee);
}
错误在这一行:
MasterClass mc = xmlSer.read(MasterClass.class, in);
当改为这样:
String str = "", s;
while((s = in.readLine()) != null) {
str += s;
//Log.d("MasterClass", str);
}
MasterClass mc = xmlSer.read(MasterClass.class, str);
它开始工作了。不知道为什么……但根据我的研究,这可能与文件在设备上的保存方式有关:
OutputStream fos = new FileOutputStream(outputFile);
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
br.write(content);
br.flush();
br.close();
和 xml 文件编码 - 在记事本 ++ 中设置为 UTF-8 而没有 BOM。
致模组:不知道如何删除所以请在下面删除我的问题,tks :)