/** * Fetches an entry value from the HttpResponseCache cache * @param connection connection from which we need the cache * @param uri uri to use to get the cache entry * @return cache entry value as String */ private String fetchFromHTTPUrlConnectionCache(HttpURLConnection connection, URI uri) { try { HttpResponseCache responseCache = HttpResponseCache.getInstalled(); if(responseCache != null){ CacheResponse cacheResponse = responseCache.get(uri, "GET", connection.getRequestProperties()); Scanner scanner = new Scanner(cacheResponse.getBody(), "UTF-8"); StringBuilder sb = new StringBuilder(); while (scanner.hasNextLine()){ sb.append(scanner.nextLine()); } return sb.toString(); } } catch (Exception ex) { ex.printStackTrace(); } return null; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_single_pane); Toolbar toolbar = (Toolbar) findViewById(R.id.app_toolbar); setSupportActionBar(toolbar); if (savedInstanceState == null) { FragmentManager.enableDebugLogging(true); getSupportFragmentManager().beginTransaction() .add(R.id.container, SampleListFragment.newInstance(), SampleListFragment.TAG) .commit(); } // http response cache File httpCacheDir = new File(getCacheDir(), "http"); long httpCacheSize = 100 * 1024 * 1024; // 100 MiB try { HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { Log.i(SampleListActivity.class.getSimpleName(), "HTTP response cache installation failed:" + e); } }
@Override public void onCreate() { super.onCreate(); // cookie store CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); // init cache for http responses try { File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http"); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch(IOException e){ Log.i(TAG, "HTTP response cache installation failed:" + e); } }
public static RestAdapter.Builder getRestBuilder(final Context context, final String url) { if (!sResponseCache) { try { final File httpCacheDir = new File(context.getCacheDir(), "http"); final long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { e.printStackTrace(); } sResponseCache = true; } return new RestAdapter.Builder() .setEndpoint(url) .setConverter(JACKSON_CONVERTER) .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("Authorization", "Basic " + XDAConstants.ENCODED_AUTHORIZATION); } }); }
protected void onStop() { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
@Override protected void onStop() { super.onStop(); HttpResponseCache cache = HttpResponseCache.getInstalled(); if(cache != null) { //Clearing the cache cache.flush(); } }
public void cacher() { httpCacheDir = getExternalCacheDir(); try { HttpResponseCache.install(httpCacheDir, httpCacheSize); //Setting the cache } catch (Exception e) { e.printStackTrace(); } }
@Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { try { HttpResponseCache.install(new File(getCacheDir(), "http"), 5 * 1024 * 1024); } catch (IOException e) { Log.e(getClass().getSimpleName(), "Could not register cache " + e.getMessage()); } } PreferenceManager.setDefaultValues(this, R.xml.settings, false); Thread.setDefaultUncaughtExceptionHandler(new YalpStoreUncaughtExceptionHandler(getApplicationContext())); registerDownloadReceiver(); registerInstallReceiver(); }
/** * Enable caching for http request : must use HttpUrlConnection ! * see : <a href="http://developer.android.com/reference/android/net/http/HttpResponseCache.html">HttpResponseCache</a> * * @param context */ public static void enableHttpCaching(Context context) { long httpCacheSize = 10 * 1024 * 1024; // 10 MiB try { File httpCacheDir = new File(context.getCacheDir(), "http"); HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { Log.i("", "HTTP response cache failed:" + e); } }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { File httpCacheDir = new File(getCacheDir(), "http"); long httpCacheSize = 0; HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { Log.i(getClass().getName(), "HTTP response cache installation failed:" + e); } }
protected void onStop() { super.onStop(); HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
@Override public void put(Request key, HttpResponse value) { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
@Override public void clear() throws IOException { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.delete(); } HttpResponseCache.install(file, size); }
@Override protected void onStop() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { final HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } } super.onStop(); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void enableHttpResponseCache() { final long httpCacheSize = 10 * 1024 * 1024; final File httpCacheDir = new File(getCacheDir(), "http"); try { HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (final IOException e) { throw new RuntimeException(e); } }
@Override protected void onPause() { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } super.onPause(); }
/** * Installs the HttpResponseCache * Note this will only work on Android 4.0+ */ private void enableHttpResponseCache() { try { long httpCacheSize = 10 * 1024 * 1024; // 10 MiB File httpCacheDir = new File(getCacheDir(), "http"); HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (Exception httpResponseCacheNotAvailable) { Log.d(TAG, "HTTP response cache is unavailable."); } }
/** * Invalidates both HttpResponseCache and Volley cache * @param view */ public void invalidateCache(View view) { mRequestQueue.getCache().clear(); try { HttpResponseCache cache = HttpResponseCache.getInstalled(); if(cache != null) cache.delete(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(this, "Cache is now empty", Toast.LENGTH_SHORT).show(); }
@Override protected void onStop() { super.onStop(); HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
private static void enableHttpResponseCache(Context context) { try { long httpCacheSize = 5 * 1024 * 1024; // 5MB File httpCacheDir = new File(context.getCacheDir(), "http"); HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { AppLog.w(T.UTILS, "Failed to enable http response cache"); } }
protected void onStop() { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } super.onStop(); }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private static HttpResponseCache installHttpResponseCache(final File cacheDir) throws IOException { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache == null) { final long maxSize = calculateDiskCacheSize(cacheDir); cache = HttpResponseCache.install(cacheDir, maxSize); } return cache; }
static Object install(Context context) throws IOException { File cacheDir = Utils.createDefaultCacheDir(context); HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache == null) { long maxSize = Utils.calculateDiskCacheSize(cacheDir); cache = HttpResponseCache.install(cacheDir, maxSize); } return cache; }
@Implementation public static HttpResponseCache install(File directory, long maxSize) { HttpResponseCache cache = newInstanceOf(HttpResponseCache.class); ShadowHttpResponseCache shadowCache = Robolectric.shadowOf(cache); shadowCache.originalObject = cache; shadowCache.directory = directory; shadowCache.maxSize = maxSize; synchronized (LOCK) { installed = shadowCache; return cache; } }
@Test public void installedCacheIsReturned() throws Exception { assertThat(HttpResponseCache.getInstalled()).isNull(); HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); HttpResponseCache installed = HttpResponseCache.getInstalled(); assertThat(installed).isSameAs(cache); assertThat(installed.maxSize()).isEqualTo(42); }
@Test public void countsStartAtZero() throws Exception { HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); assertThat(cache.getHitCount()).isZero(); assertThat(cache.getNetworkCount()).isZero(); assertThat(cache.getRequestCount()).isZero(); }
private Api(Context context) { try { File httpCacheDir = new File(context.getCacheDir(), "http"); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { Log.i("Api", "HTTP response cache installation failed:" + e); } }
@Override public void close() throws IOException { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
@Override public void onStop() { super.onStop(); mAdapter.changeCursor(null); HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
/** * */ private void initCacheFile() { try { httpCacheDir = new File(getCacheDir(), "http"); httpCacheDir.setReadable(true); HttpResponseCache.install(httpCacheDir, httpCacheSize); cache = new Cache(httpCacheDir, httpCacheSize); } catch (Exception e) { e.printStackTrace(); } }
@Override public void onTerminate() { super.onTerminate(); // TODO clear cookiestore content here // remove cached files HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
@Override public void run() { Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND); final HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) cache.flush(); }
public DefaultDownloader(Context context) { cacheDir = new File(context.getCacheDir(), "http"); try { HttpResponseCache.install(cacheDir, cacheSize); } catch (IOException e) { Log.i(TAG, "HTTP response cache installation failed:" + e); } }
@Override public void onCreate() { super.onCreate(); File httpCacheDir = new File(getCacheDir(), "http"); try { HttpResponseCache.install(httpCacheDir, HTTP_CACHE_SIZE); } catch (IOException e) { Log.e(TAG, "Failed to create http cache!", e); } }
private void tryInstallResponseCache() { if(HttpResponseCache.getInstalled() == null) { File cacheDir = new File(getCacheDir(), "http"); try { HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); } catch (IOException e) { Log.w(TAG, "Creating response cache", e); } } }
@AfterInject @Trace(tag = TAG, level = Log.DEBUG) void init() { // if (BuildConfig.DEBUG) { // aviewPrefs.pref_openedDrawer().put(false); // } // Create a unique id for this install if one doesn't already exist if (!aviewPrefs.ivid().exists()) { if (Log.isLoggable(TAG, Log.DEBUG)) Log.d(TAG, "Creating new ivid"); aviewPrefs.ivid().put(UUID.randomUUID().toString()); } if (Log.isLoggable(TAG, Log.DEBUG)) Log.d(TAG, "ivid=" + aviewPrefs.ivid().get()); // Create a cache for automatically caching HTTP requests try { File httpCacheDir = new File(this.getCacheDir(), "http"); long httpCacheSize = 10 * 1024 * 1024; // 10 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { if (Log.isLoggable(TAG, Log.INFO)) Log.i(TAG, "HTTP response cache installation failed:" + e); } }
@Override @Trace public void onTerminate() { super.onTerminate(); aviewVideoService.close(); HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } }
@Override protected void onStop() { HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } eventBus.unregister(this); super.onStop(); }
public FileCache(File file, long size) throws IOException { this.file = file; this.size = size; HttpResponseCache.install(file, size); }