0
我使用rx java與Retrofit,okhttp。 下載內容,而讀取輸入sream收到「套接字關閉」套接字關閉異常android
試圖用像不同的方式後: *正從onNext方法,然後讀取輸入流的響應
我的代碼如下所示:
mApi.getPdf(mUserManager.getUserProfile().getAuthToken())
.compose(mRxUtils.applySchedulers())
.subscribe(new Subscriber<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
mApiErrorManager.handleError(e);
}
@Override
public void onNext(ResponseBody downloadPDFModelResponse) {
downloadFile(downloadPDFModelResponse, myFile);
}
});
下載功能如下:
private void downloadFile(ResponseBody body, File myFile) throws IOException {
int count;
byte data[] = new byte[1024 * 4];
long fileSize = body.contentLength();
InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8);
File outputFile = myFile;
OutputStream output = new FileOutputStream(outputFile);
long total = 0;
long startTime = System.currentTimeMillis();
//int timeCount = 1;
while ((count = bis.read(data)) != -1) { //here getting Socket closed exception
total += count;
int progress = (int) ((total * 100)/fileSize);
mEventBus.post(new DownloadProgress(progress));
output.write(data, 0, count);
}
mEventBus.post(new DownloadProgress(DownloadProgress.COMPLETE));
output.flush();
output.close();
bis.close();
}
你所說的「讀輸入流」意思?你的輸入流是什麼?你遇到了什麼錯誤? – degs
你可以用你的logcat發佈一些代碼嗎? – JediBurrell
可能這會幫助你: [你的ans](http://stackoverflow.com/a/8840042) –