Skip to content

Commit c3dc96d

Browse files
committed
新增支持同步请求
新增支持上传进度监听 新增支持 Head 请求 新增支持 Delete 请求 新增支持 Put 请求 新增支持 Patch 请求 优化框架的使用文档 优化下载进度监听器
1 parent 18972a1 commit c3dc96d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1762
-786
lines changed

EasyHttp.apk

-313 KB
Binary file not shown.

EasyHttp.jpg

13.6 KB
Loading

HelpDoc.md

+362-5
Large diffs are not rendered by default.

README.md

+60-214
Original file line numberDiff line numberDiff line change
@@ -8,225 +8,71 @@
88

99
#### Gradle 集成
1010

11-
android {
12-
// 支持 JDK 1.8
13-
compileOptions {
14-
targetCompatibility JavaVersion.VERSION_1_8
15-
sourceCompatibility JavaVersion.VERSION_1_8
16-
}
17-
}
18-
19-
dependencies {
20-
implementation 'com.hjq:http:8.2'
21-
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
22-
implementation 'com.google.code.gson:gson:2.8.5'
11+
```groovy
12+
android {
13+
// 支持 JDK 1.8
14+
compileOptions {
15+
targetCompatibility JavaVersion.VERSION_1_8
16+
sourceCompatibility JavaVersion.VERSION_1_8
2317
}
18+
}
19+
20+
dependencies {
21+
implementation 'com.hjq:http:8.6'
22+
implementation 'com.squareup.okhttp3:okhttp:3.12.12'
23+
implementation 'com.google.code.gson:gson:2.8.5'
24+
}
25+
```
26+
27+
#### 具体用法[请点击这里查看](HelpDoc.md)
28+
29+
#### 不同网络请求框架之间的对比
2430

25-
#### 配置权限
26-
27-
<!-- 联网权限 -->
28-
<uses-permission android:name="android.permission.INTERNET" />
29-
30-
<!-- 访问网络状态 -->
31-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
32-
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
33-
34-
#### 服务器配置
35-
36-
public class RequestServer implements IRequestServer {
37-
38-
@Override
39-
public String getHost() {
40-
return "https://www.baidu.com/";
41-
}
42-
43-
@Override
44-
public String getPath() {
45-
return "api/";
46-
}
47-
48-
@Override
49-
public BodyType getType() {
50-
// 参数以 Json 格式提交(默认是表单)
51-
return BodyType.JSON;
52-
}
53-
}
54-
55-
#### 初始化
56-
57-
> 需要配置请求结果处理,具体封装可以参考 [RequestHandler](https://github.com/getActivity/EasyHttp/blob/master/app/src/main/java/com/hjq/http/demo/http/model/RequestHandler.java)
58-
59-
EasyConfig.with(new OkHttpClient())
60-
// 是否打印日志
61-
.setLogEnabled(BuildConfig.DEBUG)
62-
// 设置服务器配置
63-
.setServer(server)
64-
// 设置请求处理策略
65-
.setHandler(new RequestHandler())
66-
// 设置请求重试次数
67-
.setRetryCount(3)
68-
// 添加全局请求参数
69-
//.addParam("token", "6666666")
70-
// 添加全局请求头
71-
//.addHeader("time", "20191030")
72-
// 启用配置
73-
.into();
74-
75-
> 上述是创建配置,更新配置可以使用
76-
77-
EasyConfig.getInstance()
78-
.addParam("token", data.getData().getToken());
79-
80-
#### 配置接口
81-
82-
public final class LoginApi implements IRequestApi {
83-
84-
@Override
85-
public String getApi() {
86-
return "user/login";
87-
}
88-
89-
/** 用户名 */
90-
private String userName;
91-
92-
/** 登录密码 */
93-
private String password;
94-
95-
public LoginApi setUserName(String userName) {
96-
this.userName = userName;
97-
return this;
98-
}
99-
100-
public LoginApi setPassword(String password) {
101-
this.password = password;
102-
return this;
103-
}
104-
}
105-
106-
* 可为这个类的字段加上一些注解
107-
108-
* @HttpHeader:标记这个字段是一个请求头参数
109-
110-
* @HttpIgnore:标记这个字段不会被发送给后台
111-
112-
* @HttpRename:重新定义这个字段发送给后台的参数名称
113-
114-
* 可在这个类实现一些接口
115-
116-
* implements IRequestHost:实现这个接口之后可以重新指定这个请求的主机地址
117-
118-
* implements IRequestPath:实现这个接口之后可以重新指定这个请求的接口路径
119-
120-
* implements IRequestType:实现这个接口之后可以重新指定这个请求的提交方式
121-
122-
* 具体用法可以[点击这里查看](HelpDoc.md)
123-
124-
#### 发起请求
125-
126-
> 需要配置请求状态及生命周期处理,具体封装可以参考 [BaseActivity](https://github.com/getActivity/EasyHttp/blob/master/app/src/main/java/com/hjq/http/demo/BaseActivity.java)
127-
128-
EasyHttp.post(this)
129-
.api(new LoginApi()
130-
.setUserName("Android 轮子哥")
131-
.setPassword("123456"))
132-
.request(new HttpCallback<HttpData<LoginBean>>(activity) {
133-
134-
@Override
135-
public void onSucceed(HttpData<LoginBean> data) {
136-
ToastUtils.show("登录成功");
137-
}
138-
});
139-
140-
#### 下载文件
141-
142-
> 下载缓存策略:在指定下载文件 md5 或者后台有返回 md5 的情况下,下载框架默认开启下载缓存模式,如果这个文件已经存在手机中,并且经过 md5 校验文件完整,框架就不会重复下载,而是直接回调下载监听。减轻服务器压力,减少用户等待时间。
143-
144-
EasyHttp.download(this)
145-
.method(HttpMethod.GET)
146-
.file(new File(Environment.getExternalStorageDirectory(), "微信.apk"))
147-
.url("http://dldir1.qq.com/weixin/android/weixin708android1540.apk")
148-
.md5("2E8BDD7686474A7BC4A51ADC3667CABF")
149-
.listener(new OnDownloadListener() {
150-
151-
@Override
152-
public void onStart(Call call) {
153-
mProgressBar.setVisibility(View.VISIBLE);
154-
ToastUtils.show("下载开始");
155-
}
156-
157-
@Override
158-
public void onProgress(DownloadInfo info) {
159-
mProgressBar.setProgress(info.getDownloadProgress());
160-
}
161-
162-
@Override
163-
public void onComplete(DownloadInfo info) {
164-
ToastUtils.show("下载完成:" + info.getFile().getPath());
165-
installApk(MainActivity.this, info.getFile());
166-
}
167-
168-
@Override
169-
public void onError(DownloadInfo info, Exception e) {
170-
ToastUtils.show("下载出错:" + e.getMessage());
171-
}
172-
173-
@Override
174-
public void onEnd(Call call) {
175-
mProgressBar.setVisibility(View.GONE);
176-
ToastUtils.show("下载结束");
177-
}
178-
179-
}).start();
31+
| 功能 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
32+
| :----: | :------: | :-----: | :-----: |
33+
| 动态 Host | 支持 | 不支持 | 支持 |
34+
| 全局参数 | 支持 | 不支持 | 支持 |
35+
| 超时重试 | 支持 | 不支持 | 支持 |
36+
| 极速下载 | 支持 | 不支持 | 不支持 |
37+
| 下载校验 | 支持 | 不支持 | 不支持 |
38+
| 注解数量 | 3 个 | 25 个 | 0 个 |
39+
| 上传文件类型 | File / InputStream | RequestBody | File |
40+
| 批量上传文件 | 支持 | 不支持 | 支持 |
41+
| 上传进度监听 | 支持 | 不支持 | 支持 |
42+
| Json 参数提交 | 支持 | 支持 | 支持 |
43+
| 请求生命周期 | 自动管控 | 需要封装 | 需要封装 |
44+
| 参数传值方式 | 字段名 + 字段值 | 方法参数名 + 方法参数值 | 定义 key 和 value |
45+
| 参数灵活性 | 不强制传入 | 强制全部传入 | 不强制传入 |
46+
| 框架维护状态 | 维护中 | 维护中 | 停止维护 |
18047

181-
#### 关于 Http 明文请求
182-
183-
> Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。
184-
如果当前应用的请求是 http 请求,而非 https ,这样就会导系统禁止当前应用进行该请求,如果 WebView 的 url 用 http 协议,同样会出现加载失败,https 不受影响
185-
186-
> 在 res 下新建一个 xml 目录,然后创建一个名为:network_security_config.xml 文件 ,该文件内容如下
48+
* Retrofit 在我看来并不是那么好用,因为很多常用的功能实现起来比较麻烦,动态 Host 要写拦截器,日志打印要写拦截器,就连最常用的添加全局参数也要写拦截器,一个拦截器意味着要写很多代码,如果写得不够严谨还有可能出现 Bug,从而影响整个 OkHttp 请求流程,我经常在想这些功能能不能都用一句代码搞定,因为我觉得这些功能是框架设计的时候应该考虑的,这便是我做这个框架的初心。
18749

188-
<?xml version="1.0" encoding="utf-8"?>
189-
<network-security-config>
190-
<base-config cleartextTrafficPermitted="true" />
191-
</network-security-config>
50+
* 本框架采用了 OOP 思想,一个请求代表一个对象,通过类的继承和实现的特性来实现接口的动态化,几乎涵盖接口开发中所有的功能,使用起来非常简单灵活。
19251

193-
> 然后在 AndroidManifest.xml application 标签内应用上面的xml配置
52+
* 有很多人觉得写一个接口类很麻烦,这个点确实有点麻烦,但是这块的付出是有收获的,从前期开发的效率考虑:OkGo > EasyHttp > Retrofit,但是从后期维护的效率考虑:EasyHttp > Retrofit > OkGo,之所以比较这三个框架,是因为框架的设计思想不同,但是我始终认为 EasyHttp 才是最好的设计,所以我创造了它。
19453

195-
<application
196-
android:networkSecurityConfig="@xml/network_security_config" />
54+
* 前期开发和后期维护哪个更重要?我觉得都重要,但是如果两者之间有利益冲突,我会毫不犹豫选择后期维护,因为前期开发占据的是小头,后期的持续维护才是大头。
19755

19856
#### 混淆规则
199-
200-
# OkHttp3
201-
-keepattributes Signature
202-
-keepattributes *Annotation*
203-
-keep class okhttp3.** { *; }
204-
-keep interface okhttp3.** { *; }
205-
-dontwarn okhttp3.**
206-
-dontwarn okio.**
207-
208-
# 不混淆这个包下的字段名
209-
-keepclassmembernames class com.hjq.http.demo.http.** {
210-
<fields>;
211-
}
212-
213-
#### 对比 Retrofit
214-
215-
| 功能 | Retrofit 框架 | EasyHttp 框架 |
216-
| :----: | :------: | :-----: |
217-
| 动态 Host | 不支持 | 支持 |
218-
| 全局参数 | 不支持 | 支持 |
219-
| 动态参数 | 不支持 | 支持 |
220-
| 超时重试 | 不支持 | 支持 |
221-
| 极速下载 | 不支持 | 支持 |
222-
| 下载校验 | 不支持 | 支持 |
223-
| 注解数量 | 25 个 | 3 个 |
224-
| 上传文件 | RequestBody | File / InputStream |
225-
| 生命周期 | 需要封装 | 自动管控 |
57+
58+
```groovy
59+
# OkHttp3
60+
-keepattributes Signature
61+
-keepattributes *Annotation*
62+
-keep class okhttp3.** { *; }
63+
-keep interface okhttp3.** { *; }
64+
-dontwarn okhttp3.**
65+
-dontwarn okio.**
66+
67+
# 不混淆这个包下的字段名
68+
-keepclassmembernames class com.hjq.http.demo.http.** {
69+
<fields>;
70+
}
71+
```
22672

22773
#### 作者的其他开源项目
22874

229-
* 架构工程[AndroidProject](https://github.com/getActivity/AndroidProject)
75+
* 安卓架构[AndroidProject](https://github.com/getActivity/AndroidProject)
23076

23177
* 日志框架:[Logcat](https://github.com/getActivity/Logcat)
23278

@@ -240,12 +86,6 @@
24086

24187
* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast)
24288

243-
#### 特别感谢
244-
245-
[张鸿洋](https://github.com/hongyangAndroid)
246-
247-
[WanAndroid](https://www.wanandroid.com/)
248-
24989
#### Android技术讨论Q群:78797078
25090

25191
#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:
@@ -254,6 +94,12 @@
25494

25595
#### [点击查看捐赠列表](https://github.com/getActivity/Donate)
25696

97+
#### 特别感谢
98+
99+
[张鸿洋](https://github.com/hongyangAndroid)
100+
101+
[WanAndroid](https://www.wanandroid.com/)
102+
257103
## License
258104

259105
```text

app/build.gradle

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 28
4+
compileSdkVersion 30
5+
6+
lintOptions {
7+
abortOnError false
8+
}
59

610
// 支持 JDK 1.8
711
compileOptions {
@@ -12,9 +16,9 @@ android {
1216
defaultConfig {
1317
applicationId "com.hjq.http.demo"
1418
minSdkVersion 14
15-
targetSdkVersion 28
16-
versionCode 82
17-
versionName "8.2"
19+
targetSdkVersion 30
20+
versionCode 86
21+
versionName "8.6"
1822
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1923
}
2024

@@ -24,35 +28,37 @@ android {
2428
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2529
}
2630
release {
27-
minifyEnabled true
31+
minifyEnabled false
2832
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2933
}
3034
}
3135
}
3236

3337
dependencies {
38+
// 依赖 libs 目录下所有的 jar 和 aar 包
3439
implementation fileTree(include: ['*.jar'], dir: 'libs')
40+
implementation fileTree(include: ['*.aar'], dir: 'libs')
41+
3542
implementation project(':library')
3643

3744
testImplementation 'junit:junit:4.13'
38-
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
39-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
40-
45+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
46+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
4147

42-
implementation 'androidx.appcompat:appcompat:1.1.0'
43-
implementation 'com.google.android.material:material:1.1.0'
48+
// 谷歌兼容库:https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn
49+
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
4450

4551
// 标题栏:https://github.com/getActivity/TitleBar
46-
implementation 'com.hjq:titlebar:6.5'
52+
implementation 'com.hjq:titlebar:8.0'
4753
// 吐司工具类:https://github.com/getActivity/ToastUtils
48-
implementation 'com.hjq:toast:8.6'
54+
implementation 'com.hjq:toast:8.8'
4955
// 权限请求框架:https://github.com/getActivity/XXPermissions
50-
implementation 'com.hjq:xxpermissions:6.5'
56+
implementation 'com.hjq:xxpermissions:8.8'
5157
// Json 解析框架:https://github.com/google/gson
5258
implementation 'com.google.code.gson:gson:2.8.5'
5359
// OkHttp 框架:https://github.com/square/okhttp
5460
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
55-
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
61+
implementation 'com.squareup.okhttp3:okhttp:3.12.12'
5662
// 日志调试:https://github.com/getActivity/Logcat
57-
debugImplementation 'com.hjq:logcat:8.2'
63+
debugImplementation 'com.hjq:logcat:8.6'
5864
}

app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
1111

1212
<!-- 外部存储读写权限 -->
13+
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
1314
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1415
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1516

@@ -23,6 +24,7 @@
2324
android:networkSecurityConfig="@xml/network_security_config"
2425
android:roundIcon="@mipmap/ic_launcher_round"
2526
android:theme="@style/AppTheme"
27+
tools:ignore="LockedOrientationActivity"
2628
tools:targetApi="n">
2729

2830
<!-- 适配 Android 7.0 文件意图 -->

0 commit comments

Comments
 (0)