Skip to content

Commit ead06c8

Browse files
committed
优化和补充框架使用文档
优化框架部分 API 命名 优化延迟异步请求代码的写法 新增支持自定义 Content-Type 新增全局下载开始和成功回调方法
1 parent 4d6d78a commit ead06c8

26 files changed

+492
-197
lines changed

.github/ISSUE_TEMPLATE/issue_template_bug.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ assignees: getActivity
2222

2323
* 出现问题的安卓版本【必填】:请填写出现问题的 Android 版本
2424

25+
* 问题信息的来源渠道【必填】:请填写问题的来源(例如:自己遇到的/Bugly 看到的/用户反馈等等)
26+
2527
#### 请回答
2628

2729
* 是部分机型还是所有机型都会出现【必答】:部分/全部(例如:某为,某 Android 版本会出现)
@@ -34,7 +36,7 @@ assignees: getActivity
3436

3537
* 是否可以通过 Demo 来复现该问题【必答】:是/否(排查一下是不是自己的项目代码写得有问题导致的)
3638

37-
* 这个问题是不是后台自己的问题导致的【必答】:是/否(如果无法确定问题的原因,请先和后台开发人员协商联调,确认了问题是框架的再反馈给作者,如果是后台的问题作者也没用,最后还是要自己找后台处理才有用的)
39+
* 这个问题是不是后台自己的问题导致的【必答】:是/否(如果无法确定问题的原因,请先和后台开发人员协商联调,确认了问题是框架的再反馈给作者,如果是后台的问题找框架作者也没用,最后还是要自己找后台处理才有用的)
3840

3941
#### 其他
4042

HelpDoc.md

