我想在android应用程序中取消FileAsyncHttpResponseHandler请求,但只是一个(不是全部)。有这些方法可用,但它们都不能取消请求。直到文件完全下载完成。
FileAsyncHttpResponseHandler fileAsyncHttpResponseHandler;
...get request...
fileAsyncHttpResponseHandler.deleteTargetFile();
fileAsyncHttpResponseHandler.onCancel();
fileAsyncHttpResponseHandler.sendCancelMessage();
请帮帮我!!
我是上述库的开发人员,您基本上有三个选择。
首先,从创建请求中获取RequestHandle
,并通过该
AsyncHttpClient ahc = new AsyncHttpClient();
RequestHandle rh = ahc.get(context, url, fileResponseHandler);
rh.cancel();
第二,通过归属Context取消请求
AsyncHttpClient ahc = new AsyncHttpClient();
ahc.get(CurrentActivity.this, url, fileResponseHandler);
ahc.cancelRequests(CurrentActivity.this, boolean mayInterruptIfRunning);
第三,目前在1.4.8快照中(可能会在周日作为1.4.8版本发布),放一个TAG到请求(或在ResponseHandler中实现getTag()
,并通过该TAG取消)
String myTag = "my-long-identifier-such-as-uuid";
AsyncHttpClient ahc = new AsyncHttpClient();
RequestHandle rh = ahc.get(context, url, fileResponseHandler);
rh.setTag(myTag);
ahc.cancelRequestsByTAG(myTag, boolean mayInterruptIfRunning);
或通过从响应处理程序
实现getTag()AsyncHttpClient ahc = new AsyncHttpClient();
ahc.get(context, url, new FileAsyncHttpResponseHandler(){
// implement all methods
// override getTag() to identify request associated with this responseHandler
// for example return "url" for ability to cancel by request URL
@Override
public Object getTag() { return url; }
});
ahc.cancelRequestsByTAG(url, boolean mayInterruptIfRunning);