gpt4 book ai didi

android - FileAsyncHttpResponseHandler 取消请求

转载 作者:行者123 更新时间:2023-11-29 20:35:12 25 4
gpt4 key购买 nike

我想取消 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com