Skip to content

Commit 058382f

Browse files
committed
add api mock
1 parent 4896a4e commit 058382f

File tree

15 files changed

+398
-26
lines changed

15 files changed

+398
-26
lines changed

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ allprojects {
2525
Add the dependency
2626
``` groovy
2727
dependencies {
28-
implementation 'com.github.BakerJQ:RxRetroHttp:1.1.0'
28+
implementation 'com.github.BakerJQ:RxRetroHttp:1.2.0'
2929
}
3030
3131
```
@@ -48,16 +48,19 @@ Mention:
4848
- You need to do this after you called init()
4949
- DON'T forget to add the 'Tag' in generateRetroClient() function
5050
- If you don't set an ApiResultClass, the lib will use GsonConverterFactory as default, this means that the lib will not deal with response logic for you, or you can add your own ResponseConverter
51+
5152
```java
5253
RxRetroHttp.getInstance()
5354
.setApiResultClass(YourApiResult.class)//other result
5455
.setBaseUrl("http://host/api/data/")//other url
5556
.generateRetroClient("YourTag");//other request tag
5657
```
58+
5759
#### Settings
5860
You can customize your http settings by get and reset the builders, or calling setting functions
5961

6062
Mention: This must be done AFTER init() and BEFORE generateRetroClient() function
63+
6164
```java
6265
RxRetroHttp.init().setXXX().setXXX();
6366
Retrofit.Builder retrofitBuilder = RxRetroHttp.getRetrofitBuilder();
@@ -109,6 +112,45 @@ Just define a call like Retrofit
109112
```java
110113
RxRetroHttp.create(YourApiService.class).getTestInfo()
111114
```
115+
116+
### Mock Data
117+
You can mock your api data in two ways: from file or from json string.
118+
119+
You should enable mock when init;
120+
121+
```java
122+
RxRetroHttp.init(this).setMockEnable(true)
123+
```
124+
125+
#### Mock From File
126+
Step 1. Put your mock data file under assets dir
127+
128+
Step 2. Add headers to your api function
129+
130+
```java
131+
@Headers({RxRetroHttp.MOCK_FILE_PATH_KEY + ":mockfile.json")
132+
@GET("test/info")
133+
Observable<TestInfo> getTestInfo();
134+
```
135+
136+
#### Mock From Json String
137+
Just add headers to your api function
138+
139+
```java
140+
@Headers({RxRetroHttp.MOCK_DATA_KEY + ":{\"testInfo\":\"info\"}")
141+
@GET("test/info")
142+
Observable<TestInfo> getTestInfo();
143+
```
144+
145+
#### Header Keys
146+
147+
| key | desc |
148+
| ---- | ---- |
149+
| MOCK_DATA_KEY | mock json string |
150+
| MOCK_FILE_PATH_KEY | mock assets file path |
151+
| MOCK_ENABLE_KEY | enable or disable mock |
152+
| MOCK_DELAY_KEY | delay duration of response |
153+
112154
## Proguard
113155
- Add Retrofit proguard
114156
- Add OkHttp proguard
@@ -118,7 +160,7 @@ RxRetroHttp.create(YourApiService.class).getTestInfo()
118160
For 21- projects, add the dependency like below
119161
``` groovy
120162
dependencies {
121-
implementation ('com.github.BakerJQ:RxRetroHttp:1.1.0'){
163+
implementation ('com.github.BakerJQ:RxRetroHttp:1.2.0'){
122164
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
123165
}
124166
implementation 'com.squareup.okhttp3:okhttp:3.12.0'

README_cn.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ allprojects {
2626
在app或相应使用的module中加入
2727
``` groovy
2828
dependencies {
29-
implementation 'com.github.BakerJQ:RxRetroHttp:1.1.0'
29+
implementation 'com.github.BakerJQ:RxRetroHttp:1.2.0'
3030
}
3131
3232
```
@@ -111,11 +111,49 @@ public interface YourApiService {
111111
RxRetroHttp.create(YourApiService.class).getTestInfo()
112112
```
113113

114+
### Mock数据
115+
你可以通过json字符串以及文件的方式进行mock。
116+
117+
若要开启mock,请在初始化时进行设置。
118+
119+
```java
120+
RxRetroHttp.init(this).setMockEnable(true)
121+
```
122+
123+
#### 通过文件进行Mock
124+
Step 1. 将mock文件放到assets下
125+
126+
Step 2. 对API方法增加Headers注解
127+
128+
```java
129+
@Headers({RxRetroHttp.MOCK_FILE_PATH_KEY + ":mockfile.json")
130+
@GET("test/info")
131+
Observable<TestInfo> getTestInfo();
132+
```
133+
134+
#### 通过Json字符串进行Mock
135+
直接添加Headers
136+
137+
```java
138+
@Headers({RxRetroHttp.MOCK_DATA_KEY + ":{\"testInfo\":\"info\"}")
139+
@GET("test/info")
140+
Observable<TestInfo> getTestInfo();
141+
```
142+
143+
#### 所支持的Header Key
144+
145+
| key | 描述 |
146+
| ---- | ---- |
147+
| MOCK_DATA_KEY | mock json字符串 |
148+
| MOCK_FILE_PATH_KEY | mock assets文件路径 |
149+
| MOCK_ENABLE_KEY | 是否开启mock |
150+
| MOCK_DELAY_KEY | mock返回结果的延时 |
151+
114152
## Android API等级小于21
115153
对于Android API等级小于21的工程,按如下方式引入该库
116154
``` groovy
117155
dependencies {
118-
implementation ('com.github.BakerJQ:RxRetroHttp:1.1.0'){
156+
implementation ('com.github.BakerJQ:RxRetroHttp:1.2.0'){
119157
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
120158
}
121159
implementation 'com.squareup.okhttp3:okhttp:3.12.0'

app/src/main/assets/mock/weather.json

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"time": "2018-09-22 12:37:21",
3+
"date": "20180922",
4+
"message": "Success !",
5+
"status": 200,
6+
"data": {
7+
"cityInfo": {
8+
"city": "南昌市",
9+
"parent": "江西",
10+
"updateTime": "12:32"
11+
},
12+
"shidu": "22%",
13+
"pm25": 15.0,
14+
"pm10": 46.0,
15+
"quality": "",
16+
"wendu": "24",
17+
"ganmao": "各类人群可自由活动",
18+
"forecast": [
19+
{
20+
"date": "22",
21+
"ymd": "2018-09-22",
22+
"week": "星期六",
23+
"sunrise": "05:57",
24+
"high": "高温 26.0℃",
25+
"low": "低温 15.0℃",
26+
"sunset": "18:10",
27+
"aqi": 55.0,
28+
"fx": "西北风",
29+
"fl": "4-5级",
30+
"type": "",
31+
"notice": "愿你拥有比阳光明媚的心情"
32+
},
33+
{
34+
"date": "23",
35+
"ymd": "2018-09-22",
36+
"week": "星期日",
37+
"sunrise": "05:58",
38+
"high": "高温 23.0℃",
39+
"low": "低温 14.0℃",
40+
"sunset": "18:09",
41+
"aqi": 29.0,
42+
"fx": "西北风",
43+
"fl": "4-5级",
44+
"type": "",
45+
"notice": "愿你拥有比阳光明媚的心情"
46+
},
47+
{
48+
"date": "24",
49+
"ymd": "2018-09-22",
50+
"week": "星期一",
51+
"sunrise": "05:59",
52+
"high": "高温 24.0℃",
53+
"low": "低温 15.0℃",
54+
"sunset": "18:07",
55+
"aqi": 25.0,
56+
"fx": "西北风",
57+
"fl": "<3级",
58+
"type": "",
59+
"notice": "愿你拥有比阳光明媚的心情"
60+
},
61+
{
62+
"date": "25",
63+
"ymd": "2018-09-22",
64+
"week": "星期二",
65+
"sunrise": "06:00",
66+
"high": "高温 24.0℃",
67+
"low": "低温 16.0℃",
68+
"sunset": "18:05",
69+
"aqi": 56.0,
70+
"fx": "西南风",
71+
"fl": "<3级",
72+
"type": "",
73+
"notice": "愿你拥有比阳光明媚的心情"
74+
},
75+
{
76+
"date": "26",
77+
"ymd": "2018-09-22",
78+
"week": "星期三",
79+
"sunrise": "06:01",
80+
"high": "高温 24.0℃",
81+
"low": "低温 17.0℃",
82+
"sunset": "18:04",
83+
"aqi": 86.0,
84+
"fx": "西南风",
85+
"fl": "3-4级",
86+
"type": "",
87+
"notice": "不要被阴云遮挡住好心情"
88+
}
89+
]
90+
}
91+
}

app/src/main/java/com/bakerj/demo/rxretrohttp/APP.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class APP extends Application {
1414
public void onCreate() {
1515
super.onCreate();
1616
Utils.init(this);
17-
RxRetroHttp.init(this)
17+
RxRetroHttp.init(this).setMockEnable(true)//set mock enable
1818
.setBaseUrl("https://api.github.com/")//your main url
1919
.setDefaultErrMsg("Github开小差了")//default error hint message
2020
.setApiResultClass(GithubApiResult.class)//your main api result structure, if not, will use default gson converter

app/src/main/java/com/bakerj/demo/rxretrohttp/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import android.content.Intent;
44
import android.os.Bundle;
5+
import android.widget.Button;
6+
57
import androidx.appcompat.app.AppCompatActivity;
68

79
import com.bakerj.demo.rxretrohttp.gank.GankActivity;
810
import com.bakerj.demo.rxretrohttp.github.GithubActivity;
911
import com.bakerj.demo.rxretrohttp.weather.WeatherActivity;
12+
import com.bakerj.rxretrohttp.RxRetroHttp;
1013

1114
public class MainActivity extends AppCompatActivity {
1215

@@ -20,5 +23,11 @@ protected void onCreate(Bundle savedInstanceState) {
2023
.this, GankActivity.class)));
2124
findViewById(R.id.btn_weather).setOnClickListener(v -> startActivity(new Intent(MainActivity
2225
.this, WeatherActivity.class)));
26+
Button button = findViewById(R.id.btn_mock_enable);
27+
button.setOnClickListener(v -> {
28+
RxRetroHttp.getInstance().setMockEnable(!RxRetroHttp.isMockEnable());
29+
button.setText("Click to " + (RxRetroHttp.isMockEnable() ? "disable" : "enable") +
30+
" mock");
31+
});
2332
}
2433
}

app/src/main/java/com/bakerj/demo/rxretrohttp/entity/github/GithubUser.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.bakerj.demo.rxretrohttp.entity.github;
22

3-
import com.bakerj.demo.rxretrohttp.entity.github.GithubApiResult;
4-
53
public class GithubUser extends GithubApiResult {
64
private String login;
75
private String avatar_url;

app/src/main/java/com/bakerj/demo/rxretrohttp/entity/weather/Weather.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Weather {
88
private List<Forecast> forecast;
99
private Yesterday yesterday;
1010
private String aqi;
11-
private String city;
11+
private CityInfo cityInfo;
1212

1313
public String getWendu() {
1414
return wendu;
@@ -50,36 +50,48 @@ public void setAqi(String aqi) {
5050
this.aqi = aqi;
5151
}
5252

53-
public String getCity() {
54-
return city;
53+
public CityInfo getCityInfo() {
54+
return cityInfo;
5555
}
5656

57-
public void setCity(String city) {
58-
this.city = city;
57+
public void setCityInfo(CityInfo cityInfo) {
58+
this.cityInfo = cityInfo;
59+
}
60+
61+
public class CityInfo{
62+
private String city;
63+
64+
public String getCity() {
65+
return city;
66+
}
67+
68+
public void setCity(String city) {
69+
this.city = city;
70+
}
5971
}
6072

6173
public class Forecast {
62-
private String fengxiang;
63-
private String fengli;
74+
private String fx;
75+
private String fl;
6476
private String high;
6577
private String type;
6678
private String low;
6779
private String date;
6880

69-
public String getFengxiang() {
70-
return fengxiang;
81+
public String getFx() {
82+
return fx;
7183
}
7284

73-
public void setFengxiang(String fengxiang) {
74-
this.fengxiang = fengxiang;
85+
public void setFx(String fx) {
86+
this.fx = fx;
7587
}
7688

77-
public String getFengli() {
78-
return fengli;
89+
public String getFl() {
90+
return fl;
7991
}
8092

81-
public void setFengli(String fengli) {
82-
this.fengli = fengli;
93+
public void setFl(String fl) {
94+
this.fl = fl;
8395
}
8496

8597
public String getHigh() {
@@ -175,12 +187,12 @@ public void setDate(String date) {
175187
@Override
176188
public String toString() {
177189
StringBuilder stringBuilder = new StringBuilder();
178-
stringBuilder.append(city).append("天气:\n")
190+
stringBuilder.append(cityInfo.city).append("天气:\n")
179191
.append("温度:").append(wendu).append("\n")
180192
.append("注意:").append(ganmao).append("\n")
181193
.append("五日天气预报:\n");
182194
for (Forecast f : forecast) {
183-
stringBuilder.append(f.date + ":" + f.fengxiang + " " + f.type + " " + f.fengli + " "
195+
stringBuilder.append(f.date + ":" + f.fx + " " + f.type + " " + f.fl + " "
184196
+ f.high + " " + f.low + "\n");
185197
}
186198
return stringBuilder.toString();

0 commit comments

Comments
 (0)