我想使用DOM方法从Android上的web上解析文件。在Android上处理gzip内容
有问题的代码是:
try {
URL url = new URL("https://www.beatport.com/en-US/xml/content/home/detail/1/welcome_to_beatport");
InputSource is = new InputSource(url.openStream());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
document.getDocumentElement().normalize();
} catch(Exception e) {
Log.v(TAG, "Exception = " + e);
}
但我发现了以下异常:
V/XMLParseTest1( 846):Exception = org.xml.sax.SAXParseException: name expected (position:START_TAG <null>@2:176 in [email protected])
该文件被交给我gzip压缩。我在调试器中检查了is
对象,其长度为6733字节(与响应头文件中的文件内容长度相同),但是如果从浏览器将文件保存到我的硬盘驱动器,它的大小为59114字节。此外,如果我将它上传到我自己的服务器,而服务器在服务它们时不会gzip XML-s,并且设置了URL,那么代码运行得很好。
我猜测会发生什么是Android试图解析gzipped流。
有没有办法先解压缩流?任何其他想法?
看看这个链接http://stackoverflow.com/q/6717165/779408。在那里表示压缩和解压缩方法。 – breceivemail 2013-01-15 10:38:43