android 如何终止异步请求
程序缘LL 2016-03-12 10:35:05 最近在做一个android的项目,里面涉及到一个pdf阅读的功能:我首先把pdf文件下载到本地,我想在下载的过程中终止下载,这要如何实现?
下载的代码在Fragment里面:
private AsyncHttpResponseHandler downHandler = new BinaryHttpResponseHandler(new String[] { "application/pdf"}) {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] binaryData) {
File file = new File(Constants.PDF_SAVE_PAHT);
if (!file.exists()) {
file.mkdirs();
}
File pdfFile = new File(file, pdfName);
try {
FileOutputStream oStream = new FileOutputStream(pdfFile);
oStream.write(binaryData);
oStream.flush();
oStream.close();
Uri uri = Uri.parse(Environment
.getExternalStorageDirectory().getAbsolutePath()+"/kg/download_pdfs/"+pdfName);
Intent intent = new Intent(getActivity(), MuPDFActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
CourseApi.addRecord(uid, courseUnit.getDocId(), adhandler);
} catch (Exception e) {
e.printStackTrace();
Log.i("hck", e.toString());
}
hideWaitDialog();
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error) {
hideWaitDialog();
}
};