本文介绍了改造和OkHttp gzip解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

REST服务我想以gzip编码的JSON格式使用答案。它提供 Content-Encoding:gzip ,但我的OkHttp不会将其编码为可读文本,因此JSON转换器会抛出异常。

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.

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

根据 Content-Encoding:gzip 标题应告诉OkHttp解码正文。

According to Jake Whartons comment the Content-Encoding: gzip Header should tell OkHttp to decode the body.

创建RestAdapter的代码是:

The code for creating the RestAdapter is:

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依赖项是:

The gradle dependencies are:

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

我的ServiceInterface中的方法:

The method in my 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);


推荐答案

替换为:

@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将负责添加标题和解压缩。

When you provide your own Accept-Encoding header 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.

这篇关于改造和OkHttp gzip解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:24