android - FileAsyncHttpResponseHandler 取消请求

标签 android request androidhttpclient android-async-http

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

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(),通过说标签)

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);

关于android - FileAsyncHttpResponseHandler 取消请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31463845/

相关文章:

java - 使用 Robospice 在 Android 上固定证书

java - 制作异步 HTTPGet 类

java - RxJava Android 压缩许多不同类型的请求

android - Facebook SDK 广告在没有 AdSettings.addTestDevice 的情况下无法工作

android - Gradle同步失败-无法确定当前字符

mysql - 如何 $_REQUEST 两个字段并将它们一起发布到另一个页面上?

json - 通过 JSON 对象的 POST 请求

GitHub pull 请求 - "Allow edits by maintainers"

安卓 org.apache.http.client.HttpResponseException : Not Found

android - 动画时比屏幕大的图像被裁剪