SaxParser on Android: Unexpected End Of Document Exception

别说谁变了你拦得住时间么 提交于 2019-12-05 01:07:43

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?

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.

Solved (sort of)...

I saw sax.parser could also take in a uri directly (instead of the input stream).

As soon as I tried that, it parsed okay, and the code was a whole lot shorter =)

Thanks for the help.

  String weatherFeed = "http://192.168.1.66:8000/google.xml";
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document dom = db.parse(weatherFeed);

Your problem may have been caused by an encoding issue.

Try creating your inputStream via a ByteArrayInputStream with UTF-8 encoding like below and see if that works. This, (below), is what I had to use when I had a String of XML, but it may still be an issue via the inputestream you are using.

String sData = "..Your XML in here..";
DocumentBuilder db = dbf.newDocumentBuilder(); 
InputStream is = new ByteArrayInputStream(sData.getBytes("UTF-8"));
Document doc = db.parse(is);

This assumes that your XML is has UTF-8 encoding specified like this

<?xml version="1.0" encoding="UTF-8" ?>

Otherwise you should make sure your encoding defined in your InputStream is the same as that defined in your XML.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!