gpt4 book ai didi

java - 使用传输编码改造客户端和响应 : chunked

转载 作者:行者123 更新时间:2023-12-02 11:12:26 46 4
gpt4 key购买 nike

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

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。我该如何解决这个问题?

谢谢。

最佳答案

经过更多调查:

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

我最终在另一台机器上尝试,使用 JUnit 测试和 Android 模拟器,没有出现任何问题。

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

完整的源代码可以在这里找到:

  1. android app
  2. junit test

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

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

再见

关于java - 使用传输编码改造客户端和响应 : chunked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50531718/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com