Example usage for com.squareup.okhttp OkHttpClient setCache

List of usage examples for com.squareup.okhttp OkHttpClient setCache

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient setCache.

Prototype

public OkHttpClient setCache(Cache cache) 

Source Link

Usage

From source file:app.philm.in.network.PhilmTmdb.java

License:Apache License

@Override
protected RestAdapter.Builder newRestAdapterBuilder() {
    RestAdapter.Builder b = super.newRestAdapterBuilder();

    if (mCacheLocation != null) {
        OkHttpClient client = new OkHttpClient();

        try {//from w  ww  .ja  v a 2  s  .  co m
            File cacheDir = new File(mCacheLocation, UUID.randomUUID().toString());
            Cache cache = new Cache(cacheDir, 1024);
            client.setCache(cache);
        } catch (IOException e) {
            Log.e(TAG, "Could not use OkHttp Cache", e);
        }

        client.setConnectTimeout(Constants.CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        client.setReadTimeout(Constants.READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

        b.setClient(new OkClient(client));
    }

    return b;
}

From source file:augsburg.se.alltagsguide.ServicesModule.java

License:Open Source License

@Singleton
@Provides/*from   w  w w  .  java  2s .co m*/
OkHttpClient okHttpClient(Context context, @Named("cacheDir") File cachedir) {
    Ln.d("okHttpClient is intialized.");
    int cacheSize = 50 * 1024 * 1024; // 50 MiB
    Cache cache = new Cache(cachedir, cacheSize);
    OkHttpClient client = new OkHttpClient();
    client.setCache(cache);
    client.setConnectTimeout(10, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    client.setReadTimeout(30, TimeUnit.SECONDS);
    return client;
}

From source file:com.adamg.materialtemplate.cloud.module.OkHttpModule.java

License:MIT License

/**
 * Creates a default OkHttp client with a disk cache of 10 MB
 *
 * @param ctx a reference to the Client's {@link Context}
 * @return the instance of the OkHttp Client used as the transport layer for API calls
 *///from   w w  w.  jav a  2  s  .co  m
static OkHttpClient createOkHttpClient(final Context ctx) {
    OkHttpClient client = new OkHttpClient();

    // Install an HTTP cache in the application cache directory.
    try {
        final File cacheDir = new File(ctx.getCacheDir(), "https");
        final Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (final IOException e) {
        // Log the error
        Log.e(TAG, "Unable to install disk cache." + Log.getStackTraceString(e));
    }

    return client;
}

From source file:com.animedetour.android.framework.dependencyinjection.module.NetworkModule.java

License:Open Source License

@Provides
@Singleton//from  w  w w.  j ava  2  s .c  om
public OkHttpClient okHttp(Cache cache) {
    OkHttpClient client = new OkHttpClient();
    client.setCache(cache);

    return client;
}

From source file:com.arnaudpiroelle.marvel.api.rest.client.RetrofitHttpClient.java

License:Apache License

private static OkUrlFactory generateDefaultOkUrlFactory() {
    OkHttpClient client = new com.squareup.okhttp.OkHttpClient();
    client.setConnectTimeout(CONNECT_TIMEOUT_SEC, TimeUnit.SECONDS);
    client.setReadTimeout(READ_TIMEOUT_SEC, TimeUnit.SECONDS);

    try {/* ww w.j  av  a  2s  .c om*/
        File cacheDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
        Cache cache = new Cache(cacheDir, 1024);
        client.setCache(cache);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return new OkUrlFactory(client);
}

From source file:com.av.remusic.lastfmapi.RestServiceFactory.java

License:Open Source License

public static <T> T create(final Context context, String baseUrl, Class<T> clazz) {
    final OkHttpClient okHttpClient = new OkHttpClient();

    okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE));
    okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS);

    RequestInterceptor interceptor = new RequestInterceptor() {
        @Override//from w w w  .  j a va2s.c  om
        public void intercept(RequestFacade request) {
            //7-days cache
            request.addHeader("Cache-Control", String.format("max-age=%d,max-stale=%d",
                    Integer.valueOf(60 * 60 * 24 * 7), Integer.valueOf(31536000)));
            request.addHeader("Connection", "keep-alive");
        }
    };

    RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl)
            .setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient));

    return builder.build().create(clazz);

}

From source file:com.baoyz.dribble.AppModule.java

License:Open Source License

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();

    try {//from   w w w.ja v a 2s  .c o  m
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (IOException e) {
        Timber.e(e, "Unable to install disk cache.");
    }

    return client;
}

From source file:com.burhan.udacity.popularmovies.data.DataModule.java

License:Apache License

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(10, SECONDS);
    client.setReadTimeout(10, SECONDS);/*from  w w  w  .  ja  v a  2  s .  c o m*/
    client.setWriteTimeout(10, SECONDS);

    // Install an HTTP cache in the application cache directory.
    File cacheDir = new File(app.getCacheDir(), "http");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);

    return client;
}

From source file:com.droiddevil.myuber.data.DataModule.java

License:Apache License

static OkHttpClient createOkHttpClient(Context context) {
    OkHttpClient client = new OkHttpClient();
    File cacheDir = new File(context.getCacheDir(), "http_cache");
    try {//from   ww w.j ava  2  s .c  o m
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (IOException e) {
        Timber.e(e, "Could not create http cache");
    }
    return client;
}

From source file:com.ephemeraldreams.gallyshuttle.data.DataModule.java

License:Apache License

/**
 * Provide a HTTP client.//from  w  w  w  .  ja v  a  2s . co  m
 *
 * @param application Application to get cache directory from.
 * @return Cached HTTP client.
 */
@Provides
@ApplicationScope
OkHttpClient provideOkHttpClient(Application application) {
    OkHttpClient client = new OkHttpClient();
    File cacheDir = new File(application.getCacheDir(), "http");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);
    return client;
}