Search in sources :

Example 1 with RangeFileAsyncHttpResponseHandler

use of com.loopj.android.http.RangeFileAsyncHttpResponseHandler 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)

Aggregations

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