Skip to content

Commit 5c57fbd

Browse files
committed
优化代码规范和注释
修正参数深嵌套解析的逻辑
1 parent e1d9a22 commit 5c57fbd

File tree

13 files changed

+505
-108
lines changed

13 files changed

+505
-108
lines changed

EasyHttp.apk

1.02 MB
Binary file not shown.

HelpDoc.md

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
#### 如何添加全局参数?
2+
3+
```
4+
// 添加全局请求参数
5+
EasyConfig.getInstance().addHeader("token", "abc");
6+
// 添加全局请求头
7+
EasyConfig.getInstance().addParam("token", "abc");
8+
```
9+
10+
#### 如何在请求中忽略某个全局参数?
11+
12+
```
13+
public final class XxxApi implements IRequestApi {
14+
15+
@Override
16+
public String getApi() {
17+
return "xxx/xxxx";
18+
}
19+
20+
@HttpIgnore
21+
private String token;
22+
}
23+
```
24+
25+
#### 如何获取服务器配置?
26+
27+
```
28+
IRequestServer server = EasyConfig.getInstance().getServer();
29+
// 获取当前全局的服务器主机地址
30+
String host = server.getHost();
31+
// 获取当前全局的服务器路径地址
32+
String path = server.getPath();
33+
```
34+
35+
#### 如何修改服务器配置?
36+
37+
* 先定义一个服务器配置
38+
39+
```
40+
public class XxxServer implements IRequestServer {
41+
42+
@Override
43+
public String getHost() {
44+
return "https://www.xxxxxxx.com/";
45+
}
46+
47+
@Override
48+
public String getPath() {
49+
return "api/";
50+
}
51+
}
52+
```
53+
54+
* 再将它应用到全局配置中
55+
56+
```
57+
EasyConfig.getInstance().setServer(new XxxServer());
58+
```
59+
60+
* 如果只是针对某个接口可以这样配置
61+
62+
```
63+
public final class XxxApi extends XxxServer implements IRequestApi {
64+
65+
@Override
66+
public String getApi() {
67+
return "xxx/xxxx";
68+
}
69+
}
70+
```
71+
72+
* 如果不想单独定义一个类,也可以这样写
73+
74+
```
75+
public final class XxxApi implements IRequestServer, IRequestApi {
76+
77+
@Override
78+
public String getHost() {
79+
return "https://www.xxxxxxx.com/";
80+
}
81+
82+
@Override
83+
public String getPath() {
84+
return "api/";
85+
}
86+
87+
@Override
88+
public String getApi() {
89+
return "xxx/xxxx";
90+
}
91+
}
92+
```
93+
94+
#### 如何修改参数的提交方式?
95+
96+
* 以表单的形式提交参数(默认)
97+
98+
```
99+
public class XxxServer implements IRequestServer {
100+
101+
@Override
102+
public String getHost() {
103+
return "https://www.xxxxxxx.com/";
104+
}
105+
106+
@Override
107+
public String getPath() {
108+
return "api/";
109+
}
110+
111+
@Override
112+
public BodyType getType() {
113+
return BodyType.FORM;
114+
}
115+
}
116+
```
117+
118+
* 以 Json 的形式提交参数
119+
120+
```
121+
public class XxxServer implements IRequestServer {
122+
123+
@Override
124+
public String getHost() {
125+
return "https://www.xxxxxxx.com/";
126+
}
127+
128+
@Override
129+
public String getPath() {
130+
return "api/";
131+
}
132+
133+
@Override
134+
public BodyType getType() {
135+
return BodyType.JSON;
136+
}
137+
}
138+
```
139+
140+
* 当然也支持对某个接口进行单独配置,和上面的雷同,这里略过
141+
142+
* 表单和 Json 方式提交的优缺点对比
143+
144+
| 场景 | 表单方式 | Json 方式 |
145+
| :----: | :------: | :-----: |
146+
| 参数嵌套 | 不支持 | 支持 |
147+
| 文件上传 | 支持 | 不支持 |
148+
149+
#### 如何忽略某个参数?
150+
151+
```
152+
public final class XxxApi implements IRequestApi {
153+
154+
@Override
155+
public String getApi() {
156+
return "xxx/xxxx";
157+
}
158+
159+
@HttpIgnore
160+
private String address;
161+
}
162+
```
163+
164+
#### 如何传入请求头?
165+
166+
```
167+
public final class XxxApi implements IRequestApi {
168+
169+
@Override
170+
public String getApi() {
171+
return "xxx/xxxx";
172+
}
173+
174+
@HttpHeader
175+
private String time;
176+
}
177+
```
178+
179+
#### 如何重命名参数名称?
180+
181+
```
182+
public final class XxxApi implements IRequestApi {
183+
184+
@Override
185+
public String getApi() {
186+
return "xxx/xxxx";
187+
}
188+
189+
@HttpRename("k")
190+
private String keyword;
191+
}
192+
```
193+
194+
#### 如何上传文件?
195+
196+
* 使用 File 对象上传
197+
198+
```
199+
public final class XxxApi implements IRequestApi {
200+
201+
@Override
202+
public String getApi() {
203+
return "xxx/xxxx";
204+
}
205+
206+
private File file;
207+
}
208+
```
209+
210+
* 使用 InputStream 对象上传
211+
212+
```
213+
public final class XxxApi implements IRequestApi {
214+
215+
@Override
216+
public String getApi() {
217+
return "xxx/xxxx";
218+
}
219+
220+
private InputStream inputStream;
221+
}
222+
```
223+
224+
* 使用 RequestBody 对象上传
225+
226+
```
227+
public final class XxxApi implements IRequestApi {
228+
229+
@Override
230+
public String getApi() {
231+
return "xxx/xxxx";
232+
}
233+
234+
private RequestBody requestBody;
235+
}
236+
```
237+
238+
#### 如何上传文件列表?
239+
240+
```
241+
public final class XxxApi implements IRequestApi {
242+
243+
@Override
244+
public String getApi() {
245+
return "xxx/xxxx";
246+
}
247+
248+
private List<File> files;
249+
}
250+
```
251+
252+
#### 如何设置超时重试?
253+
254+
```
255+
EasyConfig.getInstance().setRetryCount(3);
256+
```
257+
258+
#### 如何设置不打印日志?
259+
260+
```
261+
EasyConfig.getInstance().setLogEnabled(false);
262+
```

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818

