Search in sources :

Example 1 with Cache

use of com.squareup.okhttp.Cache in project nmid-headline by miao1007.

the class RetrofitUtils method getCachedAdapter.

public static Retrofit getCachedAdapter(String endpoint) {
    Cache cache = null;
    OkHttpClient okHttpClient = null;
    Retrofit adapter;
    try {
        File cacheDir = new File(GlobalContext.getInstance().getCacheDir().getPath(), "pictures.json");
        cache = new Cache(cacheDir, 10 * 1024 * 1024);
        okHttpClient = new OkHttpClient();
        okHttpClient.setCache(cache);
    } catch (Exception e) {
        e.printStackTrace();
    }
    adapter = new Retrofit.Builder().baseUrl(endpoint).addConverterFactory(GsonConverterFactory.create()).client(okHttpClient).build();
    return adapter;
}
Also used : Retrofit(retrofit.Retrofit) OkHttpClient(com.squareup.okhttp.OkHttpClient) File(java.io.File) Cache(com.squareup.okhttp.Cache)

Example 2 with Cache

use of com.squareup.okhttp.Cache in project sbt-android by scala-android.

the class DebugView method setupOkHttpCacheSection.

private void setupOkHttpCacheSection() {
    // Shares the cache with apiClient, so no need to check both.
    Cache cache = client.getCache();
    okHttpCacheMaxSizeView.setText(getSizeString(cache.getMaxSize()));
    refreshOkHttpCacheStats();
}
Also used : Cache(com.squareup.okhttp.Cache)

Example 3 with Cache

use of com.squareup.okhttp.Cache in project sbt-android by scala-android.

the class DataModule method createOkHttpClient.

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(10, SECONDS);
    client.setReadTimeout(10, SECONDS);
    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;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) File(java.io.File) Cache(com.squareup.okhttp.Cache)

Example 4 with Cache

use of com.squareup.okhttp.Cache in project markdown-doclet by Abnaxos.

the class GithubAccessor method createCachedHttpConnector.

private OkHttpConnector createCachedHttpConnector() {
    final File cacheDirectory = getCacheDirectory();
    final Cache cache = new Cache(cacheDirectory, this.cacheSize);
    return new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache)));
}
Also used : OkUrlFactory(com.squareup.okhttp.OkUrlFactory) OkHttpClient(com.squareup.okhttp.OkHttpClient) OkHttpConnector(org.kohsuke.github.extras.OkHttpConnector) File(java.io.File) Cache(com.squareup.okhttp.Cache)

Example 5 with Cache

use of com.squareup.okhttp.Cache in project remusic by aa112901.

the class RestServiceFactory method create.

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
        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);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) RequestInterceptor(retrofit.RequestInterceptor) RestAdapter(retrofit.RestAdapter) Cache(com.squareup.okhttp.Cache)

Aggregations

Cache (com.squareup.okhttp.Cache)12 OkHttpClient (com.squareup.okhttp.OkHttpClient)8 File (java.io.File)8 IOException (java.io.IOException)4 Request (com.squareup.okhttp.Request)3 Response (com.squareup.okhttp.Response)2 DiskLruCache (com.squareup.okhttp.internal.DiskLruCache)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 RequestInterceptor (retrofit.RequestInterceptor)2 RestAdapter (retrofit.RestAdapter)2 OkClient (retrofit.client.OkClient)2 StethoInterceptor (com.facebook.stetho.okhttp.StethoInterceptor)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 ApiEndpoint (com.jakewharton.u2020.data.ApiEndpoint)1 PreferencesUtility (com.naman14.timber.utils.PreferencesUtility)1 OkUrlFactory (com.squareup.okhttp.OkUrlFactory)1 Provides (dagger.Provides)1 Singleton (javax.inject.Singleton)1 OkHttpConnector (org.kohsuke.github.extras.OkHttpConnector)1