【问题标题】:FileAsyncHttpResponseHandler cancel requestFileAsyncHttpResponseHandler 取消请求
【发布时间】:2015-10-06 11:13:37
【问题描述】:

我想在 Android 应用中取消 FileAsyncHttpResponseHandler 请求,但只取消一个(不是全部)。有这些方法可用,但它们都没有取消请求。它会一直持续到文件完全下载为止。

FileAsyncHttpResponseHandler fileAsyncHttpResponseHandler;
...get request...
fileAsyncHttpResponseHandler.deleteTargetFile();
fileAsyncHttpResponseHandler.onCancel();
fileAsyncHttpResponseHandler.sendCancelMessage();

请帮帮我!!

【问题讨论】:

    标签: android request androidhttpclient android-async-http


    【解决方案1】:

    我是上述库的开发者,您基本上有三个选择。

    首先,从创建请求中获取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);
    

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多