我正在使用Retrofit進行消費Web服務,至今其效果非常好。但是,Retrofit是否提供了從URL下載視頻的方式?使用Retrofit從Url下載視頻
我檢查了此鏈接,但@Streaming
註釋不再可用。 Retro fit Image download
我正在使用Retrofit進行消費Web服務,至今其效果非常好。但是,Retrofit是否提供了從URL下載視頻的方式?使用Retrofit從Url下載視頻
我檢查了此鏈接,但@Streaming
註釋不再可用。 Retro fit Image download
是的,您可以使用從版本1.6.0開始可用的@Streaming註釋。確保您使用該版本。
正如在changelog中指定的: 新增功能:@Streaming on Response類型將在傳遞之前跳過將主體緩存到字節[]。
interface Api {
@Get("path/to/your/resource")
@Streaming
Response getData();
}
然後,您應該可以直接從InputStream到流像這樣
Response response = api.getData()
InputStream is = response.getBody().in();
// stream your data directly from the InputStream!
請記住,我的例子是爲了簡化同步。
要完成@Miguel兒回答這裏是如何與改造2做到這一點:
interface Service {
@GET("path/to/your/resource")
@Streaming
Call<ResponseBody> getData();
}
Call<ResponseBody> call = service.getData();
try {
InputStream is = call.execute().body().byteStream();
(...)
} catch (IOException e) {...}