From e89ec2b412c9b301bf223a6af79b9349de3e1a8e Mon Sep 17 00:00:00 2001 From: mr-chengwangyong Date: Mon, 24 Jul 2017 18:22:37 +0800 Subject: [PATCH] add readMe --- .idea/vcs.xml | 6 ++++++ README.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 14afeaa..c8b15b9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,57 @@ # RetrofitCache -Retrofit add cache +优雅的给 Retrofit 加上 Cache + +只需要一行代码,就让你的应用,即可拥有 离线缓存的功能 + + + +#### 使用 + +1. 创建一个CacheInterceptor + +``` +RetrofitCacheInterceptor retrofitCacheInterceptor = +new RetrofitCacheInterceptor(getApplicationContext()); +``` + +2. 创建一个 cache 目录(一般配置`Retrofit` 的时候都会配置) + +``` +File cacheDir = Environment.getExternalStorageDirectory(); +Cache cache = new Cache(cacheDir, 20 * 1024 * 1024); +``` + +3. 给 OkHttp 设置上 cacheInterceptor + + ``` + OkHttpClient okHttpClient = new OkHttpClient() + .newBuilder() + .addInterceptor(retrofitCacheInterceptor) + .cache(cache) + .build(); + ``` + +4. 最后,配置完 `Retrofit` + + ``` + Retrofit build = new Retrofit + .Builder() + .baseUrl("http://api.jcodecraeer.com/") + //set client + .client(okHttpClient) + .addConverterFactory(ScalarsConverterFactory.create()) + .build(); + ``` + + + +其实,大部分项目都已经配置完成了 cache 和 OkHttpClint + +这个时候,只需要在 OkHttpClint里面加上即可 + +``` +addInterceptor(new RetrofitCacheInterceptor(context)) +``` + + +