在 Android 上处理 gzip 内容

发布于 2024-09-26 12:25:10 字数 972 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(2

偷得浮生 2024-10-03 12:25:10

您可以将 url.openStream() 的结果包装在 GZIPInputStream。例如:

InputSource is = new InputSource(new GZIPInputStream(url.openStream()));

要自动检测何时执行此操作,请使用 Content-Encoding HTTP 标头。例如:

URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
if ("gzip".equals(connection.getContentEncoding())) {
  stream = new GZIPInputStream(stream));
}
InputSource is = new InputSource(stream);

You can wrap the result of url.openStream() in a GZIPInputStream. eg:

InputSource is = new InputSource(new GZIPInputStream(url.openStream()));

To auto-detect when to do this, use the Content-Encoding HTTP header. eg:

URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
if ("gzip".equals(connection.getContentEncoding())) {
  stream = new GZIPInputStream(stream));
}
InputSource is = new InputSource(stream);
山色无中 2024-10-03 12:25:10

默认情况下,HttpURLConnection 的此实现请求
服务器使用 gzip 压缩。由于 getContentLength() 返回
传输的字节数,您不能使用该方法来预测如何
可以从 getInputStream() 读取许多字节。相反,请阅读
流直到耗尽:当 read() 返回 -1 时。 Gzip 压缩
可以通过在请求中设置可接受的编码来禁用
标题:

urlConnection.setRequestProperty("Accept-Encoding", "identity");

所以什么都不用做。

By default, this implementation of HttpURLConnection requests that
servers use gzip compression. Since getContentLength() returns the
number of bytes transmitted, you cannot use that method to predict how
many bytes can be read from getInputStream(). Instead, read that
stream until it is exhausted: when read() returns -1. Gzip compression
can be disabled by setting the acceptable encodings in the request
header:

urlConnection.setRequestProperty("Accept-Encoding", "identity");

so nothing need to do.

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