2

静的Webサービスクラスを使用してHTTPGETリクエストを呼び出しており、ユーザーが戻るボタンを押したときにリクエスト/AsyncTaskをキャンセルできるようにしたいと考えています。

doInBackgroundでisCancelled()を定期的にチェックします。これは、他のすべての操作で機能しますが、ユーザーがヒットバックしたときにHTTPリクエストが発生している場合、結果が受信されるかタイムアウトになるまで、タスクはキャンセルされません。

WebServices.getDeviceId()への静的呼び出しのため、HTTPクライアントのハンドルがないため、httpclient.getConnectionManager()。shutdown();を使用できません。接続を停止します。誰かが私がこの問題を解決する方法について何かアイデアがありますか?

@Override
    protected Void doInBackground(Void... params) {
        if((mInstance.getDeviceId() == null) && !isCancelled()) {
            String deviceId = WebServices.getDeviceId();
        }

        return null;
    }
4

2 に答える 2

7

http リクエストを中断したい場合は、それへの参照と呼び出しが必要ですyourHttpRequest.abort()

abortHttpRequest()WebServices クラスで、現在の HttpRequest への参照を追加するようなメソッドを追加することはできませんか?

http 要求のキューを管理し、正しい要求を中止する必要がある場合があります。

于 2013-01-14T14:26:43.537 に答える
1

これはSDKからのものです:

 Cancelling a task

A task can be cancelled at any time by invoking cancel(boolean). 
Invoking this method will cause subsequent calls to isCancelled() 
to return true. 
After invoking this method, onCancelled(Object), instead of 
onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. 
To ensure that a task is cancelled as quickly as possible, 
you should always check the return value of isCancelled() periodically from 
doInBackground(Object[]), if possible (inside a loop for instance.)

その後

if (isCancelled()) 
     break;
else
{
   // do your work here
}

これが答えです

于 2013-01-14T14:21:14.637 に答える