Search in sources :

Example 1 with Network

use of com.smartandroid.sa.vl.Network in project SmartAndroidSource by jaychou2012.

the class Volley method newRequestQueue.

/**
     * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
     *
     * @param context A {@link Context} to use for creating the cache dir.
     * @param stack An {@link HttpStack} to use for the network, or null for default.
     * @return A started {@link RequestQueue} instance.
     */
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }
    if (stack == null) {
        if (Build.VERSION.SDK_INT >= 9) {
            stack = new HurlStack();
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }
    Network network = new BasicNetwork(stack);
    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();
    return queue;
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) RequestQueue(com.smartandroid.sa.vl.RequestQueue) Network(com.smartandroid.sa.vl.Network) File(java.io.File)

Aggregations

PackageInfo (android.content.pm.PackageInfo)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Network (com.smartandroid.sa.vl.Network)1 RequestQueue (com.smartandroid.sa.vl.RequestQueue)1 File (java.io.File)1