2014-06-19 74 views

回答

15

是的,您可以使用從版本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! 

請記住,我的例子是爲了簡化同步。

2

要完成@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) {...}