简体   繁体   中英

Restart operation if it takes too long

I have an AsyncTask which downloads information from a third party website. This website is not under my control.

The problem is that sometimes I'm getting this information within 2 seconds, but sometimes it may take up to 30-40 seconds.

I know that the issue is with the website itself as I experience the same problem on my desktop in a web browser.

What I'm looking for is a way to cancel the operation if it takes longer than a certain amount of time and try again.

Here is my current code:

protected ArrayList<Card> doInBackground(Void... voids)
{
    Looper.prepare();
    publishProgress("Preparing");
    SomeClass someClass = new SomeClass(this);

    return someClass.downloadInformation();
}

You can try to set timeout and socket connection for your Http request. You see this link: How to set HttpResponse timeout for Android in Java to know how to set them.

And using HttpRequestRetryHandler to enable a custom exception recovery mechanism.

From http://hc.apache.org : "By default HttpClient attempts to automatically recover from I/O exceptions. The default auto-recovery mechanism is limited to just a few exceptions that are known to be safe.

  • HttpClient will make no attempt to recover from any logical or HTTP protocol errors (those derived from HttpException class).
  • HttpClient will automatically retry those methods that are assumed to be idempotent.
  • HttpClient will automatically retry those methods that fail with a transport exception while the HTTP request is still being transmitted to the target server (ie the request has not been fully transmitted to the server)."

Example:

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

public boolean retryRequest(
        IOException exception, 
        int executionCount,
        HttpContext context) {
    if (executionCount >= 5) {
        // Do not retry if over max retry count
        return false;
    }
    if (exception instanceof InterruptedIOException) {
        // Timeout
        return false;
    }
    if (exception instanceof UnknownHostException) {
        // Unknown host
        return false;
    }

    if (exception instanceof SocketTimeoutException) {
        //return true to retry 
        return true;
    }

    if (exception instanceof ConnectException) {
        // Connection refused
        return false;
    }
    if (exception instanceof SSLException) {
        // SSL handshake exception
        return false;
    }
    HttpRequest request = (HttpRequest) context.getAttribute(
            ExecutionContext.HTTP_REQUEST);
    boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); 
    if (idempotent) {
        // Retry if the request is considered idempotent 
        return true;
    }
    return false;
}

};

httpclient.setHttpRequestRetryHandler(myRetryHandler);

See this link: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d4e292 to know more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM