2017-01-11 24 views
1

我的界面:Retrofit2流GET方法的OutOfMemoryError

public interface Downloader { 
    @Streaming @GET Call<ResponseBody> get(@Url final String url); 
} 

我的下載方法:

private void download(final String url, final File zip) throws IOException { 
    AsyncHelper.assertNotMainThread(); 
    final Call<ResponseBody> call = downloaderInstance.get(url); 
    final Response<ResponseBody> response = call.execute(); 

    if (!response.isSuccessful()) { 
     downloadFailed(response.code()); 
     return; 
    } 

    final ResponseBody body = response.body(); 
    downloadedBytes = 0; 
    totalBytes = body.contentLength(); 

    final InputStream in; 
    final OutputStream fout; 

    in = body().byteStream(); 
    fout = new FileOutputStream(zip); 

    int read; 
    while ((read = in.read(BUFFER)) != -1) { 
     fout.write(BUFFER, 0, read); 

     downloadedBytes += read; 
     LOG.d("wrote %d bytes (total: %d)", downloadedBytes, totalBytes); 
    } 

    body.close(); 
    fout.close(); 
} 

這整個事情在後臺線程上運行,我沒有得到與任何日誌行「寫道: x字節(總數:y)「在日誌中,它告訴我,它不是流。

在以前的實現,我直接運行這個流爲ZipInputStream解壓縮上的蒼蠅,我分裂過程成下載解壓步驟以爲ZipInputStream可能是問題所在。該過程在下載期間失敗,這意味着一旦我解決這個問題,我可以恢復到動態解壓縮。

我在做什麼錯?

我也試過final InputStream in = response.body().source().inputStream();,結果相同。我也試過enqueue而不是execute,結果相同。

回答

1

所以,這是令人尷尬的,但如果其他人遇到類似的情況,我會把它留在這裏。

我的問題的答案是:「Nothing」。我做的都對。至少根據上面的代碼。

做錯了,雖然是爲了調試目的我有日誌級別設置爲BODY,這意味着它必須把整個東西放在緩衝區。這也是爲什麼沒有報告遞增的進展。