I am using Retrofit for consuming web services and so far its great. But does Retrofit provide a way to download videos from URLs?
I checked this link but the @Streaming
annotation is not available anymore.Retro fit Image download
Yes you can use the @Streaming annotation which is available as of version 1.6.0. Make sure you use that version.
As specified in the changelog: New: @Streaming on a Response type will skip buffering the body to a byte[] before delivering.
interface Api {
@Get("path/to/your/resource")
@Streaming
Response getData();
}
You should then be able to stream directly from the InputStream like so
Response response = api.getData()
InputStream is = response.getBody().in();
// stream your data directly from the InputStream!
Keep in mind that my example is synchronous for simplicity.
To complete @Miguel Lavigne answer here is how to do it with Retrofit 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) {...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With