【问题标题】:Retrofit client and response with Transfer-Encoding: chunked使用 Transfer-Encoding 改造客户端和响应:chunked
【发布时间】:2018-10-16 20:55:42
【问题描述】:

我正在开发一个从 http://www.omdbapi.com/ 获取电影列表的 Android 示例应用。

REST 服务是:

http://www.omdbapi.com/?s=star&apikey=d497e644

我正在使用 Retrofit 编写客户端。

public interface OmdbApi {
    @Streaming
    @GET("./")
    @Headers({"Cache-control: no-cache"})
    Call<Search> search(@Query("s") String search, @Query("apikey") String apiKey);

    @Streaming
    @GET("./")
    @Headers({"Cache-control: no-cache"})
    Call<FilmDetail> getFilm(@Query("i") String uid, @Query("apikey") String apiKey);
}

完整的源代码可在here获取。

当我运行应用程序时,我得到以下响应(取自 logcat):

OK http://www.omdbapi.com/?s=star&apikey=d497e644 (108ms)
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Cache-Control: public, max-age=86400
Expires: Sat, 26 May 2018 14:28:18 GMT
Last-Modified: Fri, 25 May 2018 05:39:04 GMT
Vary: *, Accept-Encoding
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
CF-Cache-Status: HIT
Server: cloudflare
CF-RAY: 4208b00c817b3db9-MXP
Connection: Keep-Alive

还有以下错误:

java.net.ProtocolException: unexpected end of stream
        at okhttp3.internal.http1.Http1Codec$ChunkedSource.read(Http1Codec.java:455)
        at okio.RealBufferedSource.read(RealBufferedSource.java:47)
okio.RealBufferedSource.exhausted(RealBufferedSource.java:57)
okio.InflaterSource.refill(InflaterSource.java:102)
okio.InflaterSource.read(InflaterSource.java:62)
okio.GzipSource.read(GzipSource.java:80)
okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:237)
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall.execute(RealCall.java:77)
        at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:91)
com.abubusoft.filmfinder.service.repository.FilmRepository.lambda$findFilm$0$FilmRepository(FilmRepository.java:18)
com.abubusoft.filmfinder.service.repository.FilmRepository$$Lambda$0.run(Unknown Source:20)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

经过一番调查,我发现问题是响应中有Transfer-Encoding: chunked。我该如何解决这个问题?

谢谢。

【问题讨论】:

    标签: java android http retrofit2 transfer-encoding


    【解决方案1】:

    经过更多调查:

    • 我删除了@Streaming 注释。它们没有用。
    • 我在防火墙后面的 PC 上的模拟器上进行了测试。
    • OkHttp 很多年前就遇到过一些问题,但现在分块传输已完全托管。
    • 在同一台机器上,在浏览器中调用相同的 URL 不会出现任何问题

    我终于在另一台机器上尝试了 JUnit 测试和 Android 模拟器,我没有问题。

    最后,我确定代码可以正常工作,所以我用来开发应用程序的环境在传输编码分块方面存在一些问题。

    完整的源代码在这里:

    1. android app
    2. junit test

    我必须准确找出问题所在,我现在确定它不是 Retrofit 或我的客户端定义或 OkHttp。

    我希望我的经验可以帮助其他开发者。

    拜兹

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2021-08-18
      • 2012-01-11
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      相关资源
      最近更新 更多