Java 类android.net.http.HttpResponseCache 实例源码

项目:volley-it    文件:MainActivity.java   
/**
 * 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;

}
项目:StreamAds-Android    文件:SampleListActivity.java   
@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);
    }
}
项目:android-rest-example    文件:MyApplication.java   
@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);
    }
}
项目:XDA-One    文件:RetrofitClient.java   
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);
                }
            });
}
项目:MvpPlus    文件:MyIntentService.java   
protected void onStop() {

        HttpResponseCache cache = HttpResponseCache.getInstalled();
        if (cache != null) {
            cache.flush();
        }
    }
项目:Odyssey2017    文件:FeedActivity.java   
@Override
protected void onStop() {
    super.onStop();
   HttpResponseCache cache = HttpResponseCache.getInstalled();
    if(cache != null) {  //Clearing the cache
        cache.flush();
    }
}
项目:Odyssey2017    文件:FeedActivity.java   
public void cacher()
{
    httpCacheDir = getExternalCacheDir();
    try {
        HttpResponseCache.install(httpCacheDir, httpCacheSize); //Setting the cache
    } catch (Exception e) {
        e.printStackTrace();
    }
}
项目:YalpStore    文件:YalpStoreApplication.java   
@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();
}
项目:pandroid    文件:NetworkUtils.java   
/**
 * 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);
    }
}
项目:Android-High-Performance-Programming    文件:ResponseCache.java   
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);
    }
}
项目:Android-High-Performance-Programming    文件:ResponseCache.java   
protected void onStop() {
    super.onStop();
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }
}
项目:android-restless    文件:FileCache.java   
@Override
public void put(Request key, HttpResponse value) {
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }
}
项目:android-restless    文件:FileCache.java   
@Override
public void clear() throws IOException {
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.delete();
    }
    HttpResponseCache.install(file, size);
}
项目:android-lite    文件:MainActivity.java   
@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();
}
项目:android-lite    文件:MainActivity.java   
@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);
    }
}
项目:PowerToggles    文件:ThemePicker.java   
@Override
protected void onPause() {
  HttpResponseCache cache = HttpResponseCache.getInstalled();
  if (cache != null) {
      cache.flush();
  }
  super.onPause();
}
项目:volley-it    文件:MainActivity.java   
/**
 * 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.");
    }
}
项目:volley-it    文件:MainActivity.java   
/**
 * 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();

}
项目:mbira-android-template    文件:LoadingActivity.java   
@Override
protected void onStop() {
    super.onStop();
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }
}
项目:wordpress_app_android    文件:WordPress.java   
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");
    }
}
项目:sonarflow-android    文件:MainActivity.java   
protected void onStop() {
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }
    super.onStop();
}
项目:lighthttp    文件:CacheUtil.java   
@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;
}
项目:mc_backup    文件:UrlConnectionDownloader.java   
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;
}
项目:FullRobolectricTestSample    文件:ShadowHttpResponseCache.java   
@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;
  }
}
项目:FullRobolectricTestSample    文件:HttpResponseCacheTest.java   
@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);
}
项目:FullRobolectricTestSample    文件:HttpResponseCacheTest.java   
@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();
}
项目:CampusFeedv2    文件:Api.java   
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);
    }
}
项目:CampusFeedv2    文件:Api.java   
@Override
public void close() throws IOException {
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }
}
项目:picasso    文件:UrlConnectionDownloader.java   
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;
}
项目:Qshp    文件:PostContentActivity.java   
@Override
public void onStop() {
    super.onStop();
    mAdapter.changeCursor(null);
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }
}
项目:UnCafe    文件:CoffeePlacesApplication.java   
/**
 *
 */
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();
    }
}
项目:android-rest-example    文件:MyApplication.java   
@Override
public void onTerminate() {
    super.onTerminate();

    // TODO clear cookiestore content here

    // remove cached files
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }
}
项目:Podcatcher-Deluxe-Android-Studio    文件:Podcatcher.java   
@Override
public void run() {
    Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND);

    final HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null)
        cache.flush();
}
项目:PkRSS    文件:DefaultDownloader.java   
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);
    }
}
项目:SadPanda    文件:SadPandaApp.java   
@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);
    }
}
项目:Impeller    文件:ImpellerApplication.java   
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);
        }
    }
}
项目:aview    文件:AviewApplication.java   
@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);
    }
}
项目:aview    文件:AviewApplication.java   
@Override
@Trace
public void onTerminate() {
    super.onTerminate();
    aviewVideoService.close();

    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }

}
项目:xr    文件:HomeActivity.java   
@Override
protected void onStop() {
    HttpResponseCache cache = HttpResponseCache.getInstalled();
    if (cache != null) {
        cache.flush();
    }

    eventBus.unregister(this);
    super.onStop();
}
项目:android-restless    文件:FileCache.java   
public FileCache(File file, long size) throws IOException {
    this.file = file;
    this.size = size;
    HttpResponseCache.install(file, size);
}