在 Android 上处理 gzip 内容
我正在尝试使用 DOM 方法在 Android 上解析来自网络的文件。
有问题的代码是:
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 java.io.InputStreamReader@43ea4538)
该文件正在被压缩后交给我。我已经检查了调试器中的 is
对象,其长度为 6733 字节(与响应标头中文件的内容长度相同),但是如果我将文件从浏览器保存到硬盘驱动器它的大小是 59114 字节。此外,如果我将其上传到我自己的服务器,而该服务器在为它们提供服务并设置 URL 时不会对 XML-s 进行 gzip,则代码运行得很好。
我猜测 Android 会尝试解析 gzip 压缩流。
有没有办法先解压流?还有其他想法吗?
I'm trying to parse a file from the web on Android using the DOM method.
The code in question is:
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);
}
But I'm getting the following exception:
V/XMLParseTest1( 846):Exception = org.xml.sax.SAXParseException: name expected (position:START_TAG <null>@2:176 in java.io.InputStreamReader@43ea4538)
The file is being handed to me gzipped. I've checked the is
object in the debugger and its length is 6733 bytes (the same as the content length of the file in the response headers) however if I save the file to my harddrive from the browser it's size is 59114 bytes. Furthermore if I upload it to my own server which doesn't gzip XML-s when it serves them and set the URL the code runs just fine.
I'm guessing that what happens is that Android tries to parse the gzipped stream.
Is there a way to first unzip the stream? Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 url.openStream() 的结果包装在 GZIPInputStream。例如:
要自动检测何时执行此操作,请使用 Content-Encoding HTTP 标头。例如:
You can wrap the result of
url.openStream()
in a GZIPInputStream. eg:To auto-detect when to do this, use the Content-Encoding HTTP header. eg:
所以什么都不用做。
so nothing need to do.