我正在开发一个从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。如何解决此问题?
谢谢。
发布于 2018-06-01 05:58:25
经过更多调查后:
@Streaming注释..它们没有什么用处。很多年前,我在firewall.OkHttp后面的PC上的模拟器上进行的带来任何问题。
我最终在另一台机器上进行了JUnit测试,并在安卓模拟器中进行了测试,结果没有任何问题。
最后,我确信代码可以工作,我用来开发应用程序的环境也是如此,它在传输编码块方面有一些问题。
完整的源代码可以在这里找到:
我必须找出到底是什么问题,我现在确定它不是翻新或我的客户定义或OkHttp。
我希望我的经验可以帮助其他开发人员。
拜兹
https://stackoverflow.com/questions/50531718
复制相似问题