Skip to content

Commit

Permalink
Merge pull request #38 from yale8848/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yale8848 authored Aug 1, 2019
2 parents 9fd9c29 + 7716032 commit 4d6a641
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 22 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

[English](README_EN.md)

[![](https://img.shields.io/badge/jcenter-retrofitcache_1.1.0-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcache/1.1.0)
[![](https://img.shields.io/badge/jcenter-retrofitcacherx2_1.1.0-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcacherx2/1.1.0)
[![](https://img.shields.io/badge/jcenter-retrofitcache_1.1.1-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcache/1.1.1)
[![](https://img.shields.io/badge/jcenter-retrofitcacherx2_1.1.1-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcacherx2/1.1.1)

RetrofitCache让retrofit2+okhttp3+rxjava 配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。

Expand Down Expand Up @@ -82,8 +82,8 @@ Observable<HttpResult> test();
- 添加 jcenter lib,注意根据自己的库选择

```
compile 'ren.yale.android:retrofitcachelib:1.1.0' //retrofit2+okhttp3+rxjava1
compile 'ren.yale.android:retrofitcachelibrx2:1.1.0' //retrofit2+okhttp3+rxjava2
compile 'ren.yale.android:retrofitcachelib:1.1.1' //retrofit2+okhttp3+rxjava1
compile 'ren.yale.android:retrofitcachelibrx2:1.1.1' //retrofit2+okhttp3+rxjava2
```

- 在Android Application里初始化
Expand Down Expand Up @@ -116,6 +116,8 @@ OkHttpClient client = clientBuilder.cache(cache).build();
```
okhttp3.OkHttpClient.Builder clientBuilder=new okhttp3.OkHttpClient.Builder();
...
//clientBuilder.addInterceptor(new MockInterceptor()); //如果只用Mock模拟功能,只需添加这个拦截器
clientBuilder.addInterceptor(new CacheForceInterceptorNoNet());
clientBuilder.addNetworkInterceptor(new CacheInterceptorOnNet());
...
Expand Down
10 changes: 6 additions & 4 deletions README_EN.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# RetrofitCache

[![](https://img.shields.io/badge/jcenter-retrofitcache_1.1.0-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcache/1.1.0)
[![](https://img.shields.io/badge/jcenter-retrofitcacherx2_1.1.0-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcacherx2/1.1.0)
[![](https://img.shields.io/badge/jcenter-retrofitcache_1.1.1-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcache/1.1.1)
[![](https://img.shields.io/badge/jcenter-retrofitcacherx2_1.1.1-519dd9.svg)](https://bintray.com/yale8848/maven/retrofitcacherx2/1.1.1)

RetrofitCache let retrofit2,okhttp3,rxjava add cache so easy. You can config cache strategy with each api by annotation.Also you can config mock data with each api easily.

Expand Down Expand Up @@ -80,8 +80,8 @@ Observable<HttpResult> test();
- Add jcenter lib,notice chose different with you project

```
compile 'ren.yale.android:retrofitcachelib:1.1.0' //retrofit2+okhttp3+rxjava1
compile 'ren.yale.android:retrofitcachelibrx2:1.1.0' //retrofit2+okhttp3+rxjava2
compile 'ren.yale.android:retrofitcachelib:1.1.1' //retrofit2+okhttp3+rxjava1
compile 'ren.yale.android:retrofitcachelibrx2:1.1.1' //retrofit2+okhttp3+rxjava2
```

- Init in Android Application
Expand Down Expand Up @@ -114,6 +114,8 @@ OkHttpClient client = clientBuilder.cache(cache).build();
```
okhttp3.OkHttpClient.Builder clientBuilder=new okhttp3.OkHttpClient.Builder();
...
//clientBuilder.addInterceptor(new MockInterceptor()); //Only add this interceptor if only use Mock
clientBuilder.addInterceptor(new CacheForceInterceptorNoNet());
clientBuilder.addNetworkInterceptor(new CacheInterceptorOnNet());
...
Expand Down
10 changes: 8 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
compileSdkVersion 28
buildToolsVersion "28.0.3"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "ren.yale.android.retrofitcachetest"
Expand Down Expand Up @@ -45,6 +45,12 @@ android {
dependencies {




//implementation 'ren.yale.android:retrofitcachelib:1.1.1' //retrofit2+okhttp3+rxjava1
//implementation 'ren.yale.android:retrofitcachelibrx2:1.1.1' //retrofit2+okhttp3+rxjava2


implementation project(':retrofitcachelib')
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public OkHttpClient getOkHttpClient(){
clientBuilder.connectTimeout(20, TimeUnit.SECONDS);
clientBuilder.writeTimeout(20, TimeUnit.SECONDS);
clientBuilder.addInterceptor(new LogInterceptor());
//clientBuilder.addInterceptor(new MockInterceptor());
clientBuilder.addInterceptor(new CacheForceInterceptorNoNet());
clientBuilder.addNetworkInterceptor(new CacheInterceptorOnNet());
clientBuilder.retryOnConnectionFailure(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import ren.yale.android.retrofitcachelib.anno.Cache;
import ren.yale.android.retrofitcachelibrx2.anno.Mock;
import ren.yale.android.retrofitcachelibrx2.anno.Cache;
import ren.yale.android.retrofitcachetest.bean.GankAndroid;
import retrofit2.http.GET;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public OkHttpClient getOkHttpClient(){
clientBuilder.connectTimeout(20, TimeUnit.SECONDS);
clientBuilder.writeTimeout(20, TimeUnit.SECONDS);
clientBuilder.addInterceptor(new LogInterceptor());
//clientBuilder.addInterceptor(new MockInterceptor());
clientBuilder.addInterceptor(new CacheForceInterceptorNoNet());
clientBuilder.addNetworkInterceptor(new CacheInterceptorOnNet());

Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
//classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'


Expand Down
4 changes: 2 additions & 2 deletions retrofitcachelib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ apply plugin: 'com.android.library'
//group='com.github.yale8848'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
compileSdkVersion 28
buildToolsVersion "28.0.3"

defaultConfig {
minSdkVersion 14
Expand Down
2 changes: 1 addition & 1 deletion retrofitcachelib/config/RetrofitCache.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'


version = '1.1.0'
version = '1.1.1'

repositories {
jcenter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ren.yale.android.retrofitcachelib.intercept;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import ren.yale.android.retrofitcachelib.util.LogUtil;

/**
* Created by yale on 2019/7/24.
*/
public class MockInterceptor extends BaseInterceptor implements Interceptor {


@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response mockResponse = mockResponse(chain);
if (mockResponse!=null){
return mockResponse;
}
String mockUrl = mockUrl(chain);
if (mockUrl!=null){
LogUtil.d("get data from mock url: "+mockUrl);
request = request.newBuilder().url(mockUrl).header(KEY_HEADER_PRE_URL,request.url().toString())
.build();
}
Response response = chain.proceed(request);
int code = response.code();
if ( code == 504){
response = chain.proceed(chain.request());
}
if(response.networkResponse()!=null){
LogUtil.d("get data from net");
} else
if (response.cacheResponse()!=null){
LogUtil.d("get data from cache");
}

return response;
}
}
6 changes: 3 additions & 3 deletions retrofitcachelibrx2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ apply plugin: 'com.android.library'
//group='com.github.yale8848'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
compileSdkVersion 28
buildToolsVersion "28.0.3"

defaultConfig {
minSdkVersion 14
Expand Down Expand Up @@ -35,4 +35,4 @@ dependencies {
implementation'com.squareup.retrofit2:adapter-rxjava2:2.2.0'

}
apply from: './config/RetrofitCache.gradle'
//apply from: './config/RetrofitCache.gradle'
2 changes: 1 addition & 1 deletion retrofitcachelibrx2/config/RetrofitCache.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'


version = '1.1.0'
version = '1.1.1'

repositories {
jcenter()
Expand Down
1 change: 0 additions & 1 deletion retrofitcachelibrx2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
package="com.daoxuehao.android.retrofitcachelibrx2">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ren.yale.android.retrofitcachelibrx2.intercept;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import ren.yale.android.retrofitcachelibrx2.util.LogUtil;

/**
* Created by yale on 2019/7/24.
*/
public class MockInterceptor extends BaseInterceptor implements Interceptor {


@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response mockResponse = mockResponse(chain);
if (mockResponse!=null){
return mockResponse;
}
String mockUrl = mockUrl(chain);
if (mockUrl!=null){
LogUtil.d("get data from mock url: "+mockUrl);
request = request.newBuilder().url(mockUrl).header(KEY_HEADER_PRE_URL,request.url().toString())
.build();
}
Response response = chain.proceed(request);
int code = response.code();
if ( code == 504){
response = chain.proceed(chain.request());
}
if(response.networkResponse()!=null){
LogUtil.d("get data from net");
} else
if (response.cacheResponse()!=null){
LogUtil.d("get data from cache");
}

return response;
}
}

0 comments on commit 4d6a641

Please sign in to comment.