簡體   English   中英

使用Transfer-Encoding改造客戶端和響應:分塊

[英]Retrofit client and response with Transfer-Encoding: chunked

我正在開發一個從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);
}

完整的源代碼在這里

運行應用程序時,我得到以下響應(取自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. 安卓應用
  2. 初級測試

我必須確切地找到問題所在,現在確定不是Retrofit或我的客戶端定義或OkHttp。

希望我的經驗能對其他開發人員有所幫助。

Byez

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM