Skip to content

Commit

Permalink
优化框架代码逻辑
Browse files Browse the repository at this point in the history
优化框架方法和类命名
修复延迟请求无法被取消的问题
修复数组类型参数无法被正常解析的问题
修复在多线程情况下使用异步请求会出现索引越界的问题
  • Loading branch information
getActivity committed Apr 2, 2023
1 parent a3b83c4 commit 6420cfa
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 114 deletions.
4 changes: 3 additions & 1 deletion .github/ISSUE_TEMPLATE/issue_template_bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ assignees: getActivity

* 复现步骤【必填】:XXX(注意:目前不受理没有复现步骤的 Bug 单)

* 是否必现【必填】:填是/否
* 是否必现【必填】:是/否

* 项目 targetSdkVersion【必填】:XX

* 出现问题的手机信息【必填】:请填写出现问题的品牌和机型

Expand Down
46 changes: 23 additions & 23 deletions HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* [如何设置请求超时时间](#如何设置请求超时时间)

* [如何设置不打印日志](#如何设置不打印日志)

* [如何修改日志打印策略](#如何修改日志打印策略)

* [如何取消已发起的请求](#如何取消已发起的请求)
Expand Down Expand Up @@ -443,7 +443,7 @@ EasyHttp.post(this)

@Override
public void onSucceed(Xxx<Xxx> data) {

}
});
```
Expand Down Expand Up @@ -481,7 +481,7 @@ public final class RequestHandler implements IRequestHandler {
mApplication = application;
mMmkv = MMKV.mmkvWithID("http_cache_id");
}

..................

@Nullable
Expand Down Expand Up @@ -717,7 +717,7 @@ public final class XxxApi implements IRequestApi {
public String getApi() {
return "xxx/xxxx";
}

@HttpIgnore
private String token;
}
Expand Down Expand Up @@ -980,7 +980,7 @@ public final class XxxApi implements IRequestApi {
public String getApi() {
return "xxx/xxxx";
}

@HttpIgnore
private String address;
}
Expand All @@ -996,7 +996,7 @@ public final class XxxApi implements IRequestApi {
public String getApi() {
return "xxx/xxxx";
}

@HttpHeader
private String time;
}
Expand All @@ -1012,7 +1012,7 @@ public final class XxxApi implements IRequestApi {
public String getApi() {
return "xxx/xxxx";
}

@HttpRename("k")
private String keyword;
}
Expand All @@ -1024,13 +1024,13 @@ public final class XxxApi implements IRequestApi {

```java
public final class XxxApi implements IRequestApi {

@NonNull
@Override
public String getApi() {
return "xxx/xxxx";
}

private File file;
}
```
Expand All @@ -1039,13 +1039,13 @@ public final class XxxApi implements IRequestApi {

```java
public final class XxxApi implements IRequestApi {

@NonNull
@Override
public String getApi() {
return "xxx/xxxx";
}

private InputStream inputStream;
}
```
Expand All @@ -1060,7 +1060,7 @@ public final class XxxApi implements IRequestApi {
public String getApi() {
return "xxx/xxxx";
}

private RequestBody requestBody;
}
```
Expand All @@ -1075,7 +1075,7 @@ public final class XxxApi implements IRequestApi {
public String getApi() {
return "xxx/xxxx";
}

private List<File> files;
}
```
Expand Down Expand Up @@ -1147,7 +1147,7 @@ EasyConfig.with(okHttpClient)
* 需要修改日志打印策略的场景

* 需要将请求的日志写入到本地

* 需要修改打印的请求日志格式

#### 如何取消已发起的请求
Expand All @@ -1172,7 +1172,7 @@ EasyHttp.post(MainActivity.this)

@Override
public void onSucceed(HttpData<XxxBean> result) {

}
});
```
Expand Down Expand Up @@ -1207,7 +1207,7 @@ EasyHttp.post(this)

@Override
public void onSucceed(Xxx result) {

}
});
```
Expand Down Expand Up @@ -1394,7 +1394,7 @@ EasyHttp.post(this)

@Override
public void onSucceed(Xxx result) {

}

@Override
Expand Down Expand Up @@ -1493,7 +1493,7 @@ EasyHttp.post(this)

@Override
public void onSucceed(HttpData<Xxx> result) {

}
});
```
Expand Down Expand Up @@ -1590,7 +1590,7 @@ EasyHttp.get(this)