1919
dependencies {
20-
implementation 'com.hjq:http:6.8'
20+
implementation 'com.hjq:http:6.9'
2121
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
2222
implementation 'com.google.code.gson:gson:2.8.5'
2323
}
@@ -79,7 +79,6 @@
7979

8080
#### 配置接口
8181

82-
@Keep
8382
public final class LoginApi implements IRequestApi {
8483

8584
@Override
@@ -112,13 +111,14 @@
112111

113112
* @HttpRename:重新定义这个字段发送给后台的参数名称
114113

115-
* 可为这个类多实现一些配置接口
114+
* 可在这个类实现一些接口
116115

117116
* implements IRequestHost:实现这个接口之后可以重新指定这个请求的主机地址
118117

119118
* implements IRequestPath:实现这个接口之后可以重新指定这个请求的接口路径
120119

121-
* implements IRequestType:实现这个接口之后可以重新指定这个请求的提交方式
120+
* implements IRequestType:实现这个接口之后可以重新指定这个请求的提交方式
121+
122122

123123
#### 发起请求
124124

@@ -208,6 +208,21 @@
208208
-keepclassmembernames class com.hjq.http.demo.http.** {
209209
<fields>;
210210
}
211+
212+
#### 对比 Retrofit
213+
214+
| 功能 | Retrofit 框架 | EasyHttp 框架 |
215+
| :----: | :------: | :-----: |
216+
| 动态 URL | 不支持 | 支持 |
217+
| 全局参数 | 不支持 | 支持 |
218+
| 超时重试 | 不支持 | 支持 |
219+
| 极速下载 | 不支持 | 支持 |
220+
| 下载校验 | 不支持 | 支持 |
221+
| 注解数量 | 25 个 | 3 个 |
222+
| 上传文件 | RequestBody | File / InputStream |
223+
| 请求管理 | 需要封装 | 自动管控 |
224+
225+
#### 具体用法可以[点击这里查看](HelpDoc.md)
211226

212227
#### 作者的其他开源项目
213228

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ android {
1313
applicationId "com.hjq.http.demo"
1414
minSdkVersion 14
1515
targetSdkVersion 28
16-
versionCode 68
17-
versionName "6.8"
16+
versionCode 69
17+
versionName "6.9"
1818
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1919
}
2020

2121
buildTypes {
2222
debug {
23-
minifyEnabled true
23+
minifyEnabled false
2424
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2525
}
2626
release {
@@ -45,7 +45,7 @@ dependencies {
4545
// 标题栏:https://github.com/getActivity/TitleBar
4646
implementation 'com.hjq:titlebar:6.5'
4747
// 吐司工具类:https://github.com/getActivity/ToastUtils
48-
implementation 'com.hjq:toast:8.2'
48+
implementation 'com.hjq:toast:8.3'
4949
// 权限请求框架:https://github.com/getActivity/XXPermissions
5050
implementation 'com.hjq:xxpermissions:6.5'
5151
// Json 解析框架:https://github.com/google/gson

app/src/main/java/com/hjq/http/demo/MainActivity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import androidx.core.content.FileProvider;
1717

1818
import com.hjq.http.EasyHttp;
19+
import com.hjq.http.EasyLog;
1920
import com.hjq.http.demo.http.model.HttpData;
2021
import com.hjq.http.demo.http.request.SearchAuthorApi;
2122
import com.hjq.http.demo.http.request.SearchBlogsApi;
@@ -108,7 +109,7 @@ public void onClick(View v) {
108109

109110
@Override
110111
public void onSucceed(HttpData<SearchBean> result) {
111-
ToastUtils.show("请求成功");
112+
ToastUtils.show("请求成功,请看日志");
112113
}
113114
});
114115
break;
@@ -120,7 +121,7 @@ public void onSucceed(HttpData<SearchBean> result) {
120121

121122
@Override
122123
public void onSucceed(HttpData<SearchBean> result) {
123-
ToastUtils.show("请求成功");
124+
ToastUtils.show("请求成功,请看日志");
124125
}
125126
});
126127
break;
@@ -239,7 +240,7 @@ private void drawableToFile(Drawable drawable, File file) {
239240
((BitmapDrawable) drawable).getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
240241
out.close();
241242
} catch (IOException e) {
242-
e.printStackTrace();
243+
EasyLog.print(e);
243244
}
244245
}
245246
}

0 commit comments

Comments
 (0)