Example usage for com.squareup.okhttp Cache Cache

List of usage examples for com.squareup.okhttp Cache Cache

Introduction

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

Prototype

public Cache(File directory, long maxSize) 

Source Link

Usage

From source file:com.steven.android_okhttpfrist.MyDownLoader.java

License:Apache License

/**
 * Create new downloader that uses OkHttp. This will install an image cache into the specified
 * directory.// w  ww . ja va 2  s .  c o  m
 *
 * @param cacheDir The directory in which the cache should be stored
 * @param maxSize  The size limit for the cache.
 */
public MyDownLoader(final File cacheDir, final long maxSize) {
    this(defaultOkHttpClient());
    try {
        client.setCache(new Cache(cacheDir, maxSize));
    } catch (Exception ignored) {
    }
}

From source file:com.tanmay.blip.BlipApplication.java

License:Apache License

@Override
public void onCreate() {
    super.onCreate();
    Fabric.with(this, new Crashlytics());
    mInstance = this;

    int cacheSize = 10 * 1024 * 1024;
    Cache cache = new Cache(getCacheDir(), cacheSize);
    client.setCache(cache);//  www  . ja va 2  s . c  o m

    SharedPrefs.create(this);
    SpeechSynthesizer.create(this);
}

From source file:com.thanksmister.btcblue.data.DataModule.java

License:Apache License

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

    // Install an HTTP cache in the application cache directory.
    try {//from ww w.j  ava2s  . c  om
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (Exception e) {
        Timber.e(e, "Unable to install disk cache.");
    }

    return client;
}

From source file:com.uditgupta.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);// ww w .j  av  a  2s.c o m
    client.setWriteTimeout(10, SECONDS);

    File cacheDir = new File(app.getCacheDir(), "http");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);

    return client;
}

From source file:com.ushahidi.android.presentation.di.modules.AppModule.java

License:Open Source License

private static OkHttpClient createOkHttpClient(Context app) {
    OkHttpClient client = new OkHttpClient();

    File cacheDir = new File(app.getApplicationContext().getCacheDir(), "ushahidi-android-http-cache");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);//from   w  w  w  .  j a  v a2  s. com
    return client;
}

From source file:com.vaporwarecorp.mirror.app.MirrorApplication.java

License:Apache License

private void initializeGlide() {
    Cache cache = new Cache(new File(getCacheDir(), "http"), 25 * 1024 * 1024);

    OkHttpClient mOkHttpClient = new OkHttpClient();
    mOkHttpClient.setCache(cache);//from  ww  w.  j av  a  2 s .c o m
    mOkHttpClient.setConnectTimeout(10, SECONDS);
    mOkHttpClient.setReadTimeout(10, SECONDS);
    mOkHttpClient.setWriteTimeout(10, SECONDS);

    Glide.get(getApplicationContext()).register(GlideUrl.class, InputStream.class,
            new OkHttpUrlLoader.Factory(mOkHttpClient));
}

From source file:com.veaer.gank.request.LineRetrofit.java

License:Open Source License

LineRetrofit() {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(12, TimeUnit.SECONDS);
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    cache = new Cache(FileUtils.getHttpCacheDir(), cacheSize);
    client.setCache(cache);//w ww  .j  a va 2  s . co m
    client.networkInterceptors().add(new CacheInterceptor());

    RestAdapter restAdapter = new RestAdapter.Builder().setClient(new OkClient(client))
            .setEndpoint("http://gank.avosapps.com/api/").setConverter(new GsonConverter(gson)).build();
    service = restAdapter.create(Line.class);
}

From source file:com.whiterabbit.robolectricdependency.client.GitHubClient.java

License:Apache License

public GitHubClient(Context c) {
    OkHttpClient okHttpClient = new OkHttpClient();
    File cacheDir = c.getCacheDir();
    Cache cache = null;/*from w  w  w.  java  2 s. c o  m*/
    cache = new Cache(cacheDir, 1024);
    okHttpClient.setCache(cache);
    Retrofit retrofit = new Retrofit.Builder().baseUrl(API_URL)
            .addConverterFactory(GsonConverterFactory.create()).build();

    // Create an instance of our GitHub API interface.
    mService = retrofit.create(GitHubService.class);

}

From source file:de.dev.eth0.rssreader.app.http.HttpLoaderImpl.java

License:Apache License

/**
 * Builds a cache as configured in preferences
 *
 * @param ctx/*from   w ww .j  a v a 2 s .c o m*/
 * @return
 */
private static Cache getCache(Context ctx) {
    int cacheSize = PreferenceHelper.getCacheSize(ctx);
    Timber.d("Setting cachesize to %d MB", cacheSize);
    if (cacheSize > 0) {
        File cacheDir = ctx.getDir("http_cache", Context.MODE_PRIVATE);
        Timber.d("Using cachedir %s", cacheDir.getPath());
        return new Cache(cacheDir, cacheSize * 1024 * 1024);
    }
    Timber.d("No cache configured");
    return null;
}

From source file:de.uni_weimar.m18.backupfestival.network.UnsplashApi.java

License:Apache License

public UnsplashApi() {
    Cache cache = null;/*www .j  ava  2 s.  c  om*/
    OkHttpClient okHttpClient = null;

    try {
        File cacheDir = new File(FestivalApplication.getContext().getCacheDir().getPath(), "pictures.json");
        cache = new Cache(cacheDir, 10 * 1024 * 1024);
        okHttpClient = new OkHttpClient();
        okHttpClient.setCache(cache);
    } catch (Exception e) {
        // TODO: do something meaningful? - File error handling?
    }

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(ENDPOINT)
            .setClient(new OkClient(okHttpClient)).setConverter(new GsonConverter(gson))
            .setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader("Cache-Control", "public, max-age=" + 60 * 60 * 4);
                }
            }).build();
    mWebService = restAdapter.create(UnsplashService.class);
}