@Override
public void onSucceed(Xxx result) {

}
});
```
Expand Down Expand Up @@ -1620,7 +1620,7 @@ Observable.create(new ObservableOnSubscribe<HttpData<SearchBean>>() {

@Override
public void subscribe(ObservableEmitter<HttpData<SearchBean>> emitter) throws Exception {

HttpData<SearchBean> data1;
try {
data1 = EasyHttp.post(MainActivity.this)
Expand Down Expand Up @@ -1657,7 +1657,7 @@ Observable.create(new ObservableOnSubscribe<HttpData<SearchBean>>() {
}

}, new Consumer<Throwable>() {

@Override
public void accept(Throwable throwable) throws Exception {
toast(throwable.getMessage());
Expand Down Expand Up @@ -1738,9 +1738,9 @@ Observable.create(new ObservableOnSubscribe<HttpData<SearchBean>>() {
public void accept(String s) throws Exception {
Log.i("EasyHttp", ""当前页码位置" + s);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
toast(throwable.getMessage());
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

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

![](picture/demo_code.png)

Expand Down Expand Up @@ -61,24 +61,24 @@ android {
dependencies {
// 网络请求框架:https://github.com/getActivity/EasyHttp
implementation 'com.github.getActivity:EasyHttp:11.6'
implementation 'com.github.getActivity:EasyHttp:11.8'
// OkHttp 框架:https://github.com/square/okhttp
// noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
}
```

* 需要注意的是:由于使用了 Lifecycle 特性,目前框架只支持 AndroidX 的项目集成

## [框架的具体用法请点击这里查看](HelpDoc.md)

### 不同网络请求框架之间的对比

| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
| :----: | :------: | :-----: | :-----: |
| 对应版本 | 11.6 | 2.9.0 | 3.0.4 |
| 对应版本 | 11.8 | 2.9.0 | 3.0.4 |
| 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) |
| **aar 包大小** | 89 KB | 123 KB | 131 KB |
| **aar 包大小** | 90 KB | 123 KB | 131 KB |
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
| 配置多域名 ||||
| **动态 Host** ||||
Expand Down Expand Up @@ -109,9 +109,9 @@ dependencies {
* OkGo 其实也存在一些弊端,例如会把参数的 key 引用放到外层去,这样会引发一些问题:

1. Key 管理问题:这个 key 可能会在外层被使用很多次,这样参数的 key 管理就会变得不可控,后续接口改动可能会出现漏改的风险,尽管这种情况比较少见,但是也不容忽视,而 EasyHttp 没有这个问题,因为 EasyHttp 不会将参数 key 值放置到外层中去。

2. 接口参数注释的问题:站在代码的规范角度上讲,我们应该在代码中注明参数的含义及作用,如果一旦将 key 放到外层,那么每一处调用的地方都需要写一遍注释,而 EasyHttp 是将参数字段化,只需要写一次注释到字段上即可。

3. 接口信息完整信息展示:使用 OkGo 请求网络,只能在调用的地方看到传递的接口参数,而一些被其他地方引用的参数,我们无法很直观的看到,只能通过追踪代码或者查看文档来得知,而 EasyHttp 将一个接口的信息全部通过一个类来管理的,这个类其实就相当于一个接口文档。

4. 接口的动态化配置:除了接口的参数之外,一个接口还有可能单独配置 OkHttpClient 对象、参数的提交方式、接口响应处理方式等,这些用 OkGo 是可以实现,但是每个地方都要写一次,而 EasyHttp 可以直接在 API 类中配置,真正做到一劳永逸。
Expand All @@ -127,7 +127,7 @@ public final class XxxApi implements IRequestApi {
public String getApi() {
return "xxx/xxx";
}

private int xxx;

public XxxApi setXxx(int xxx) {
Expand Down Expand Up @@ -166,7 +166,7 @@ public final class HttpLifecycleManager implements LifecycleEventObserver {
/**
* 绑定组件的生命周期
*/
public static void bind(LifecycleOwner lifecycleOwner) {
public static void register(LifecycleOwner lifecycleOwner) {
lifecycleOwner.getLifecycle().addObserver(new HttpLifecycleManager());
}

Expand Down Expand Up @@ -208,7 +208,7 @@ EasyHttp.post(this)

@Override
public void onSucceed(HttpData<XxxBean> result) {

}
});
```
Expand Down Expand Up @@ -249,6 +249,8 @@ EasyHttp.post(this)

* 表情包大集合:[EmojiPackage](https://github.com/getActivity/EmojiPackage) ![](https://img.shields.io/github/stars/getActivity/EmojiPackage.svg) ![](https://img.shields.io/github/forks/getActivity/EmojiPackage.svg)

* AI 资源大汇总:[AiIndex](https://github.com/getActivity/AiIndex) ![](https://img.shields.io/github/stars/getActivity/AiIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AiIndex.svg)

* 省市区 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)

* Markdown 语法文档:[MarkdownDoc](https://github.com/getActivity/MarkdownDoc) ![](https://img.shields.io/github/stars/getActivity/MarkdownDoc.svg) ![](https://img.shields.io/github/forks/getActivity/MarkdownDoc.svg)
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId 'com.hjq.easy.demo'
minSdkVersion 21
targetSdkVersion 31
versionCode 1160
versionName '11.6'
versionCode 1180
versionName '11.8'
}

// 支持 JDK 1.8
Expand Down Expand Up @@ -76,10 +76,10 @@ dependencies {
implementation 'com.github.getActivity:Toaster:12.0'

// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:16.6'
implementation 'com.github.getActivity:XXPermissions:16.8'

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:10.0'
implementation 'com.github.getActivity:TitleBar:10.2'

// Json 解析框架:https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.10.1'
Expand Down
7 changes: 1 addition & 6 deletions app/src/main/java/com/hjq/easy/demo/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import androidx.appcompat.app.AppCompatActivity;

import com.hjq.easy.demo.http.model.HttpData;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.Toaster;

Expand Down Expand Up @@ -66,11 +65,7 @@ public void onStart(Call call) {
}

@Override
public void onSucceed(Object result) {
if (result instanceof HttpData) {
Toaster.show(((HttpData<?>) result).getMessage());
}
}
public void onSucceed(Object result) {}

@Override
public void onFail(Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/hjq/easy/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.hjq.easy.demo.http.model.HttpData;
import com.hjq.http.EasyHttp;
import com.hjq.http.EasyUtils;
import com.hjq.http.exception.FileMD5Exception;
import com.hjq.http.exception.FileMd5Exception;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.listener.OnUpdateListener;
Expand Down Expand Up @@ -288,7 +288,7 @@ public void onComplete(File file) {
@Override
public void onError(File file, Exception e) {
Toaster.show(e.getMessage());
if (e instanceof FileMD5Exception) {
if (e instanceof FileMd5Exception) {
// 如果是文件 md5 校验失败,则删除文件
file.delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.hjq.http.config.IRequestHandler;
import com.hjq.http.exception.CancelException;
import com.hjq.http.exception.DataException;
import com.hjq.http.exception.FileMD5Exception;
import com.hjq.http.exception.FileMd5Exception;
import com.hjq.http.exception.HttpException;
import com.hjq.http.exception.NetworkException;
import com.hjq.http.exception.NullBodyException;
Expand Down Expand Up @@ -188,8 +188,8 @@ public Exception downloadFail(@NonNull HttpRequest<?> httpRequest, @NonNull Exce
NullBodyException nullBodyException = ((NullBodyException) e);
nullBodyException.setMessage(mApplication.getString(R.string.http_response_null_body));
return nullBodyException;
} else if (e instanceof FileMD5Exception) {
FileMD5Exception fileMd5Exception = ((FileMD5Exception) e);
} else if (e instanceof FileMd5Exception) {
FileMd5Exception fileMd5Exception = ((FileMd5Exception) e);
fileMd5Exception.setMessage(mApplication.getString(R.string.http_response_md5_error));
return fileMd5Exception;
}
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 1160
versionName "11.6"
versionCode 1180
versionName "11.8"
}

// 使用 JDK 1.8
Expand Down
Loading

0 comments on commit 6420cfa

Please sign in to comment.