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.example.app.di.modules.ApiModule.java

License:Apache License

@Provides
@Singleton/*from  www .ja  v a2s .  c  o m*/
@Named("cache")
OkHttpClient provideOkHttpClient(Cache cache, HttpLoggingInterceptor loggingInterceptor,
        @Named("userAgent") String userAgentValue) {
    OkHttpClient client = new OkHttpClient();
    client.setCache(cache);
    client.interceptors().add(loggingInterceptor);
    client.networkInterceptors().add(new UserAgentInterceptor(userAgentValue));
    client.networkInterceptors().add(new CacheResponseInterceptor());
    client.networkInterceptors().add(new StethoInterceptor());
    return client;
}

From source file:com.geek.geekmall.utils.ImageLoader.java

License:Apache License

/**
 * Create avatar helper//from  w  ww .j a  v a2s . co  m
 *
 * @param context
 */
private ImageLoader(final Context context) {
    this.context = context;

    OkHttpClient client = new OkHttpClient();

    // Install an HTTP cache in the application cache directory.
    File cacheDir = new File(SDCardUtils.getSDCardPath(), "geekmall");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);
    p = new Picasso.Builder(context).downloader(new OkHttpDownloader(client)).build();
    //        p.setIndicatorsEnabled(true);
    //        p.setLoggingEnabled(true);
    bitmapUtils = new BitmapUtils(context, cacheDir.getPath(), 0.15f, DISK_CACHE_SIZE);
    mUniverImageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
            .diskCache(new UnlimitedDiskCache(cacheDir)).diskCacheSize(500 * 1024 * 1024)
            .diskCacheFileCount(500).writeDebugLogs() // Remove for release app
            .build();
    mUniverImageLoader.init(configuration);
}

From source file:com.github.baoti.pioneer.data.DataModule.java

License:Apache License

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

    // 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.github.mobile.util.AvatarLoader.java

License:Apache License

/**
 * Create avatar helper//from   w  w w  .  j  a v a2 s  .c  o m
 *
 * @param context
 */
@Inject
public AvatarLoader(final Context context) {
    this.context = context;

    OkHttpClient client = new OkHttpClient();

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

    p = new Picasso.Builder(context).downloader(new OkHttpDownloader(client)).build();

    float density = context.getResources().getDisplayMetrics().density;
    cornerRadius = CORNER_RADIUS_IN_DIP * density;

    if (avatarSize == 0) {
        avatarSize = getMaxAvatarSize(context);
    }

    // TODO remove this eventually
    // Delete the old cache
    final File avatarDir = new File(context.getCacheDir(), "avatars/github.com");
    if (avatarDir.isDirectory())
        deleteCache(avatarDir);
}

From source file:com.github.shredder121.gh_event_api.handler.AbstractHandlerTest.java

License:Apache License

private static GitHub getGitHub() throws IOException {
    OkHttpClient client = new OkHttpClient();
    client.setCache(new Cache(Paths.get(".", ".cache").toFile(), FileUtils.ONE_MB * 10));
    return new GitHubBuilder().withConnector(new RawGitOkHttpConnector(new OkUrlFactory(client))).build();
}

From source file:com.guoxiaoxing.music.lastfmapi.RestServiceFactory.java

License:Open Source License

public static <T> T createStatic(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  ww  . j av  a2s . co  m
        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.jaspersoft.android.jaspermobile.internal.di.modules.app.AppModule.java

License:Open Source License

@Singleton
@Named("webview_client")
@Provides/*from w  w w. j  av  a 2s  . c  o  m*/
OkHttpClient provideWebViewClient(@ApplicationContext Context context) {
    OkHttpClient client = new OkHttpClient();

    File cacheDir = context.getApplicationContext().getCacheDir();
    File okCache = new File(cacheDir, "ok-cache");
    if (!okCache.exists()) {
        boolean cachedCreated = okCache.mkdirs();

        if (cachedCreated) {
            int cacheSize = 50 * 1024 * 1024;
            Cache cache = new Cache(okCache, cacheSize);
            client.setCache(cache);
        }
    }

    return client;
}

From source file:com.linroid.pushapp.module.DataModule.java

License:Apache License

@Provides
@Singleton// www.  jav a2  s. co  m
OkHttpClient provideOkHttp(Cache cache) {
    OkHttpClient okHttp = new OkHttpClient();
    okHttp.setCache(cache);
    okHttp.setConnectTimeout(30, TimeUnit.SECONDS);
    okHttp.setReadTimeout(30, TimeUnit.SECONDS);
    okHttp.setWriteTimeout(30, TimeUnit.SECONDS);
    okHttp.networkInterceptors().add(new StethoInterceptor());
    return okHttp;
}

From source file:com.nabilhachicha.kc.di.DataModule.java

License:Apache License

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(SOCKET_TIMEOUT, TimeUnit.SECONDS);
    client.setReadTimeout(SOCKET_TIMEOUT, TimeUnit.SECONDS);

    // Install an HTTP cache in the application cache directory.
    try {//from   w w  w .j  av  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) {
        e.printStackTrace();
    }

    return client;
}

From source file:com.nbonnec.mediaseb.di.modules.ApiModule.java

License:Apache License

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

    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    client.setCookieHandler(cookieManager);
    client.networkInterceptors().add(new UserAgentInterceptor(USER_AGENT));

    try {/*from   www . j av  a2s .com*/
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (NullPointerException e) {
        Log.e(TAG, "Unable to initialize OkHttpclient with disk cache", e);
    }

    return client;
}