List of usage examples for com.squareup.okhttp Cache Cache
public Cache(File directory, long maxSize)
From source file:de.uni_weimar.m18.backupfestival.network.WpJsonApi.java
License:Apache License
public WpJsonApi() { Cache cache = null;//from ww w. ja va2s .co m OkHttpClient okHttpClient = null; try { File cacheDir = new File(FestivalApplication.getContext().getCacheDir().getPath(), "data.json"); cache = new Cache(cacheDir, 10 * 1024 * 1024); okHttpClient = new OkHttpClient(); okHttpClient.setCache(cache); } catch (Exception e) { Log.e(LOG_TAG, "Error during OkHttpClient init: " + e.getMessage()); // 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(WpJsonService.class); }
From source file:es.upv.grycap.coreutils.fiber.http.Http2Clients.java
License:Apache License
/** * Creates a {@link OkHttpClient} instance and configures it with a cache. The same instance is used across the application to * benefit from a common cache storage and to prevent cache corruption. The cache directory is created private to the user who * runs the application and its content is deleted when the JVM starts its shutting down sequence. * @return A {@link OkHttpClient} instance that can be used everywhere in the application. *///from w w w. j a v a2 s . c o m private static OkHttpClient client() { return COREUTILS_CONTEXT.getClient(OkHttpClient.class, "coreutils-fiber", () -> { final OkHttpClient client = new FiberOkHttpClient(); try { // load default configuration final Config config = new Configurer().loadConfig(null, "coreutils"); final long tmp = config.getBytes("coreutils.clients.http2.cache-size"); final long cacheSize = CACHE_SIZE_RANGE.contains(tmp) ? tmp : CACHE_SIZE_RANGE.lowerEndpoint(); // create the cache directory final File cacheDir = createTempDirectory("coreutils-okhttp-cache-", asFileAttribute(fromString("rwx------"))).toFile(); final Http2ClientShutdownListener shutdownListener = new Http2ClientShutdownListener(cacheDir); COREUTILS_CONTEXT.addShutdownListener(shutdownListener, Http2ClientShutdownListener.class, "coreutils-fiber"); final Cache cache = new Cache(cacheDir, cacheSize); client.setCache(cache); } catch (IOException e) { LOGGER.error("Failed to create directory cache", e); } return client; }); }
From source file:me.calebjones.blogsite.BlogsiteApplication.java
License:Apache License
@Override public void onCreate() { super.onCreate(); mInstance = this; int cacheSize = 50 * 1024 * 1024; Cache cache = new Cache(getCacheDir(), cacheSize); client.setCache(cache);/*from w ww .j av a2s . c o m*/ SharedPrefs.create(this); //TODO: Test this! mInstance.startService(new Intent(mInstance, UpdateCheckService.class)); }
From source file:me.hoangchunghien.popularmovies.data.api.ApiModule.java
License:Apache License
@Provides @Singleton/*from w w w.ja v a2 s . c o m*/ OkHttpClient provideOkHttpClient(@ForApplication Context 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; }
From source file:me.rhespanhol.instalib.ServiceUtils.java
License:Apache License
/** * Returns this apps {@link OkHttpClient} with enabled response cache. * Should be used with API calls./*from w w w.ja v a 2 s . c om*/ */ public static synchronized OkHttpClient getCachingOkHttpClient(Context context) { if (cachingHttpClient == null) { cachingHttpClient = new OkHttpClient(); cachingHttpClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); cachingHttpClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); File cacheDir = createApiCacheDir(context); cachingHttpClient.setCache(new Cache(cacheDir, calculateApiDiskCacheSize(cacheDir))); } return cachingHttpClient; }
From source file:net.soulwolf.structure.component.OkHttpComponent.java
License:Apache License
public static void init(AppStructureConfig.HttpConfig config) { if (config == null) { throw new IllegalArgumentException( "OkHttpComponent HttpConfig initialization parameters can not be NULL!"); }/* w ww .ja v a 2 s .com*/ if (mOkHttpClient == null) { synchronized (OkHttpComponent.class) { if (mOkHttpClient == null) { File cacheDir = new File(AppStructure.getInstance().getCacheDir(), config.getCacheDir()); mOkHttpClient = new OkHttpClient(); mOkHttpClient.setCache(new Cache(cacheDir, config.getCacheSize())); mOkHttpClient.setConnectTimeout(config.getConnectTimeout(), TimeUnit.MILLISECONDS); mOkHttpClient.setReadTimeout(config.getReadTimeout(), TimeUnit.MILLISECONDS); if (AppStructure.isDebug()) { TLog.i(LOG_TAG, "OkHttpComponent initialization!"); } } } } }
From source file:org.amahi.anywhere.server.ApiModule.java
License:Open Source License
@Provides @Singleton/*from w w w . j a v a 2s . c o m*/ Cache provideHttpCache(Context context) { try { File cacheDirectory = new File(context.getCacheDir(), "http-cache"); long cacheSize = BinaryByteUnit.MEBIBYTES.toBytes(10); return new Cache(cacheDirectory, cacheSize); } catch (IOException e) { return null; } }
From source file:org.domokit.oknet.NetworkServiceImpl.java
License:Open Source License
public NetworkServiceImpl(Context context, Core core) { assert core != null; mCore = core;//from ww w. j ava 2s. c o m if (sThreadPool == null) { sThreadPool = Executors.newCachedThreadPool(); } if (sClient == null) { sClient = new OkHttpClient(); try { int cacheSize = 10 * 1024 * 1024; // 10 MiB File cacheDirectory = new File(context.getCacheDir(), "ok_http_cache"); Cache cache = new Cache(cacheDirectory, cacheSize); sClient.setCache(cache); } catch (IOException e) { Log.e(TAG, "Unable to create HTTP cache", e); } } }
From source file:org.icgc.dcc.dev.github.GithubConfig.java
License:Open Source License
@Bean public HttpConnector connector() throws IOException { val maxSize = 10 * 1024 * 1024; // 10MB cache val cache = new Cache(cacheDir, maxSize); return new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))); }
From source file:org.jamienicol.episodes.EpisodesApplication.java
License:Open Source License
@Override public void onCreate() { super.onCreate(); instance = this; // ensure the default settings are initialised at first launch, // rather than waiting for the settings screen to be opened. // do this before anything that needs these settings is instantiated. PreferenceManager.setDefaultValues(this, R.xml.preferences, false); autoRefreshHelper = AutoRefreshHelper.getInstance(this); httpClient = new OkHttpClient(); try {/* www .j a v a2 s. c om*/ Cache httpCache = new Cache(getCacheDir(), 1024 * 1024); httpClient.setCache(httpCache); } catch (IOException e) { Log.w(TAG, "Error initialising okhttp cache", e); } final ImageLoaderConfiguration imageLoaderConfig = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(imageLoaderConfig); }