Skip to content

Commit

Permalink
add readMe
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-chengwangyong committed Jul 24, 2017
1 parent f40cbd7 commit e89ec2b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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))
```



0 comments on commit e89ec2b

Please sign in to comment.