【问题标题】:Retrofit and OkHttp gzip decode改造和 OkHttp gzip 解码
【发布时间】:2016-02-26 16:03:12
【问题描述】:

我想将答案作为 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)

根据Jake Whartons comment Content-Encoding: gzip Header 应该告诉 OkHttp 解码正文。

创建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);

gradle 依赖项是:

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

我的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);

【问题讨论】:

  • 您如何检索响应数据?可能你使用了错误的方法。
  • 我在我的 ServiceInterface 中添加了 GET 方法。当我使用 Advanced Rest Client 执行相同的请求时,一切正常。

标签: java retrofit okhttp


【解决方案1】:

替换这个:

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

有了这个:

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

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

【讨论】:

  • 但是如果我省略了"Accept-Encoding: gzip, deflate",,我会得到压缩响应吗?
  • 是的。如果省略 Accept-Encoding 标头,OkHttp 会自动添加自己的并代表您解压缩。
  • 我在 Retrofit 2 上也遇到了类似的问题。但在我的情况下,我将 .header("Accept-Encoding", "gzip, deflate") 添加到请求构建器中。
  • 如果你想禁用 OkHttp 的自动 gzip 功能,只添加一个 Accept-Encoding 头。
  • @DavidCheung 我不必添加这个。根据 Jesse Wilson 的说法,看起来 OkHttp 会自动添加此标头,并且如果服务器返回 gzip 数据流,它就足够聪明地知道并解压缩它。如果服务器没有响应标头中的 gzip 请求,它不会尝试解压缩它。聪明。
【解决方案2】:

在我的情况下,我评论了client(OkHttpClient client) 以下是 sn-p 代码

Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(BuildConfig.END_POINT)
            //.client(client)
            .build()

因为我使用的是LoggingInterceptor,我认为他不会处理与gzip相关的所有事情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 2014-07-12
    • 1970-01-01
    • 2014-05-02
    • 2015-12-28
    • 2014-03-02
    • 2016-06-27
    相关资源
    最近更新 更多