使用Transfer-Encoding改进客户端和响应:chunked [英] Retrofit client and response with Transfer-Encoding: chunked

查看:2979
本文介绍了使用Transfer-Encoding改进客户端和响应:chunked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android示例应用程序,可以从 http://www.omdbapi.com/获取电影列表

I'm working on a Android sample app that get film list from http://www.omdbapi.com/.

REST服务是:

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

我正在使用编写客户端的改造。

I'm using Retrofit to write the client.

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

完整的源代码可用这里

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

When I run the application, I obtain the following response (taken from 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 。我该如何解决这个问题?

After a bit of investigation, I found that the problem is that the response has Transfer-Encoding: chunked. How can I resolve this problem?

谢谢。

推荐答案

经过更多调查:


  • 我删除了@Streaming注释..它们没用。

  • 我做的测试是在防火墙后面的PC上的模拟器。

  • OkHttp多年前就遇到了一些问题,但是现在已经完全控制了传输。

  • On同一台机器,在浏览器中调用相同的URL不会出现任何问题

  • I remove the @Streaming annotations.. they aren't useful.
  • Tests that I done was on an emulator on an PC behind a firewall.
  • OkHttp had some problem about it many years ago, but now chunked transfer is completly managed.
  • On the same machine, invoking the same url in a browser does not present any problems

我终于尝试使用JUnit测试的另一台机器在Android模拟器中,我没有问题。

I finally try on another machine, with a JUnit test and in Android emulator and I had no problem.

最后,我很确定代码是有效的,我用来开发应用程序的环境也是如此transfer-encoding chunked的问题。

At last, I'm pretty sure that code works, so is the environment that I used to develop the app has some problem with transfer-encoding chunked.

这里有完整的源代码:


  1. Android app

  2. <一个href =https://github.com/xcesco/kripton/blob/v4.x/kripton-retrofit-converter/src/test/java/com/abubusoft/kripton/retrofit3/TestOmdb.java\"rel =nofollow noreferrer > junit测试

  1. android app
  2. junit test

我必须找到究竟是什么问题,我现在肯定它不是Retrofit或者我的客户定义或者OkHttp。

I have to find exactly what is the problem, I'm sure now that it not Retrofit or my client definition or OkHttp.

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

I hope my experience can help other developers.

Byez

这篇关于使用Transfer-Encoding改进客户端和响应:chunked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