我是上述库的开发者,您基本上有三个选择。
首先,从创建请求中获取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);