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
,結果相同。