SAXParser的Andr​​oid上:意外的结束文件例外中 [英] SaxParser on Android: Unexpected End Of Document Exception

查看:200
本文介绍了SAXParser的Andr​​oid上:意外的结束文件例外中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个的SAXParseException:文件意外结束错误尝试解析在Android上的XML文档时。

I'm getting a "SAXParseException: Unexpected end of document" error when trying to parse an xml document on android.

有问题的文件是从谷歌天气的API,但似乎不管扔有问题的XML文件的同样的错误(只要XML是有效的),所以我怀疑这是用我的方法有问题,而不是该XML。

The document in question is from the google weather api, but it seems to throw the same error regardless of the xml file in question (as long as the xml is valid) so I suspect it's a problem with my approach, rather than the xml.

这是正在做的一个学习锻炼,所以我可能(希望如此)忽略了一些东西明显=)

This is being done as a learning exercise, so I've probably (hopefully) overlooked something obvious =)

我已经运行通过网上验证的XML,它回来为得到很好的形成。 (不能告诉我,如果它是有效的,因为我没有DTD,但我不认为我需要的DTD解析XML)。

I've run the xml through an online validator, and it comes back as being well formed. (Can't tell me if it's valid as I don't have a DTD, but I dont think I need the DTD to parse the xml).

这是我使用的尝试和解析该文件的code:

This is the code that I'm using to try and parse the file:

private void refreshForecast() 
        URL url;
        try {       
                url = new URL( "http://192.168.1.66:8000/google4.xml");

                URLConnection connection = url.openConnection();
                HttpURLConnection httpConnection = (HttpURLConnection)connection; 
                int responseCode = httpConnection.getResponseCode(); 

                if (responseCode == HttpURLConnection.HTTP_OK) { 
                        InputStream in = httpConnection.getInputStream();        
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();

                        // falls over here parsing the xml.
                        Document dom = db.parse(in);    
                }
        } catch (ManyExceptions e) {
        .... 
} 

这会产生错误的XML的缩减版本是:

A cutdown version of the xml that produces the error is:

<?xml version="1.0"?>
<xml_api_reply version="1">
      <weather>
            <forecast_information>
                    <city>Hamilton</city>
            </forecast_information>
      </weather>
</xml_api_reply>

该堆栈跟踪是:

The stacktrace is:

11-20 06:17:24.416: WARN/System.err(406): org.xml.sax.SAXParseException: Unexpected end of document
11-20 06:17:24.416: WARN/System.err(406):     at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:131)
11-20 06:17:24.416: WARN/System.err(406):     at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:110)
11-20 06:17:24.426: WARN/System.err(406):     at com.dave.nzweather.WeatherApp.refreshForecast(WeatherApp.java:159)
11-20 06:17:24.426: WARN/System.err(406):     at com.dave.nzweather.WeatherApp.onCreate(WeatherApp.java:100)
11-20 06:17:24.426: WARN/System.err(406):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 06:17:24.438: WARN/System.err(406):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-20 06:17:24.438: WARN/System.err(406):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-20 06:17:24.446: WARN/System.err(406):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-20 06:17:24.446: WARN/System.err(406):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-20 06:17:24.456: WARN/System.err(406):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 06:17:24.456: WARN/System.err(406):     at android.os.Looper.loop(Looper.java:123)
11-20 06:17:24.456: WARN/System.err(406):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-20 06:17:24.466: WARN/System.err(406):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 06:17:24.466: WARN/System.err(406):     at java.lang.reflect.Method.invoke(Method.java:521)
11-20 06:17:24.466: WARN/System.err(406):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-20 06:17:24.476: WARN/System.err(406):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-20 06:17:24.476: WARN/System.err(406):     at dalvik.system.NativeStart.main(Native Method)
11-20 06:17:24.486: WARN/ROGER(406): org.xml.sax.SAXParseException: Unexpected end of document

在为简洁起见,我已经不包含原始XML,但它只是标准的天气XML从谷歌饲料。

In the interest of brevity, I've not included the original xml, but it's just the standard weather xml from googles feed.

我也尝试了一些完全不同的XML文件(包括<一个样本href="http://www.ibm.com/developerworks/xml/library/x-android/">http://www.ibm.com/developerworks/xml/library/x-android/)他们都给予同样的错误。(他们也都验证以及形成的,当我通过在线XML验证运行它们)。

I've also tried a few completely different xml files, (including the sample from http://www.ibm.com/developerworks/xml/library/x-android/) and they all give the same error. (They also all validate as well formed when I run them through an online xml validator).

这让我觉得,这不是与XML的问题,而是怎么用我试图把它送入解析器。

This makes me think that it's not a problem with the xml, but rather with how I'm trying to feed it into the parser.

干杯

戴夫Smylie

推荐答案

看你的code似乎您试图获取文件掀起了计算机在网络上。你有没有试图打开该网址在浏览器中,以检查它是否真的发送XML文件?

Looking at your code it seems that you are trying to fetch the file off a computer on your network. Have you tried to open that URL in a browser to check if it is really sending the XML file?

另外,您还可以检查与Wireshark的手机得到什么答案。我的猜测是,你根本就没有得到XML文档回来,但一个404错误页面。

Alternatively you can also check with Wireshark what answer your phone gets. My guess is that you simply don't get the XML document back but a 404 error page.

这篇关于SAXParser的Andr​​oid上:意外的结束文件例外中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