OkHttp缓存

代码实现:

//创建手机内存文件夹(Environment用来管理手机SDcard内存的)

File file = new File(Environment.getExternalStorageDirectory() + "/H1706A");

//设置文件夹内存大小

long maxSize = 8 * 1024 * 1024;

Cache cache = new Cache(file, maxSize);

//OKHTTPClient两种写法 第二种就是通过构造者模式也可以使用

okHttpClient = new OkHttpClient.Builder().cache(cache).build();

//获取request对象并设置缓存保存时间

Request request = new Request.Builder().url(url).cacheControl(new CacheControl.Builder().maxStale(24 * 60 * 60, TimeUnit.SECONDS).build()).build();

//通过okHttpClient.newCall(request).execute()获取response对象

Response response = okHttpClient.newCall(request).execute();

String str = response.body().string();

你可能感兴趣的:(OkHttp缓存)