Skip to content

Commit 0b6471f

Browse files
committed
新增网络请求代码日志定位功能
新增 Https 相关证书类及文档 优化框架代码逻辑嵌套及代码注释 新增支持为单个接口设置请求处理器
1 parent e163f27 commit 0b6471f

Some content is hidden

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

57 files changed

+1607
-1515
lines changed

.gitignore

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
*.iml
2-
.gradle
3-
/local.properties
4-
/.idea/workspace.xml
5-
/.idea/libraries
6-
.DS_Store
1+
/.gradle
2+
/.idea
73
/build
4+
*/build
85
/captures
9-
/.idea
6+
/.cxx
7+
/.externalNativeBuild
108

11-
/app/*.pro
12-
13-
/library/*.pro
9+
*.iml
10+
.DS_Store
11+
local.properties

EasyHttp.apk

-39.3 KB
Binary file not shown.

HelpDoc.md

+129-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。
2525
如果当前应用的请求是 http 请求,而非 https ,这样就会导系统禁止当前应用进行该请求,如果 WebView 的 url 用 http 协议,同样会出现加载失败,https 不受影响
2626

27-
* 在 res 下新建一个 xml 目录,然后创建一个名为:network_security_config.xml 文件 ,该文件内容如下
27+
* 在 res 下新建一个 xml 目录,然后创建一个名为:`network_security_config.xml` 文件 ,该文件内容如下
2828

2929
```xml
3030
<?xml version="1.0" encoding="utf-8"?>
@@ -65,7 +65,7 @@ public class RequestServer implements IRequestServer {
6565

6666
#### 框架初始化
6767

68-
* 需要配置请求结果处理,具体封装可以参考 [RequestHandler](app/src/main/java/com/hjq/http/demo/http/model/RequestHandler.java)
68+
* 需要配置请求结果处理,具体封装可以参考 [RequestHandler](app/src/main/java/com/hjq/easy/demo/http/model/RequestHandler.java)
6969

7070
```java
7171
OkHttpClient okHttpClient = new OkHttpClient.Builder()
@@ -107,7 +107,7 @@ EasyConfig.getInstance()
107107
-dontwarn okio.**
108108
109109
# 不混淆这个包下的字段名
110-
-keepclassmembernames class com.hjq.http.demo.http.** {
110+
-keepclassmembernames class com.xxx.xxx.xxx.xxx.** {
111111
<fields>;
112112
}
113113
```
@@ -353,7 +353,7 @@ String host = server.getHost();
353353
String path = server.getPath();
354354
```
355355

356-
#### 如何修改服务器配置
356+
#### 如何修改接口的服务器配置
357357

358358
* 先定义一个服务器配置
359359

@@ -412,6 +412,85 @@ public final class XxxApi implements IRequestServer, IRequestApi {
412412
}
413413
```
414414

415+
#### 如何配置多域名?
416+
417+
* 先定义一个普通接口的测试服和正式服的配置
418+
419+
```java
420+
public class TestServer implements IRequestServer {
421+
422+
@Override
423+
public String getHost() {
424+
return "https://www.test.xxxxxxx.com/";
425+
}
426+
427+
@Override
428+
public String getPath() {
429+
return "api/";
430+
}
431+
}
432+
```
433+
434+
```java
435+
public class ReleaseServer implements IRequestServer {
436+
437+
@Override
438+
public String getHost() {
439+
return "https://www.xxxxxxx.com/";
440+
}
441+
442+
@Override
443+
public String getPath() {
444+
return "api/";
445+
}
446+
}
447+
```
448+
449+
* 再将它应用到全局配置中
450+
451+
```java
452+
IRequestServer server;
453+
if (BuildConfig.DEBUG) {
454+
server = new TestServer();
455+
} else {
456+
server = new ReleaseServer();
457+
}
458+
EasyConfig.getInstance().setServer(server);
459+
```
460+
461+
* 假设要为 H5 业务模块设定特定服务器配置,可以这样做
462+
463+
```java
464+
public class H5Server implements IRequestServer {
465+
466+
@Override
467+
public String getHost() {
468+
IRequestServer server = EasyConfig.getInstance().getServer();
469+
if (server instanceof TestServer) {
470+
return "https://www.test.h5.xxxxxxx.com/";
471+
}
472+
return "https://www.h5.xxxxxxx.com/";
473+
}
474+
475+
@Override
476+
public String getPath() {
477+
return "api/";
478+
}
479+
}
480+
```
481+
482+
* 在配置接口的时候继承 H5Server 就可以了,其他 H5 模块的配置也是雷同
483+
484+
```java
485+
public final class UserAgreementApi extends H5Server implements IRequestApi {
486+
487+
@Override
488+
public String getApi() {
489+
return "user/agreement";
490+
}
491+
}
492+
```
493+
415494
#### 如何修改参数的提交方式?
416495

417496
* 以表单的形式提交参数(默认)
@@ -482,6 +561,39 @@ public final class XxxApi implements IRequestApi, IRequestType {
482561
| 参数嵌套 | 不支持 | 支持 |
483562
| 文件上传 | 支持 | 不支持 |
484563

564+
#### 如何对整个 Json 字符串进行加密?
565+
566+
* 这块的需求比较奇葩,但是搭配 OkHttp 拦截器仍然是可以实现的,这得益于 EasyHttp 良好的框架设计
567+
568+
```java
569+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
570+
.addInterceptor(new Interceptor() {
571+
@Override
572+
public Response intercept(Chain chain) throws IOException {
573+
Request request = chain.request();
574+
RequestBody body = request.body();
575+
if (body instanceof JsonBody) {
576+
body = new JsonBody("假装加密了:" + ((JsonBody) body).getJson());
577+
Request.Builder builder = request.newBuilder();
578+
builder.method(request.method(), body);
579+
request = builder.build();
580+
}
581+
return chain.proceed(request);
582+
}
583+
})
584+
.build();
585+
```
586+
587+
* 在 Application 初始化 EasyHttp 的时候配置进去
588+
589+
```java
590+
EasyConfig.with(okHttpClient)
591+
//.setXxxx(Xxxx)
592+
//.setXxxx(Xxxx)
593+
//.setXxxx(Xxxx)
594+
.into();
595+
```
596+
485597
#### 如何忽略某个参数?
486598

487599
```java
@@ -512,7 +624,7 @@ public final class XxxApi implements IRequestApi {
512624
}
513625
```
514626

515-
#### 如何重命名参数名称
627+
#### 如何重命名参数/请求头的名称
516628

517629
```java
518630
public final class XxxApi implements IRequestApi {
@@ -683,6 +795,18 @@ EasyHttp.cancel(Object tag);
683795
EasyHttp.cancel();
684796
```
685797

798+
#### Https 如何配置信任所有证书?
799+
800+
* 在初始化 OkHttp 的时候这样设置,但是不推荐这样做,因为这样是不安全的,意味着每个请求都不会 Https 去校验
801+
802+
```java
803+
HttpSslConfig sslConfig = HttpSslFactory.generateSslConfig();
804+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
805+
.sslSocketFactory(sslConfig.getsSLSocketFactory(), sslConfig.getTrustManager())
806+
.hostnameVerifier(HttpSslFactory.generateUnSafeHostnameVerifier())
807+
.build();
808+
```
809+
686810
#### getHost、getPath、getApi 方法之间的作用和区别?
687811

688812
* Host:服务器主机的地址

README.md

+46-33
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
# 简单易用的网络框架
22

3-
> 码云地址:[Gitee](https://gitee.com/getActivity/EasyHttp)
3+
* 码云地址:[Gitee](https://gitee.com/getActivity/EasyHttp)
44

5-
> [点击此处下载Demo](EasyHttp.apk)
5+
* 博客地址:[网络请求,如斯优雅](https://www.jianshu.com/p/93cd59dec002)
6+
7+
* [点击此处下载Demo](EasyHttp.apk)
68

79
![](EasyHttp.jpg)
810

9-
> 另外对 OkHttp 源码感兴趣的同学可以看下面几篇文章
11+
* 另外对 OkHttp 原理感兴趣的同学推荐你看以下源码分析文章
1012

11-
* [OkHttp 精讲:拦截器执行原理](https://www.jianshu.com/p/e0f324fd9411)
13+
* [OkHttp 精讲:拦截器执行原理](https://www.jianshu.com/p/e0f324fd9411)
1214

13-
* [OkHttp 精讲:RetryAndFollowUpInterceptor](https://www.jianshu.com/p/40636d32cb67)
15+
* [OkHttp 精讲:RetryAndFollowUpInterceptor](https://www.jianshu.com/p/40636d32cb67)
1416

15-
* [OkHttp 精讲:BridgeInterceptor](https://www.jianshu.com/p/fab2d74de900)
17+
* [OkHttp 精讲:BridgeInterceptor](https://www.jianshu.com/p/fab2d74de900)
1618

17-
* [OkHttp 精讲:CacheInterceptor](https://www.jianshu.com/p/44fad764c0ae)
19+
* [OkHttp 精讲:CacheInterceptor](https://www.jianshu.com/p/44fad764c0ae)
1820

19-
* [OkHttp 精讲:ConnectInterceptor](https://www.jianshu.com/p/a3a774fdff4f)
21+
* [OkHttp 精讲:ConnectInterceptor](https://www.jianshu.com/p/a3a774fdff4f)
2022

21-
* [OkHttp 精讲:CallServerInterceptor](https://www.jianshu.com/p/aa77af6251ff)
23+
* [OkHttp 精讲:CallServerInterceptor](https://www.jianshu.com/p/aa77af6251ff)
2224

2325
#### Gradle 集成
2426

@@ -33,38 +35,40 @@ android {
3335
3436
dependencies {
3537
// 网络请求框架:https://github.com/getActivity/EasyHttp
36-
implementation 'com.hjq:http:8.9'
38+
implementation 'com.hjq:http:9.0'
3739
// OkHttp 框架:https://github.com/square/okhttp
3840
// noinspection GradleDependency
3941
implementation 'com.squareup.okhttp3:okhttp:3.12.12'
4042
}
4143
```
4244
4345
## [框架的具体用法请点击这里查看](HelpDoc.md)
44-
45-
#### 不同网络请求框架之间的对比
4646

47-
| 功能 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
47+
### 不同网络请求框架之间的对比
48+
49+
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
4850
| :----: | :------: | :-----: | :-----: |
49-
| 对应版本 | 8.9 | 2.9.0 | 3.0.4 |
50-
| 动态 Host | 支持 | 不支持 | 支持 |
51+
| 对应版本 | 9.0 | 2.9.0 | 3.0.4 |
52+
| **aar 包大小** | [61 KB](https://bintray.com/getactivity/maven/http#files/com/hjq/http) | [123 KB](https://bintray.com/bintray/jcenter/com.squareup.retrofit2%3Aretrofit#files) | [131 KB](https://bintray.com/jeasonlzy/maven/okgo#files/com/lzy/net/okgo) |
53+
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
54+
| 配置多域名 | 支持 | 不支持 | 支持 |
55+
| **动态 Host** | 支持 | 不支持 | 不支持 |
5156
| 全局参数 | 支持 | 不支持 | 支持 |
5257
| 超时重试 | 支持 | 不支持 | 支持 |
53-
| 下载校验 | 支持 | 不支持 | 不支持 |
54-
| 极速下载 | 支持 | 不支持 | 不支持 |
55-
| 注解数量 | 3 个 | 25 个 | 0 个 |
58+
| **下载校验** | 支持 | 不支持 | 不支持 |
59+
| **极速下载** | 支持 | 不支持 | 不支持 |
5660
| 上传文件类型 | File / InputStream / RequestBody | RequestBody | File |
5761
| 批量上传文件 | 支持 | 不支持 | 支持 |
5862
| 上传进度监听 | 支持 | 不支持 | 支持 |
59-
| Json 参数提交 | 支持 | 不支持 | 支持 |
60-
| 请求生命周期 | 自动管控 | 需要封装 | 需要封装 |
61-
| 参数传值方式 | 字段名 + 字段值 | 方法参数名 + 方法参数值 | 定义 Key + Value |
63+
| Json 参数提交 | 支持 | 不支持 | 支持 |
64+
| **请求代码定位** | 支持 | 不支持 | 不支持 |
65+
| **请求生命周期** | 自动管控 | 需要封装 | 需要封装 |
66+
| 参数传值方式 | 字段名 + 字段值 | 参数名 + 参数值 | 定义 Key + Value |
6267
| 参数灵活性 | 不强制传入 | 强制全部传入 | 不强制传入 |
63-
| 自定义 RequestBody | 支持 | 支持 | 支持 |
64-
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
65-
| class 文件数量 | 70 个 | 54 个 | 85 个 |
66-
| aar 包大小 | [55 KB](https://bintray.com/getactivity/maven/http#files/com/hjq/http) | [123 KB](https://bintray.com/bintray/jcenter/com.squareup.retrofit2%3Aretrofit#files) | [131 KB](https://bintray.com/jeasonlzy/maven/okgo#files/com/lzy/net/okgo) |
67-
| 框架维护状态 | 维护中 | 维护中 | 停止维护 |
68+
| 框架学习成本 ||||
69+
| **API 记忆成本** ||||
70+
| **接口维护成本** ||||
71+
| 框架维护状态 | 维护中 | 维护中 | 停止维护 |
6872

6973
* Retrofit 在我看来并不是那么好用,因为很多常用的功能实现起来比较麻烦,动态 Host 要写拦截器,日志打印要写拦截器,就连最常用的添加全局参数也要写拦截器,一个拦截器意味着要写很多代码,如果写得不够严谨还有可能出现 Bug,从而影响整个 OkHttp 请求流程,我经常在想这些功能能不能都用一句代码搞定,因为我觉得这些功能是设计框架的时候本应该考虑的,这便是我做这个框架的初心。
7074

@@ -76,25 +80,33 @@ dependencies {
7680

7781
#### 极速下载功能介绍
7882

79-
* 其实本质上面和极速秒传的原理是差不多的,只不过一个是上传,一个是下载。而极速上传是将本地文件的 MD5 值和服务器上面的进行比对,如果服务器存在这个 MD5 值的文件,就将这份文件映射一份到这个用户的网盘上面,从而达到了极速秒传的效果。而极速下载也是同理,根据后台给的文件 MD5 值和本地文件进行对比,如果存在这个文件并且 MD5 值一致,证明这个文件和服务器上面的文件是一致的,那么就直接跳过下载,直接回调下载成功监听。
83+
* 其实本质上面和极速秒传的原理是差不多的,只不过一个是上传,另一个是下载。而极速上传是将本地文件的 MD5 值和服务器上面的进行比对,如果服务器存在这个 MD5 值的文件,就将这份文件映射一份到这个用户的网盘上面,从而达到了极速秒传的效果。而极速下载也是同理,根据后台给的文件 MD5 值和本地文件进行对比,如果存在这个文件并且 MD5 值一致,证明这个文件和服务器上面的文件是一致的,那么就直接跳过下载,直接回调下载成功监听。
8084

81-
* 极速秒传和极速下载两者相同的共同点就是,利用缓存来达到极速的效果,只不过一者通过的是服务器的缓存,另一者使用的是本地的缓存,这两者都有一个共同的特点,就是减少服务器的压力,节省用户的等待时间。
85+
* 极速秒传和极速下载两者相同的共同点就是,利用缓存来达到极速的效果,只不过一者通过的是服务器的缓存,另一者使用的是本地的缓存,这两者都有一个共同的特点,就是减少服务器的压力,节省用户的等待时间。
86+
87+
#### 代码定位功能介绍
88+
89+
* 框架会在日志打印中输出在网络请求的代码位置,这样开发者可以直接通过点击 Log 来定位代码是在哪个类哪行代码,这样可以极大提升我们排查问题的效率,特别是在请求一多且业务复杂的情况下,我相信没有一个人会拒绝这样的功能。
90+
91+
![](RequestCode.png)
8292

8393
#### 作者的其他开源项目
8494

8595
* 安卓技术中台:[AndroidProject](https://github.com/getActivity/AndroidProject)
8696

87-
* 日志框架:[Logcat](https://github.com/getActivity/Logcat)
97+
* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions)
8898

8999
* 吐司框架:[ToastUtils](https://github.com/getActivity/ToastUtils)
90100

91-
* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions)
92-
93101
* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar)
94102

95103
* 国际化框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages)
96104

97-
* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast)
105+
* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast)
106+
107+
* 日志框架:[Logcat](https://github.com/getActivity/Logcat)
108+
109+
* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory)
98110

99111
#### Android技术讨论Q群:78797078
100112

@@ -125,4 +137,5 @@ Unless required by applicable law or agreed to in writing, software
125137
distributed under the License is distributed on an "AS IS" BASIS,
126138
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127139
See the License for the specific language governing permissions and
128-
limitations under the License.
140+
limitations under the License.
141+
```

RequestCode.png

412 KB
Loading

app/.gitignore

-1
This file was deleted.

0 commit comments

Comments
 (0)