Skip to content

Commit aa3b0f9

Browse files
committed
优化框架的代码写法
优化框架的使用的文档 移动 ResultException 类 移动 TokenException 类 新增 LifecycleAppFragment 类 修改 LifecycleService 类的回调时机 修正 Get 请求不能存在多个同名参数的问题 修复 USE_CACHE_AFTER_FAILURE 没有重新写入缓存的 Bug
1 parent ba1231e commit aa3b0f9

26 files changed

+635
-309
lines changed

.github/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: https://raw.githubusercontent.com/getActivity/Donate/master/picture/pay_ali.png
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
name: 提交 Bug
3+
about: 请告诉我框架存在的问题,我会协助你解决此问题!
4+
labels: bug
5+
assignees: getActivity
6+
7+
---
8+
9+
## 问题描述
10+
11+
* 框架版本:XXX
12+
13+
* 问题描述:XXX
14+
15+
* 复现步骤:XXX
16+
17+
* 是否必现:填是/否
18+
19+
* 手机信息:例如某米 9 / Android 10
20+
21+
## 请回答
22+
23+
* 是部分机型还是所有机型都会出现:部分/全部(例如:某为,某 Android 版本会出现)
24+
25+
* 框架最新的版本是否存在这个问题:是/否(如果用的是旧版本的话,建议升级看问题是否还存在)
26+
27+
* 是否已经查阅框架文档还未能解决的:是/否(文档会提供最常见的问题解答,可以看看是否有自己想要的)
28+
29+
* issue 是否有人曾提过类似的问题:是/否(看看曾经有人提过类似的问题,先参考一下别人是怎么解决的)
30+
31+
* 是否可以通过 Demo 来复现该问题:是/否(排查一下是不是自己的项目代码写得有问题导致的)
32+
33+
* 使用原生 API 是否会出现该问题:是/否(排查一下是不是框架的代码写法存在问题导致的)
34+
35+
## 其他
36+
37+
* 提供报错堆栈(如果有报错的话必填)
38+
39+
* 提供截图或视频(根据需要提供,此项不强制)
40+
41+
* 提供解决方案(如果已经解决了的话,此项不强制)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
name: 提交建议
3+
about: 请告诉我框架的不足之处,让我做得更好!
4+
labels: bug
5+
assignees: getActivity
6+
7+
---
8+
9+
## 建议收集
10+
11+
* 你觉得框架有什么不足之处?(必答项,你可以描述框架有什么令你不满意的地方)
12+
13+
* 你觉得该怎么去完善会比较好?(非必答项,你可以提供一下自己的想法或者做法供作者参考)

.github/workflows/android.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Android CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: set up JDK 1.8
13+
uses: actions/setup-java@v1
14+
with:
15+
java-version: 1.8
16+
- name: Build with Gradle
17+
run: ./gradlew build aR

HelpDoc.md

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ EasyHttp.post(this)
354354

355355
* **需要注意的是:如果上传的文件过多或者过大,可能会导致请求超时,可以重新设置本次请求超时时间,超时时间建议根据文件大小而定,具体设置超时方式文档有介绍,可以在本页面直接搜索。**
356356

357-
* 当然除了可以使用 **File** 类型的对象进行上传,还可以使用 **FileContentResolver** / **InputStream** / **RequestBody** 类型的对象进行上传,如果你需要批量上传,请使用 `List<File>` 或者 `List<MultipartBody.Part>` 类型的对象来做批量上传。
357+
* 当然除了可以使用 `File` 类型的对象进行上传,还可以使用 `FileContentResolver``InputStream``RequestBody``MultipartBody.Part` 类型的对象进行上传,如果你需要批量上传,请使用 `List<File>``List<FileContentResolver>``List<InputStream>``List<RequestBody>``List<MultipartBody.Part>` 类型的对象来做批量上传。
358358

359359
#### 下载文件
360360

@@ -400,6 +400,8 @@ EasyHttp.download(this)
400400

401401
#### 同步请求
402402

