Java 使用传输编码改进客户端和响应:分块

Java 使用传输编码改进客户端和响应:分块,java,android,http,retrofit2,transfer-encoding,Java,Android,Http,Retrofit2,Transfer Encoding,我正在开发一个Android示例应用程序,从中获取电影列表 其余服务为: http://www.omdbapi.com/?s=star&apikey=d497e644 我正在使用改型来编写客户端 public interface OmdbApi { @Streaming @GET("./") @Headers({"Cache-control: no-cache"}) Call<Search> search(@Query("s") String

我正在开发一个Android示例应用程序,从中获取电影列表

其余服务为:

http://www.omdbapi.com/?s=star&apikey=d497e644
我正在使用改型来编写客户端

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);
}
以及以下错误:

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)
经过一点调查,我发现问题在于响应具有
传输编码:chunked
。我如何解决这个问题


谢谢。

经过进一步调查:

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

最后,我确信代码是有效的,我用来开发应用程序的环境也存在传输编码分块的问题

完整的源代码可在此处获得:

  • 我必须找到到底是什么问题,我现在肯定它不是改装或我的客户端定义或OkHttp

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

    比耶兹

    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)