Search in sources :

Example 1 with HttpUriRequest

use of cz.msebera.android.httpclient.client.methods.HttpUriRequest in project android-async-http by loopj.

the class RangeResponseSample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new RangeFileAsyncHttpResponseHandler(file) {

        @Override
        public void onSuccess(int statusCode, Header[] headers, File file) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (fileSize < 1) {
                boolean supportsRange = false;
                // Cycle through the headers and look for the Content-Length header.
                for (Header header : headers) {
                    String headerName = header.getName();
                    if (CONTENT_LENGTH.equals(headerName)) {
                        fileSize = Long.parseLong(header.getValue());
                    } else if (ACCEPT_RANGES.equals(headerName)) {
                        supportsRange = true;
                    }
                }
                // Is the content length known?
                if (!supportsRange || fileSize < 1) {
                    Toast.makeText(RangeResponseSample.this, "Unable to determine remote file's size, or\nremote server doesn't support ranges", Toast.LENGTH_LONG).show();
                }
            }
            // If remote file size is known, request next portion.
            if (fileSize > 0) {
                debugFileResponse(file);
                // Send a new request for the same resource.
                sendNextRangeRequest();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable e, File file) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, e);
            debugFileResponse(file);
        }

        @Override
        public void updateRequestHeaders(HttpUriRequest uriRequest) {
            // Call super so appending could work.
            super.updateRequestHeaders(uriRequest);
            // Length of the downloaded content thus far.
            long length = file.length();
            // Request the next portion of the file to be downloaded.
            uriRequest.setHeader("Range", "bytes=" + length + "-" + (length + CHUNK_SIZE - 1));
        }

        void debugFileResponse(File file) {
            debugResponse(LOG_TAG, "File size thus far: " + file.length() + " bytes");
        }
    };
}
Also used : HttpUriRequest(cz.msebera.android.httpclient.client.methods.HttpUriRequest) RangeFileAsyncHttpResponseHandler(com.loopj.android.http.RangeFileAsyncHttpResponseHandler) Header(cz.msebera.android.httpclient.Header) File(java.io.File)

Example 2 with HttpUriRequest

use of cz.msebera.android.httpclient.client.methods.HttpUriRequest in project android-async-http by loopj.

the class RetryHandler method retryRequest.

@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
    boolean retry = true;
    Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
    boolean sent = (b != null && b);
    if (executionCount > maxRetries) {
        // Do not retry if over max retry count
        retry = false;
    } else if (isInList(exceptionWhitelist, exception)) {
        // immediately retry if error is whitelisted
        retry = true;
    } else if (isInList(exceptionBlacklist, exception)) {
        // immediately cancel retry if the error is blacklisted
        retry = false;
    } else if (!sent) {
        // for most other errors, retry only if request hasn't been fully sent yet
        retry = true;
    }
    if (retry) {
        // resend all idempotent requests
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        if (currentReq == null) {
            return false;
        }
    }
    if (retry) {
        SystemClock.sleep(retrySleepTimeMS);
    } else {
        exception.printStackTrace();
    }
    return retry;
}
Also used : HttpUriRequest(cz.msebera.android.httpclient.client.methods.HttpUriRequest)

Aggregations

HttpUriRequest (cz.msebera.android.httpclient.client.methods.HttpUriRequest)2 RangeFileAsyncHttpResponseHandler (com.loopj.android.http.RangeFileAsyncHttpResponseHandler)1 Header (cz.msebera.android.httpclient.Header)1 File (java.io.File)1