下面列出了怎么用android.net.http.HttpResponseCache的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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;
}
public VenvySvgaImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
svgaParser = initSVGAParse();
if (!isInstallCache) {
isInstallCache = true;
File cacheDir = new File(SVGAParser.getCacheDirectory(context, true), "venvy/svga/");
try {
HttpResponseCache.install(cacheDir, 1024 * 1024 * 128);
} catch (IOException e) {
}
}
}
@TargetApi(21)
public VenvySvgaImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
svgaParser = initSVGAParse();
if (!isInstallCache) {
isInstallCache = true;
File cacheDir = new File(SVGAParser.getCacheDirectory(context, true), "venvy/svga/");
try {
HttpResponseCache.install(cacheDir, 1024 * 1024 * 128);
} catch (IOException e) {
}
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void initHttpCache() {
try {
if (null != HttpResponseCache.getInstalled()) {
HttpResponseCache.getInstalled().delete();
}
HttpResponseCache.install(new File(getCacheDir(), "http"), 5 * 1024 * 1024);
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Could not register cache " + e.getMessage());
}
}
/**
* 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);
}
}
@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;
}
@Override
public void onStop() {
super.onStop();
mAdapter.changeCursor(null);
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache != null) {
cache.flush();
}
}
/**
* Installs a cache for fetched URLs
*/
private void installCache() {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache == null) {
try {
File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
HttpResponseCache.install(httpCacheDir, CACHE_SIZE);
} catch (IOException e) {
Log.w(TAG, "HTTP response cache installation failed:", e);
}
}
}
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);
}
}
static void close(Object cache) {
try {
((HttpResponseCache) cache).close();
} catch (IOException ignored) {
}
}
static void close(Object cache) {
try {
((HttpResponseCache) cache).close();
} catch (IOException ignored) {
}
}