解析异常:在第 1 行第 0 列:未找到元素

发布于 2024-08-30 16:53:24 字数 1210 浏览 2 评论 0原文

我有一个奇怪的问题。我收到以下导致强制关闭的错误:

org.apache.harmony.xml.ExpatParser$ParseException:在第 1 行第 0 列:在 org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:508) 中找不到元素 在 org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467) 在 org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329) 在 org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)

单击“强制关闭”按钮后,将重新创建 Activity 并且解析顺利完成。我在 AsyncTask 的 doInBackground 中使用以下代码片段:

URL serverAddress = new URL(url[0]);

HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setReadTimeout(10000);
connection.connect();

InputStream stream = connection.getInputStream();

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();

xr.parse(new InputSource(stream));  // The line that throws the exception

为什么 Activity 会强制关闭,然后立即运行而不会出现任何问题? BufferedInputStream 会有什么不同吗?我很困惑。 :(

感谢大家抽出时间。

更新:事实证明 HttpURLConnection.getResponseCode() 经常返回 -1,因此 InputStream 可能没有正确设置。

I have a weird issue. I receive the following error that causes a force-close:

org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:508)
at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)

After clicking the Force Close button, the Activity is recreated and the parsing completes without a hitch. I'm using the following code snippet inside doInBackground of an AsyncTask:

URL serverAddress = new URL(url[0]);

HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setReadTimeout(10000);
connection.connect();

InputStream stream = connection.getInputStream();

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();

xr.parse(new InputSource(stream));  // The line that throws the exception

Why would the Activity force-close and then run without any problems immediately after? Would a BufferedInputStream be any different? I'm baffled. :(

Thanks for your time everyone.

Update: It turns out HttpURLConnection.getResponseCode() returns -1 every so often, so the InputStream probably isn't being correctly set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

单调的奢华 2024-09-06 16:53:24
HTTPURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);

这些线条有点奇怪。是HTTPURLConnection还是HttpURLConnection?默认请求方法已经是GET。然而,setDoOutput(true) 将强制其POST

我将替换所有这些行

URLConnection connection = serverAddress.openConnection();

并重试。它可能会返回错误,因为您强制 POST 并且没有向输出(请求正文)写入任何内容。顺便说一句,connection.connect() 已经被 connection.getInputStream() 隐式调用,因此该行也是多余的。

更新:以下用于测试目的是否有效?

BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
    System.out.println(line);
}
reader.close();
HTTPURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);

Those lines are a bit odd. Is it HTTPURLConnection or HttpURLConnection? The default request method is already GET. The setDoOutput(true) will however force it to POST.

I'd replace all of those lines by

URLConnection connection = serverAddress.openConnection();

and retry. It might happen that it returned an error because you forced POST and didn't write anything to the output (the request body). The connection.connect() is by the way already implicitly called by connection.getInputStream(), so that line is superfluous as well.

Update: does the following for testing purposes work?

BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
    System.out.println(line);
}
reader.close();
无法言说的痛 2024-09-06 16:53:24

我不知道你是否解决了这个问题,但我也遇到了同样的问题。这很奇怪,它在模拟器中可以正常工作,但在手机上,它总是给我 xr.parse() 错误。即使当我打印 InputStream 时,它也会为我提供 xml 文档的合法输出。问题似乎出在创建 InputSource 对象上,

这是我修复它的方法:而不是使用 InputStream 创建我刚刚创建的 InputSource直接从 url 字符串输入源。

InputSource a =  new InputSource(url_string);   

其中 url_string 只是包含您的 url 的字符串。不要问我为什么它有效......我真的不喜欢它,因为没有办法检查超时和类似的事情。但它有效,让我知道进展如何!

I dont' know if you fixed this, but I had the same problem. It was weird, it would work fine in the emulator, but then on the phone, it was always giving me the xr.parse() error. Even when I printed the InputStream it would give me legitimate output of the xml document. It seemed the problem was in the creating of the InputSource object

Here's how I fixed it: instead of using InputStream to create your InputSource I just created input source from the url string directly.

InputSource a =  new InputSource(url_string);   

where url_string is just a string with your url. Don't ask me why it works...I dont really like it, as there's no way to check for timeouts and things like that it seems. But it works, let me know how it goes!

苏璃陌 2024-09-06 16:53:24

根据 InputStream javadoc,该方法将阻塞,直到数据可用或遇到 EOF。因此,Socket 的另一端需要关闭它 - 然后 inStream.read() 调用将返回。

如果使用 BufferedReader ,则可以逐行读取。一旦读取了 HTTP 响应中的一行,readLine() 方法就会返回。

Per InputStream javadoc the method will block until the data is available or the EOF is encountered. So, the other side of Socket needs to close it - then the inStream.read() call will return.

If you use BufferedReader , you can read in a line-by-line manner. The readLine() method will return as soon as a line from HTTP response is read.

探春 2024-09-06 16:53:24

在相关的设计说明中,加载 URL 的内容永远不应该强制关闭活动 - 我建议将所有这些放入 AsyncTask 实现中,并在返回 GUI 线程后报告或重试。

On a related design note, loading up contents of a URL should never Force Close an activity - I recommend putting all this into an AsyncTask implementation and report or retry after you are back on the GUI thread.

浊酒尽余欢 2024-09-06 16:53:24

即使我也面临同样的问题。
我首先使用 Scanner 中的 InputStream 来打印它的内容。
然后尝试将其传递到 XML 解析器中。

问题是我没有关闭 Scanner 对象。并在解析器中使用Inputstream

关闭扫描仪对象后,我能够解决这个问题。

Even I faced the same issue.
I was first using the InputStream in Scanner to print the content of it.
And then trying to pass it in XML parser.

The problem was that I was not closing the Scanner object. And using the Inputstream in parser.

After closing the scanner object, I was able to tackle this issue.

失退 2024-09-06 16:53:24

我遇到了同样的问题,但无法理解它,因为我是直接从 InputSource 进行解析。当我修改代码以在 xml 解析之前将结果提取到字符串中时,我发现问题只是拼写错误的 Web 服务方法名称,而该服务报告的错误消息才是杀手。

I ran into the same problem and could make no sense of it because I was parsing directly from the InputSource. When I modified the code to pull the result into a string before the xml parse, I found out the problem was simply a mispelled web service method name and that the error message reported by that service was the killer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文