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:com.pacoworks.dereference.dependencies.modules.NetworkModule.java

License:Open Source License

@Provides
@Singleton//  www .  jav  a  2 s  .  co m
OkHttpClient provideOkHttp(final Cache cache, LoggerInterceptor loggerInterceptor,
        StethoInterceptor stethoInterceptor) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(cache);
    okHttpClient.networkInterceptors().add(loggerInterceptor);
    okHttpClient.networkInterceptors().add(stethoInterceptor);
    okHttpClient.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    return okHttpClient;
}

From source file:com.raskasa.dropwizard.okhttp.OkHttpClientBuilder.java

License:Apache License

/** Builds the {@link OkHttpClient}. */
public OkHttpClient build() {
    final OkHttpClient rawClient = new OkHttpClient();
    if (configuration.getConnectTimeout() > 0L) {
        rawClient.setConnectTimeout(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
    }//from  w w  w . j av  a 2 s .c  om
    if (configuration.getReadTimeout() > 0L) {
        rawClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.MILLISECONDS);
    }
    if (configuration.getWriteTimeout() > 0L) {
        rawClient.setWriteTimeout(configuration.getWriteTimeout(), TimeUnit.MILLISECONDS);
    }
    if (configuration.getCacheDir() != null && configuration.getCacheSize() > 0L) {
        rawClient.setCache(new Cache(configuration.getCacheDir(), configuration.getCacheSize()));
    }
    final OkHttpClient client = InstrumentedOkHttpClients.create(registry, rawClient, name);

    // If the environment is present, we tie the client with the server lifecycle
    if (environment != null) {
        environment.lifecycle().manage(new Managed() {
            @Override
            public void start() throws Exception {
            }

            @Override
            public void stop() throws Exception {
                client.getConnectionPool().evictAll();
            }
        });
    }
    return client;
}

From source file:com.squareup.picasso.OkHttp3DownloaderTest.java

License:Apache License

@Test
public void shutdownClosesCache() throws Exception {
    OkHttpClient client = new OkHttpClient();
    Cache cache = new Cache(temporaryFolder.getRoot(), 100);
    client.setCache(cache);
    new OkHttpDownloader(client).shutdown();
    assertThat(cache.isClosed()).isTrue();
}

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 {/*  w ww  . ja v a2 s  .com*/
        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);/*from  ww w  . ja  v  a  2 s.  co  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);
    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);
    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);
    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  .ja v a 2s  . co 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.uni_weimar.m18.backupfestival.network.UnsplashApi.java

License:Apache License

public UnsplashApi() {
    Cache cache = null;/*from   w ww . j  ava2  s .c o  m*/
    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);
}