我对Retrofit不是很熟悉,但它似乎非常强大。 使用普通JSON时一切正常,但是当我尝试使用GZIP压缩的JSON时,就会出现错误:
I/System.out(14883): ------retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
显然,我在日志中看到的奇怪符号无法解析为JSON。
我认为okHttp可以正确处理gzip内容? 我错过了什么吗?
这些JAR包位于我的libs文件夹中:
retrofit-1.6.1.jar
okhttp-2.0.0.jar
gson-2.2.4.jar
这是我发起通话的方式:
public interface OvlService {
@GET("/gziptest.gz")
void getOvls(Callback<OvlWrapper> callback);
}
...
OkHttpClient okHttpClient = new OkHttpClient();
Executor executor = Executors.newCachedThreadPool();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://my.domain") // The base API endpoint.
.setLogLevel(RestAdapter.LogLevel.FULL)
.setExecutors(executor, executor)
.setClient(new OkClient(okHttpClient))
.build();
OvlService ovlService = restAdapter.create(OvlService.class);
ovlService.getOvls(new Callback<OvlWrapper>() {
@Override
public void success(OvlWrapper arg0, Response arg1) {
System.out.println("result: " + arg0.toString());
}
});
result:
行从未显示,但我看到了很多这样的东西T�� �����=�Mk�A�G�4.��@��A��
在RetrofitLog中。我错在哪里,gzip没有被解压缩?谢谢!
编辑:
我在我的测试服务器上尝试了一个gzip文件(server.com/ovl.gz
),并且我也尝试了原始的api服务器(server2.com/api.php?id=ovlgzip
)。相同的结果,但不同的头文件:HTTP/1.1 200 OK
Accept-Ranges: bytes
Connection: close
Content-Length: 477
Content-Type: application/x-gzip
ETag: "2cc40cb-1dd-..."
Last-Modified: Tue, 08 Jul 2014 17:00:08 GMT
OkHttp-Received-Millis: 1404950522590
OkHttp-Response-Source: NETWORK 200
OkHttp-Selected-Transport: http/1.1
OkHttp-Sent-Millis: 1404950522533
Server: Apache
�������������}�Qk�0���...
原始服务器:
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Type: application/json
Keep-Alive: timeout=4, max=1000
OkHttp-Received-Millis: 1404950697627
OkHttp-Response-Source: NETWORK 200
OkHttp-Selected-Transport: http/1.1
OkHttp-Sent-Millis: 1404950697002
Server: Apache
Transfer-Encoding: chunked
X-Powered-By: PHP/5.3.3-7+squeeze19
�������������}�Qk�0���...
Content-Encoding: gzip
的头部信息。 - Jake WhartonGZIPInputStream
,它可以正常工作,但是okHttp显然不能,所以我需要更改服务器部分。 - A2i_