Lines changed: 143 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
* [下载文件](#下载文件)
2424

25-
* [同步请求](#同步请求)
25+
* [分区存储适配](#分区存储适配)
2626

27-
* [请求缓存](#请求缓存)
27+
* [发起同步请求](#发起同步请求)
2828

29-
* [搭配协程使用](#搭配协程使用)
29+
* [设置请求缓存](#设置请求缓存)
3030

31-
* [分区存储适配](#分区存储适配)
31+
* [搭配协程使用](#搭配协程使用)
3232

3333
* [疑难解答](#疑难解答)
3434

@@ -94,6 +94,10 @@
9494

9595
* [我想自定义一个 RequestBody 进行请求该怎么办](#我想自定义一个-requestbody-进行请求该怎么办)
9696

97+
* [我想自定义请求头中的 ContentType 该怎么做](#我想自定义请求头中的-contenttype-该怎么做)
98+
99+
* [我想自定义 Get 请求参数中的 key 和 value 该怎么做](#我想自定义-get-请求参数中的-key-和-value-该怎么做)
100+
97101
* [搭配 RxJava](#搭配-rxjava)
98102

99103
* [准备工作](#准备工作)
@@ -169,9 +173,9 @@ OkHttpClient okHttpClient = new OkHttpClient.Builder()
169173
EasyConfig.with(okHttpClient)
170174
// 是否打印日志
171175
.setLogEnabled(BuildConfig.DEBUG)
172-
// 设置服务器配置
176+
// 设置服务器配置(必须设置)
173177
.setServer(server)
174-
// 设置请求处理策略
178+
// 设置请求处理策略(必须设置)
175179
.setHandler(new RequestHandler())
176180
// 设置请求重试次数
177181
.setRetryCount(3)
@@ -413,7 +417,38 @@ EasyHttp.download(this)
413417
}).start();
414418
```
415419

416-
#### 同步请求
420+
#### 分区存储适配
421+
422+
* 在 Android 10 之前,我们在读写外部存储的时候,可以直接使用 File 对象来上传或者下载文件,但是在 Android 10 之后,如果你的项目需要 Android 10 分区存储的特性,那么在读写外部存储文件的时候,就不能直接使用 File 对象,因为 `ContentResolver.insert` 返回是一个 `Uri` 对象,这个时候就需要使用到框架提供的 `FileContentResolver` 对象了(这个对象是 File 的子类),具体使用案例如下:
423+
424+
```java
425+
File outputFile;
426+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
427+
ContentValues values = new ContentValues();
428+
.........
429+
// 生成一个新的 uri 路径
430+
Uri outputUri = getContentResolver().insert(MediaStore.Xxx.Media.EXTERNAL_CONTENT_URI, values);
431+
// 适配 Android 10 分区存储特性
432+
outputFile = new FileContentResolver(context, outputUri);
433+
} else {
434+
outputFile = new File(xxxx);
435+
}
436+
437+
EasyHttp.post(this)
438+
.api(new XxxApi()
439+
.setImage(outputFile))
440+
.request(new HttpCallback<Xxx <Xxx>>(this) {
441+
442+
@Override
443+
public void onSucceed(Xxx<Xxx> data) {
444+
445+
}
446+
});
447+
```
448+
449+
* 这是上传的案例,下载也同理,这里不再赘述。
450+
451+
#### 发起同步请求
417452

418453
* 需要注意的是:同步请求是耗时操作,不能在主线程中执行,请务必保证此操作在子线程中执行
419454

@@ -430,7 +465,7 @@ try {
430465
}
431466
```
432467

433-
#### 请求缓存
468+
#### 设置请求缓存
434469

435470
* 需要先实现读取和写入缓存的接口,如果已配置则可以跳过,这里以 MMKV 为例
436471

@@ -615,37 +650,6 @@ lifecycleScope.launch(Dispatchers.IO) {
615650

616651
* 如果你对协程的使用不太熟悉,推荐你看一下[这篇文章](https://www.jianshu.com/p/2e0746c7d4f3)
617652

618-
#### 分区存储适配
619-
620-
* 在 Android 10 之前,我们在读写外部存储的时候,可以直接使用 File 对象来上传或者下载文件,但是在 Android 10 之后,如果你的项目需要 Android 10 分区存储的特性,那么在读写外部存储文件的时候,就不能直接使用 File 对象,因为 `ContentResolver.insert` 返回是一个 `Uri` 对象,这个时候就需要使用到框架提供的 `FileContentResolver` 对象了(这个对象是 File 的子类),具体使用案例如下:
621-
622-
```java
623-
File outputFile;
624-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
625-
ContentValues values = new ContentValues();
626-
.........
627-
// 生成一个新的 uri 路径
628-
Uri outputUri = getContentResolver().insert(MediaStore.Xxx.Media.EXTERNAL_CONTENT_URI, values);
629-
// 适配 Android 10 分区存储特性
630-
outputFile = new FileContentResolver(context, outputUri);
631-
} else {
632-
outputFile = new File(xxxx);
633-
}
634-
635-
EasyHttp.post(this)
636-
.api(new XxxApi()
637-
.setImage(outputFile))
638-
.request(new HttpCallback<Xxx <Xxx>>(this) {
639-
640-
@Override
641-
public void onSucceed(Xxx<Xxx> data) {
642-
643-
}
644-
});
645-
```
646-
647-
* 这是上传的案例,下载也同理,这里不再赘述。
648-
649653
# 疑难解答
650654

651655
#### 如何设置 Cookie
@@ -1423,10 +1427,11 @@ parameter.put("key1", value1);
14231427
parameter.put("key2", value2);
14241428

14251429
String json = gson.toJson(parameter);
1430+
JsonBody jsonBody = new JsonBody(json)
14261431

14271432
EasyHttp.post(this)
14281433
.api(new XxxApi())
1429-
.body(new JsonBody(json))
1434+
.body(jsonBody)
14301435
.request(new HttpCallback<HttpData<Xxx>>(this) {
14311436

14321437
@Override
@@ -1438,7 +1443,7 @@ EasyHttp.post(this)
14381443

14391444
#### 如何设置自定义的 UA 标识
14401445

1441-
* 首先 UA 是 User Agent 的简称,当我们没有设置自定义 UA 标识的时候,那么 OkHttp 会在 BridgeInterceptor 拦截器添加一个默认的 UA 标识,那么如何在 EasyHttp 设置自定义 UA 标识呢?其实很简单,UA 标识本质上其实就是一个请求头,在 EasyHttp 中添加一个请求头为 `"User-Agent` 的参数即可,至于怎么添加请求头,前面的文档已经有介绍了,这里不再赘述。
1446+
* 首先 UA 是 User Agent 的简称,当我们没有设置自定义 UA 标识的时候,那么 OkHttp 会在 BridgeInterceptor 拦截器添加一个默认的 UA 标识,那么如何在 EasyHttp 设置自定义 UA 标识呢?其实很简单,UA 标识本质上其实就是一个请求头,在 EasyHttp 中添加一个请求头为 `User-Agent` 的参数即可,至于怎么添加请求头,前面的文档已经有介绍了,这里不再赘述。
14421447

14431448
#### 我想修改请求回调所在的线程该怎么办
14441449

@@ -1475,6 +1480,103 @@ EasyHttp.post(this)
14751480

14761481
* 需要注意的是:由于 Post 请求是将参数放置到 `RequestBody` 上面,而一个请求只能设置一个 `RequestBody`,如果你设置了自定义 `body(RequestBody body)`,那么框架将不会去将 `XxxApi` 类中的字段解析成参数。另外除了 Post 请求,Put 请求和 Patch 请求也可以使用这种方式进行设置,这里不再赘述。
14771482

1483+
#### 我想自定义请求头中的 ContentType 该怎么做
1484+
1485+
* 具体的写法示例如下:
1486+
1487+
```java
1488+
public final class SearchBlogsApi implements IRequestApi {
1489+
1490+
@NonNull
1491+
@Override
1492+
public String getApi() {
1493+
return "xxx/xxx";
1494+
}
1495+
1496+
@HttpHeader
1497+
@HttpRename("Content-Type")
1498+
private String contentType = "application/x-www-form-urlencoded;charset=utf-8";
1499+
}
1500+
```
1501+
1502+
* 需要注意的是:此功能仅是在框架 **11.5** 版本的时候加上的,之前的版本没有这一功能
1503+
1504+
#### 我想自定义 Get 请求参数中的 key 和 value 该怎么做
1505+
1506+
* 先自定义一个 Api 类,然后通过 `getApi` 方法将参数动态拼接上去
1507+
1508+
```java
1509+
public final class CustomParameterApi implements IRequestApi {
1510+
1511+
@HttpIgnore
1512+
@NonNull
1513+
private final Map<String, String> parameters;
1514+
1515+
public CustomParameterApi() {
1516+
this(new HashMap<>());
1517+
}
1518+
1519+
public CustomParameterApi(@NonNull Map<String, String> parameters) {
1520+
this.parameters = parameters;
1521+
}
1522+
1523+
@NonNull
1524+
@Override
1525+
public String getApi() {
1526+
Set<String> keys = parameters.keySet();
1527+
1528+
StringBuilder builder = new StringBuilder();
1529+
int index = 0;
1530+
for (String key : keys) {
1531+
String value = parameters.get(key);
1532+
1533+
if (index == 0) {
1534+
builder.append("?");
1535+
}
1536+
builder.append(key)
1537+
.append("=")
1538+
.append(value);
1539+
if (index < keys.size() - 1) {
1540+
builder.append("&");
1541+
}
1542+
index++;
1543+
}
1544+
1545+
return "xxx/xxx" + builder;
1546+
}
1547+
1548+
public CustomParameterApi putParameter(String key, String value) {
1549+
parameters.put(key, value);
1550+
return this;
1551+
}
1552+
1553+
public CustomParameterApi removeParameter(String key) {
1554+
parameters.remove(key);
1555+
return this;
1556+
}
1557+
}
1558+
```
1559+
1560+
* 外层可以通过以下方式进行调用
1561+
1562+
```java
1563+
CustomParameterApi api = new CustomParameterApi();
1564+
api.putParameter("key1", "value1");
1565+
api.putParameter("key2", "value2");
1566+
1567+
EasyHttp.get(this)
1568+
.api(api)
1569+
.request(new HttpCallback<Xxx>(this) {
1570+
1571+
@Override
1572+
public void onSucceed(Xxx result) {
1573+
1574+
}
1575+
});
1576+
```
1577+
1578+
* 需要注意的是:这种实现方式仅适用于在框架设计无法满足需求的情况下,其他情况下作者并不提倡用这种方式,因为这样不方便管理请求参数的 key,还是推荐大家使用在类上面定义字段的方式来实现。
1579+
14781580
# 搭配 RxJava
14791581

14801582
#### 准备工作

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# 简单易用的网络框架
22

3-
* 项目地址:[Github](https://github.com/getActivity/EasyHttp)[码云](https://gitee.com/getActivity/EasyHttp)
3+
* 项目地址:[Github](https://github.com/getActivity/EasyHttp)
44

55
* 博客地址:[网络请求,如斯优雅](https://www.jianshu.com/p/93cd59dec002)
66

7-
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/11.2/EasyHttp.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/11.5/EasyHttp.apk)
88

99
![](picture/demo_code.png)
1010

@@ -61,7 +61,7 @@ android {
6161
6262
dependencies {
6363
// 网络请求框架:https://github.com/getActivity/EasyHttp
64-
implementation 'com.github.getActivity:EasyHttp:11.2'
64+
implementation 'com.github.getActivity:EasyHttp:11.5'
6565
// OkHttp 框架:https://github.com/square/okhttp
6666
// noinspection GradleDependency
6767
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
@@ -76,7 +76,7 @@ dependencies {
7676

7777
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
7878
| :----: | :------: | :-----: | :-----: |
79-
| 对应版本 | 11.2 | 2.9.0 | 3.0.4 |
79+
| 对应版本 | 11.5 | 2.9.0 | 3.0.4 |
8080
| issues 数 | [![](https://img.shields.io/github/issues/getActivity/EasyHttp.svg)](https://github.com/getActivity/EasyHttp/issues) | [![](https://img.shields.io/github/issues/square/retrofit.svg)](https://github.com/square/retrofit/issues) | [![](https://img.shields.io/github/issues/jeasonlzy/okhttp-OkGo.svg)](https://github.com/jeasonlzy/okhttp-OkGo/issues) |
8181
| **aar 包大小** | 86 KB | 123 KB | 131 KB |
8282
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
@@ -241,6 +241,8 @@ EasyHttp.post(this)
241241

242242
* Android 代码规范:[AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard) ![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)
243243

244+
* Android 资源大汇总:[AndroidIndex](https://github.com/getActivity/AndroidIndex) ![](https://img.shields.io/github/stars/getActivity/AndroidIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidIndex.svg)
245+
244246
* Android 开源排行榜:[AndroidGithubBoss](https://github.com/getActivity/AndroidGithubBoss) ![](https://img.shields.io/github/stars/getActivity/AndroidGithubBoss.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidGithubBoss.svg)
245247

246248
* Studio 精品插件:[StudioPlugins](https://github.com/getActivity/StudioPlugins) ![](https://img.shields.io/github/stars/getActivity/StudioPlugins.svg) ![](https://img.shields.io/github/forks/getActivity/StudioPlugins.svg)

app/build.gradle

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'monitor-plugin'
45

56
android {
67
compileSdkVersion 31
@@ -11,10 +12,10 @@ android {
1112

1213
defaultConfig {
1314
applicationId 'com.hjq.easy.demo'
14-
minSdkVersion 16
15+
minSdkVersion 21
1516
targetSdkVersion 31
16-
versionCode 1120
17-
versionName '11.2'
17+
versionCode 1150
18+
versionName '11.5'
1819
}
1920

2021
// 支持 JDK 1.8
@@ -72,25 +73,28 @@ dependencies {
7273
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
7374

7475
// 吐司框架:https://github.com/getActivity/ToastUtils
75-
implementation 'com.github.getActivity:ToastUtils:10.5'
76+
implementation 'com.github.getActivity:ToastUtils:11.2'
7677

7778
// 权限请求框架:https://github.com/getActivity/XXPermissions
78-
implementation 'com.github.getActivity:XXPermissions:15.0'
79+
implementation 'com.github.getActivity:XXPermissions:16.5'
7980

8081
// 标题栏框架:https://github.com/getActivity/TitleBar
81-
implementation 'com.github.getActivity:TitleBar:9.5'
82+
implementation 'com.github.getActivity:TitleBar:9.6'
8283

8384
// Json 解析框架:https://github.com/google/gson
84-
implementation 'com.google.code.gson:gson:2.9.0'
85+
implementation 'com.google.code.gson:gson:2.9.1'
8586
// Gson 解析容错:https://github.com/getActivity/GsonFactory
86-
implementation 'com.github.getActivity:GsonFactory:6.2'
87+
implementation 'com.github.getActivity:GsonFactory:6.3'
8788

8889
// 腾讯 MMKV:https://github.com/Tencent/MMKV
89-
implementation 'com.tencent:mmkv-static:1.2.12'
90+
implementation 'com.tencent:mmkv-static:1.2.14'
9091

9192
// 日志调试框架:https://github.com/getActivity/Logcat
92-
debugImplementation 'com.github.getActivity:Logcat:10.6'
93+
debugImplementation 'com.github.getActivity:Logcat:11.0'
94+
95+
// OkHttp 抓包框架:https://github.com/lygttpod/AndroidMonitor
96+
debugImplementation 'io.github.lygttpod:monitor:0.0.7'
9397

9498
// 内存泄漏监测框架:https://github.com/square/leakcanary
95-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
99+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
96100
}

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
android:name="LogcatNotifyEntrance"
4343
android:value="true" />
4444

45+
<!-- 默认搜索关键字 -->
46+
<meta-data
47+
android:name="LogcatDefaultSearchKey"
48+
android:value="EasyHttp" />
49+
4550
<!-- 适配 Android 7.0 文件意图 -->
4651
<provider
4752
android:name="androidx.core.content.FileProvider"

app/src/main/java/com/hjq/easy/demo/AppApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public void onCreate() {
4646
EasyConfig.with(okHttpClient)
4747
// 是否打印日志
4848
//.setLogEnabled(BuildConfig.DEBUG)
49-
// 设置服务器配置
49+
// 设置服务器配置(必须设置)
5050
.setServer(server)
51-
// 设置请求处理策略
51+
// 设置请求处理策略(必须设置)
5252
.setHandler(new RequestHandler(this))
5353
// 设置请求参数拦截器
5454
.setInterceptor(new IRequestInterceptor() {

0 commit comments

Comments
 (0)