/**
* 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;
}
java类android.net.http.HttpResponseCache的实例源码
MainActivity.java 文件源码
项目:volley-it
阅读 23
收藏 0
点赞 0
评论 0
SampleListActivity.java 文件源码
项目:StreamAds-Android
阅读 24
收藏 0
点赞 0
评论 0
@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);
}
}
MyApplication.java 文件源码
项目:android-rest-example
阅读 29
收藏 0
点赞 0
评论 0
@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);
}
}
RetrofitClient.java 文件源码
项目:XDA-One
阅读 32
收藏 0
点赞 0
评论 0
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);
}
});
}
MyIntentService.java 文件源码
项目:MvpPlus
阅读 35
收藏 0
点赞 0
评论 0
protected void onStop() {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
FeedActivity.java 文件源码
项目:Odyssey2017
阅读 30
收藏 0
点赞 0
评论 0
@Override
protected void onStop() {
super.onStop();
HttpResponseCache cache = HttpResponseCache.getInstalled();
if(cache != null) { //Clearing the cache
cache.flush();
}
}
FeedActivity.java 文件源码
项目:Odyssey2017
阅读 25
收藏 0
点赞 0
评论 0
public void cacher()
{
httpCacheDir = getExternalCacheDir();
try {
HttpResponseCache.install(httpCacheDir, httpCacheSize); //Setting the cache
} catch (Exception e) {
e.printStackTrace();
}
}
YalpStoreApplication.java 文件源码
项目:YalpStore
阅读 30
收藏 0
点赞 0
评论 0
@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();
}
NetworkUtils.java 文件源码
项目:pandroid
阅读 37
收藏 0
点赞 0
评论 0
/**
* 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);
}
}
ResponseCache.java 文件源码
项目:Android-High-Performance-Programming
阅读 32
收藏 0
点赞 0
评论 0
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);
}
}
ResponseCache.java 文件源码
项目:Android-High-Performance-Programming
阅读 33
收藏 0
点赞 0
评论 0
protected void onStop() {
super.onStop();
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
FileCache.java 文件源码
项目:android-restless
阅读 33
收藏 0
点赞 0
评论 0
@Override
public void put(Request key, HttpResponse value) {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
FileCache.java 文件源码
项目:android-restless
阅读 27
收藏 0
点赞 0
评论 0
@Override
public void clear() throws IOException {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.delete();
}
HttpResponseCache.install(file, size);
}
MainActivity.java 文件源码
项目:android-lite
阅读 20
收藏 0
点赞 0
评论 0
@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();
}
MainActivity.java 文件源码
项目:android-lite
阅读 26
收藏 0
点赞 0
评论 0
@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);
}
}
ThemePicker.java 文件源码
项目:PowerToggles
阅读 33
收藏 0
点赞 0
评论 0
@Override
protected void onPause() {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
super.onPause();
}
MainActivity.java 文件源码
项目:volley-it
阅读 20
收藏 0
点赞 0
评论 0
/**
* 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.");
}
}
MainActivity.java 文件源码
项目:volley-it
阅读 23
收藏 0
点赞 0
评论 0
/**
* 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();
}
LoadingActivity.java 文件源码
项目:mbira-android-template
阅读 28
收藏 0
点赞 0
评论 0
@Override
protected void onStop() {
super.onStop();
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
WordPress.java 文件源码
项目:wordpress_app_android
阅读 26
收藏 0
点赞 0
评论 0
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");
}
}
MainActivity.java 文件源码
项目:sonarflow-android
阅读 21
收藏 0
点赞 0
评论 0
protected void onStop() {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
super.onStop();
}
CacheUtil.java 文件源码
项目:lighthttp
阅读 30
收藏 0
点赞 0
评论 0
@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;
}
UrlConnectionDownloader.java 文件源码
项目:mc_backup
阅读 34
收藏 0
点赞 0
评论 0
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;
}
ShadowHttpResponseCache.java 文件源码
项目:FullRobolectricTestSample
阅读 27
收藏 0
点赞 0
评论 0
@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;
}
}
HttpResponseCacheTest.java 文件源码
项目:FullRobolectricTestSample
阅读 29
收藏 0
点赞 0
评论 0
@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);
}
HttpResponseCacheTest.java 文件源码
项目:FullRobolectricTestSample
阅读 32
收藏 0
点赞 0
评论 0
@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();
}
Api.java 文件源码
项目:CampusFeedv2
阅读 30
收藏 0
点赞 0
评论 0
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);
}
}
Api.java 文件源码
项目:CampusFeedv2
阅读 31
收藏 0
点赞 0
评论 0
@Override
public void close() throws IOException {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
UrlConnectionDownloader.java 文件源码
项目:picasso
阅读 27
收藏 0
点赞 0
评论 0
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;
}
PostContentActivity.java 文件源码
项目:Qshp
阅读 33
收藏 0
点赞 0
评论 0
@Override
public void onStop() {
super.onStop();
mAdapter.changeCursor(null);
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}