403+
* 需要注意的是:同步请求是耗时操作,不能在主线程中执行,请务必保证此操作在子线程中执行
404+
403405
```java
404406
PostRequest postRequest = EasyHttp.post(MainActivity.this);
405407
try {
@@ -433,7 +435,7 @@ public final class RequestHandler implements IRequestHandler {
433435

434436
@Override
435437
public Object readCache(HttpRequest<?> httpRequest, Type type, long cacheTime) {
436-
String cacheKey = GsonFactory.getSingletonGson().toJson(api);
438+
String cacheKey = GsonFactory.getSingletonGson().toJson(httpRequest.getRequestApi());
437439
String cacheValue = mMmkv.getString(cacheKey, null);
438440
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
439441
return null;
@@ -447,7 +449,7 @@ public final class RequestHandler implements IRequestHandler {
447449

448450
@Override
449451
public boolean writeCache(HttpRequest<?> httpRequest, Response response, Object result) {
450-
String cacheKey = GsonFactory.getSingletonGson().toJson(api);
452+
String cacheKey = GsonFactory.getSingletonGson().toJson(httpRequest.getRequestApi());
451453
String cacheValue = GsonFactory.getSingletonGson().toJson(result);
452454
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
453455
return false;
@@ -479,16 +481,16 @@ public enum CacheMode {
479481
/**
480482
* 只使用缓存
481483
*
482-
* 有缓存的情况下:读取缓存 -> 回调成功
483-
* 无缓存的情况下:请求网络 -> 写入缓存 -> 回调成功
484+
* 已有缓存的情况下:读取缓存 -> 回调成功
485+
* 没有缓存的情况下:请求网络 -> 写入缓存 -> 回调成功
484486
*/
485487
USE_CACHE_ONLY,
486488

487489
/**
488490
* 优先使用缓存
489491
*
490-
* 有缓存的情况下:先读缓存 —> 回调成功 —> 请求网络 —> 刷新缓存
491-
* 无缓存的情况下:请求网络 -> 写入缓存 -> 回调成功
492+
* 已有缓存的情况下:先读缓存 —> 回调成功 —> 请求网络 —> 刷新缓存
493+
* 没有缓存的情况下:请求网络 -> 写入缓存 -> 回调成功
492494
*/
493495
USE_CACHE_FIRST,
494496

@@ -1114,9 +1116,9 @@ EasyHttp.post(this)
11141116

11151117
#### 框架只能传入 LifecycleOwner 该怎么办
11161118

1117-
* 其中 AndroidX.AppCompatActivity 和 AndroidX.Fragment 都是 LifecycleOwner 子类的,这个是毋庸置疑的
1119+
* 其中 `androidx.appcompat.app.AppCompatActivity``androidx.fragment.app.Fragment` 都是 LifecycleOwner 子类的,这个是毋庸置疑的,可以直接当做 LifecycleOwner 传给框架
11181120

1119-
* 但是你如果传入的是 Activity 对象,并非 AppCompatActivity 对象,那么你可以这样写
1121+
* 但是你如果传入的是 `android.app.Activity` 对象,并非 `androidx.appcompat.app.AppCompatActivity` 对象,那么你可以这样写
11201122

11211123
```java
11221124
EasyHttp.post(new ActivityLifecycle(this))
@@ -1130,32 +1132,9 @@ EasyHttp.post(new ActivityLifecycle(this))
11301132
});
11311133
```
11321134

1133-
* 你如果想在 Service 中使用 EasyHttp,请将 Service 直接继承框架中的 LifecycleService 类,又或者在项目中封装一个带有 Lifecycle 特性的 Service 基类,具体实现如下:
1134-
1135-
```java
1136-
public abstract class LifecycleService extends Service implements LifecycleOwner {
1137-
1138-
private final LifecycleRegistry mLifecycle = new LifecycleRegistry(this);
1139-
1140-
@NonNull
1141-
@Override
1142-
public Lifecycle getLifecycle() {
1143-
return mLifecycle;
1144-
}
1145-
1146-
@Override
1147-
public void onCreate() {
1148-
super.onCreate();
1149-
mLifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
1150-
}
1135+
* 如果你传入的是 `android.app.Fragment` 对象,并非 `androidx.fragment.app.Fragment` 对象,请将 Fragment 直接继承框架中的 LifecycleAppFragment 类,又或者在项目中封装一个带有 Lifecycle 特性的 Fragment 基类
11511136

1152-
@Override
1153-
public void onDestroy() {
1154-
super.onDestroy();
1155-
mLifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
1156-
}
1157-
}
1158-
```
1137+
* 你如果想在 `android.app.Service` 中使用 EasyHttp,请将 Service 直接继承框架中的 LifecycleService 类,又或者在项目中封装一个带有 Lifecycle 特性的 Service 基类
11591138

11601139
* 如果以上条件都不满足,但是你就是想在某个地方请求网络,那么你可以这样写
11611140

README.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

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

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

@@ -26,22 +26,29 @@
2626

2727
#### 集成步骤
2828

29-
* 在项目根目录下的 `build.gradle` 文件中加入
29+
* 如果你的项目 Gradle 配置是在 `7.0 以下`,需要在 `build.gradle` 文件中加入
3030

3131
```groovy
32-
buildscript {
32+
allprojects {
3333
repositories {
34+
// JitPack 远程仓库:https://jitpack.io
3435
maven { url 'https://jitpack.io' }
3536
}
3637
}
37-
allprojects {
38+
```
39+
40+
* 如果你的 Gradle 配置是 `7.0 及以上`,则需要在 `settings.gradle` 文件中加入
41+
42+
```groovy
43+
dependencyResolutionManagement {
3844
repositories {
45+
// JitPack 远程仓库:https://jitpack.io
3946
maven { url 'https://jitpack.io' }
4047
}
4148
}
4249
```
4350

44-
* 在项目 app 模块下的 `build.gradle` 文件中加入
51+
* 配置完远程仓库后,在项目 app 模块下的 `build.gradle` 文件中加入远程依赖
4552

4653
```groovy
4754
android {
@@ -54,7 +61,7 @@ android {
5461
5562
dependencies {
5663
// 网络请求框架:https://github.com/getActivity/EasyHttp
57-
implementation 'com.github.getActivity:EasyHttp:10.6'
64+
implementation 'com.github.getActivity:EasyHttp:10.8'
5865
// OkHttp 框架:https://github.com/square/okhttp
5966
// noinspection GradleDependency
6067
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
@@ -67,9 +74,9 @@ dependencies {
6774

6875
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
6976
| :----: | :------: | :-----: | :-----: |
70-
| 对应版本 | 10.6 | 2.9.0 | 3.0.4 |
77+
| 对应版本 | 10.8 | 2.9.0 | 3.0.4 |
7178
| 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) |
72-
| **aar 包大小** | 78 KB | 123 KB | 131 KB |
79+
| **aar 包大小** | 80 KB | 123 KB | 131 KB |
7380
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
7481
| 配置多域名 ||||
7582
| **动态 Host** ||||
@@ -208,23 +215,35 @@ EasyHttp.post(this)
208215

209216
#### 作者的其他开源项目
210217

211-
* 安卓技术中台:[AndroidProject](https://github.com/getActivity/AndroidProject)
218+
* 安卓技术中台:[AndroidProject](https://github.com/getActivity/AndroidProject) ![](https://img.shields.io/github/stars/getActivity/AndroidProject.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidProject.svg)
219+
220+
* 安卓技术中台 Kt 版:[AndroidProject-Kotlin](https://github.com/getActivity/AndroidProject-Kotlin) ![](https://img.shields.io/github/stars/getActivity/AndroidProject-Kotlin.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidProject-Kotlin.svg)
221+
222+
* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions) ![](https://img.shields.io/github/stars/getActivity/XXPermissions.svg) ![](https://img.shields.io/github/forks/getActivity/XXPermissions.svg)
223+
224+
* 吐司框架:[ToastUtils](https://github.com/getActivity/ToastUtils) ![](https://img.shields.io/github/stars/getActivity/ToastUtils.svg) ![](https://img.shields.io/github/forks/getActivity/ToastUtils.svg)
225+
226+
* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar) ![](https://img.shields.io/github/stars/getActivity/TitleBar.svg) ![](https://img.shields.io/github/forks/getActivity/TitleBar.svg)
227+
228+
* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast) ![](https://img.shields.io/github/stars/getActivity/XToast.svg) ![](https://img.shields.io/github/forks/getActivity/XToast.svg)
229+
230+
* Shape 框架:[ShapeView](https://github.com/getActivity/ShapeView) ![](https://img.shields.io/github/stars/getActivity/ShapeView.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)
212231

213-
* 权限框架:[XXPermissions](https://github.com/getActivity/XXPermissions)
232+
* 语种切换框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages) ![](https://img.shields.io/github/stars/getActivity/MultiLanguages.svg) ![](https://img.shields.io/github/forks/getActivity/MultiLanguages.svg)
214233

215-
* 吐司框架:[ToastUtils](https://github.com/getActivity/ToastUtils)
234+
* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory) ![](https://img.shields.io/github/stars/getActivity/GsonFactory.svg) ![](https://img.shields.io/github/forks/getActivity/GsonFactory.svg)
216235

217-
* 标题栏框架:[TitleBar](https://github.com/getActivity/TitleBar)
236+
* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat) ![](https://img.shields.io/github/stars/getActivity/Logcat.svg) ![](https://img.shields.io/github/forks/getActivity/Logcat.svg)
218237

219-
* 国际化框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages)
238+
* Android 版本适配:[AndroidVersionAdapter](https://github.com/getActivity/AndroidVersionAdapter) ![](https://img.shields.io/github/stars/getActivity/AndroidVersionAdapter.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidVersionAdapter.svg)
220239

221-
* 悬浮窗框架:[XToast](https://github.com/getActivity/XToast)
240+
* 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)
222241

223-
* Shape 框架:[ShapeView](https://github.com/getActivity/ShapeView)
242+
* 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)
224243

225-
* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory)
244+
* 表情包大集合:[EmojiPackage](https://github.com/getActivity/EmojiPackage) ![](https://img.shields.io/github/stars/getActivity/EmojiPackage.svg) ![](https://img.shields.io/github/forks/getActivity/EmojiPackage.svg)
226245

227-
* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat)
246+
* 省市区 Json 数据:[ProvinceJson](https://github.com/getActivity/ProvinceJson) ![](https://img.shields.io/github/stars/getActivity/ProvinceJson.svg) ![](https://img.shields.io/github/forks/getActivity/ProvinceJson.svg)
228247

229248
#### 微信公众号:Android轮子哥
230249

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
applicationId 'com.hjq.easy.demo'
1414
minSdkVersion 16
1515
targetSdkVersion 31
16-
versionCode 1060
17-
versionName '10.6'
16+
versionCode 1080
17+
versionName '10.8'
1818
}
1919

2020
// 支持 JDK 1.8
@@ -71,21 +71,21 @@ dependencies {
7171
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
7272

7373
// 吐司框架:https://github.com/getActivity/ToastUtils
74-
implementation 'com.github.getActivity:ToastUtils:9.6'
74+
implementation 'com.github.getActivity:ToastUtils:10.0'
7575

7676
// 权限请求框架:https://github.com/getActivity/XXPermissions
77-
implementation 'com.github.getActivity:XXPermissions:12.8'
77+
implementation 'com.github.getActivity:XXPermissions:13.2'
7878

7979
// 标题栏框架:https://github.com/getActivity/TitleBar
80-
implementation 'com.github.getActivity:TitleBar:9.2'
80+
implementation 'com.github.getActivity:TitleBar:9.3'
8181

8282
// Json 解析框架:https://github.com/google/gson
8383
implementation 'com.google.code.gson:gson:2.8.9'
8484
// Gson 解析容错:https://github.com/getActivity/GsonFactory
8585
implementation 'com.github.getActivity:GsonFactory:5.2'
8686

8787
// 日志调试框架:https://github.com/getActivity/Logcat
88-
debugImplementation 'com.github.getActivity:Logcat:9.9'
88+
debugImplementation 'com.github.getActivity:Logcat:10.0'
8989

9090
// 腾讯 MMKV:https://github.com/Tencent/MMKV
9191
implementation 'com.tencent:mmkv-static:1.2.11'

app/src/main/AndroidManifest.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
android:name="ScopedStorage"
3333
android:value="true" />
3434

35+
<!-- 悬浮窗入口 -->
36+
<meta-data
37+
android:name="LogcatWindowEntrance"
38+
android:value="true" />
39+
40+
<!-- 通知栏入口 -->
41+
<meta-data
42+
android:name="LogcatNotifyEntrance"
43+
android:value="true" />
44+
3545
<!-- 适配 Android 7.0 文件意图 -->
3646
<provider
3747
android:name="androidx.core.content.FileProvider"
@@ -59,4 +69,14 @@
5969

6070
</application>
6171

72+
<!-- Android 11 软件包可见性适配 -->
73+
<queries>
74+
75+
<!-- 调起其他页面意图:Intent.ACTION_VIEW -->
76+
<intent>
77+
<action android:name="android.intent.action.VIEW" />
78+
</intent>
79+
80+
</queries>
81+
6282
</manifest>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import androidx.core.content.ContextCompat;
1717
import androidx.core.content.FileProvider;
1818

19+
import com.hjq.bar.OnTitleBarListener;
20+
import com.hjq.bar.TitleBar;
1921
import com.hjq.easy.demo.http.api.SearchAuthorApi;
2022
import com.hjq.easy.demo.http.api.SearchBlogsApi;
2123
import com.hjq.easy.demo.http.api.UpdateImageApi;
@@ -64,6 +66,17 @@ protected void onCreate(Bundle savedInstanceState) {
6466
findViewById(R.id.btn_main_exec).setOnClickListener(this);
6567
findViewById(R.id.btn_main_update).setOnClickListener(this);
6668
findViewById(R.id.btn_main_download).setOnClickListener(this);
69+
70+
TitleBar titleBar = findViewById(R.id.tb_main_bar);
71+
titleBar.setOnTitleBarListener(new OnTitleBarListener() {
72+
@Override
73+
public void onTitleClick(TitleBar titleBar) {
74+
Intent intent = new Intent(Intent.ACTION_VIEW);
75+
intent.setData(Uri.parse(titleBar.getTitle().toString()));
76+
startActivity(intent);
77+
}
78+
});
79+
6780
requestPermission();
6881
}
6982

0 commit comments

Comments
 (0)