java 改造和 OkHttp gzip 解码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33889840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:15:09  来源:igfitidea点击:

Retrofit and OkHttp gzip decode

javaretrofitokhttp

提问by Simon Tenbeitel

The REST service I want to consume answers as a gzipped encoded JSON. It provides the Content-Encoding: gzip, but my OkHttp does not encode it to readable text, so the JSON converter throws an exception.

我想将答案作为 gzip 编码的 JSON 使用的 REST 服务。它提供了Content-Encoding: gzip,但我的 OkHttp 没有将其编码为可读文本,因此 JSON 转换器会引发异常。

---> HTTP GET https://rapla.dhbw-karlsruhe.de/rapla/events?resources=%5B%27rc85dbd6-7d98-4eb7-a7f6-b867213c73d8%27%5D&start=2015-09-01&end=2015-12-31
Accept-Encoding: gzip, deflate
Accept: application/json
Authorization: *not posted*
Content-Type: application/json;charset=utf-8
---> END HTTP (no body)
<--- HTTP 200 https://rapla.dhbw-karlsruhe.de/rapla/events?resources=%5B%27rc85dbd6-7d98-4eb7-a7f6-b867213c73d8%27%5D&start=2015-09-01&end=2015-12-31 (13ms)
Date: Tue, 24 Nov 2015 09:09:10 GMT
Server: Jetty(9.2.2.v20140723)
Expires: Tue, 01 Jan 1980 00:00:00 GMT
Pragma: no-cache
Cache-Control: no-cache, must-revalidate
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Content-Disposition: attachment
Content-Length: 9684
Via: 1.1 rapla.dhbw-karlsruhe.de
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1448356149978
OkHttp-Received-Millis: 1448356149991

????WK?{??J?`k?_??Z????E?p?>3m?WMa?????p?0??<??
... skipped rest of the body
E??>???S???n 
<--- END HTTP (9684-byte body)

According to Jake Whartons commentthe Content-Encoding: gzipHeader should tell OkHttp to decode the body.

根据Jake Whartons 的评论Content-Encoding: gzipHeader 应该告诉 OkHttp 解码正文。

The code for creating the RestAdapter is:

创建 RestAdapter 的代码是:

final RestAdapter adapter = new RestAdapter.Builder()
    .setEndpoint(baseUrl)
    .setClient(new OkClient(new OkHttpClient()))
    .setConverter(new GsonConverter(gson))
    .setLogLevel(RestAdapter.LogLevel.FULL)
    .build();
service = adapter.create(RaplaService.class);

The gradle dependencies are:

gradle 依赖项是:

compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.6.0'

The method in my ServiceInterface:

我的 ServiceInterface 中的方法:

@Headers({
        "Accept-Encoding: gzip, deflate",
        "Content-Type: application/json;charset=utf-8",
        "Accept: application/json"
})
@GET("/events")
List<Event> getEvents(@Header("Authorization") String token, @Query("resources") String resources, @Query("start") String start, @Query("end") String end);

回答by Jesse Wilson

Replace this:

替换这个:

@Headers({
    "Accept-Encoding: gzip, deflate",
    "Content-Type: application/json;charset=utf-8",
    "Accept: application/json"
})

With this:

有了这个:

@Headers({
    "Content-Type: application/json;charset=utf-8",
    "Accept: application/json"
})

When you provide your own Accept-Encodingheader you're instructing OkHttp that you want to do your own decompression. By omitting it, OkHttp will take care of both adding the header and the decompression.

当您提供自己的Accept-Encoding标头时,您是在指示 OkHttp 您要进行自己的解压。通过省略它,OkHttp 将负责添加标头和解压。