All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.koushikdutta.ion.loader.HttpLoader Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package com.koushikdutta.ion.loader;

import android.text.TextUtils;

import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.http.AsyncHttpResponse;
import com.koushikdutta.async.http.ResponseCacheMiddleware;
import com.koushikdutta.async.http.callback.HttpConnectCallback;
import com.koushikdutta.async.http.libcore.RawHeaders;
import com.koushikdutta.ion.Ion;

/**
 * Created by koush on 5/22/13.
 */
public class HttpLoader extends SimpleLoader {
    @SuppressWarnings("unchecked")
    @Override
    public Future load(Ion ion, AsyncHttpRequest request, final FutureCallback callback) {
        if (!request.getUri().getScheme().startsWith("http"))
            return null;
        return (Future< DataEmitter >)(Future)ion.getHttpClient().execute(request, new HttpConnectCallback() {
            @Override
            public void onConnectCompleted(Exception ex, AsyncHttpResponse response) {
                long length = -1;
                int loadedFrom = LoaderEmitter.LOADED_FROM_NETWORK;
                RawHeaders headers = null;
                AsyncHttpRequest request = null;
                if (response != null) {
                    request = response.getRequest();
                    headers = response.getHeaders().getHeaders();
                    length = response.getHeaders().getContentLength();
                    String servedFrom = response.getHeaders().getHeaders().get(ResponseCacheMiddleware.SERVED_FROM);
                    if (TextUtils.equals(servedFrom, ResponseCacheMiddleware.CACHE))
                        loadedFrom = LoaderEmitter.LOADED_FROM_CACHE;
                    else if (TextUtils.equals(servedFrom, ResponseCacheMiddleware.CONDITIONAL_CACHE))
                        loadedFrom = LoaderEmitter.LOADED_FROM_CONDITIONAL_CACHE;
                }
                callback.onCompleted(ex, new LoaderEmitter(response, length, loadedFrom, headers, request));
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy