gpt4 book ai didi

gzip - 安卓 Volley : gzip response

转载 作者:行者123 更新时间:2023-12-04 12:41:22 27 4
gpt4 key购买 nike

我们必须使用什么类型的响应监听器来处理 Android Volley 的 gzip 响应?

如果使用 String 监听器,则响应似乎会丢失其编码。

你如何使用 Volley 处理 gzip 响应?

主要编辑:
HttpUrlConnection 会自动添加 gzip 请求头,如果响应被压缩,它会无缝地解码它并向你呈现响应。所有 gzip 的东西都发生在幕后,您不需要按照我在 gist 中发布的内容来回答这个问题。请参阅此处的文档 http://developer.android.com/reference/java/net/HttpURLConnection.html

其实我贴的答案不应该 使用,因为 gzip 解码非常慢,应该留给 HttpUrlConnection 处理。

这是文档中的确切内容:

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");

最佳答案

所以我想出了如何做到这一点。

基本上,我扩展了 StringRequest 以便它以不同的方式处理网络响应。

您可以使用 GZipInputStream 解析响应字节数组并返回结果字符串。

要点如下:https://gist.github.com/premnirmal/8526542

关于gzip - 安卓 Volley : gzip response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21144992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com