Android Open Source - VolleyCookie App Controller






From Project

Back to project page VolleyCookie.

License

The source code is released under:

Apache License

If you think the Android project VolleyCookie listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package br.com.newagemobile.volleycookie;
/*from  w w  w. j  ava 2s.  co m*/
import android.app.Application;
import android.text.TextUtils;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.HttpClientStack;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private CloseableHttpClient httpClient;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
//        if (mRequestQueue == null) {
//            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
//        }
//
//        return mRequestQueue;
        if (mRequestQueue == null) {
            httpClient = HttpClients.custom()
                    .setConnectionManager(new PoolingHttpClientConnectionManager())
                    .setDefaultCookieStore(new PersistentCookieStore(getApplicationContext()))
                    .build();
            mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HttpClientStack(httpClient));
        }
        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue, new LruBitmapCache());
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req).setRetryPolicy(new DefaultRetryPolicy(30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); // set timeout
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}




Java Source Code List

br.com.newagemobile.volleycookie.AppController.java
br.com.newagemobile.volleycookie.ApplicationTest.java
br.com.newagemobile.volleycookie.LruBitmapCache.java
br.com.newagemobile.volleycookie.PersistentCookieStore.java
br.com.newagemobile.volleycookie.SerializableCookie.java