FileAsyncHttpResponseHandler 取消请求

FileAsyncHttpResponseHandler cancel request

我想在 android 应用程序中取消一个 FileAsyncHttpResponseHandler 请求,但只是一个(不是全部)。有这些方法可用,但其中 none 取消了请求。它会继续进行,直到文件完全下载。

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 snapshot中(1.4.8 release大概周日发布),放一个TAG去request(或者在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);