Skip to content

Commit

Permalink
优化网络请求日志的 TAG 名称
Browse files Browse the repository at this point in the history
优化反射性能(利用 LruCache 缓存)
新增支持设置回调监听所在的线程类型
新增 IRequestHandler 下载失败处理逻辑
新增 Delete 使用 Body 传参的请求方法
修复请求 Bean 类为流类型时流被关闭的问题
修正上传文件时带中文的文件名被编码的问题
  • Loading branch information
getActivity committed Jun 26, 2022
1 parent 4a0c7a9 commit b2b10da
Show file tree
Hide file tree
Showing 25 changed files with 468 additions and 203 deletions.
24 changes: 12 additions & 12 deletions .github/ISSUE_TEMPLATE/issue_template_bug.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ assignees: getActivity

## 问题描述

* 框架版本:XXX
* 框架版本【必填】:XXX

* 问题描述:XXX
* 问题描述【必填】:XXX

* 复现步骤:XXX
* 复现步骤【必填】:XXX

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

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

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

## 请回答

* 是部分机型还是所有机型都会出现:部分/全部(例如:某为,某 Android 版本会出现)
* 是部分机型还是所有机型都会出现【必答】:部分/全部(例如:某为,某 Android 版本会出现)

* 框架最新的版本是否存在这个问题:是/否(如果用的是旧版本的话,建议升级看问题是否还存在)
* 框架最新的版本是否存在这个问题【必答】:是/否(如果用的是旧版本的话,建议升级看问题是否还存在)

* 是否已经查阅框架文档还未能解决的:是/否(文档会提供最常见的问题解答,可以看看是否有自己想要的)
* 是否已经查阅框架文档还未能解决的【必答】:是/否(文档会提供最常见的问题解答,可以看看是否有自己想要的)

* issue 是否有人曾提过类似的问题:是/否(看看曾经有人提过类似的问题,先参考一下别人是怎么解决的)
* issue 是否有人曾提过类似的问题【必答】:是/否(看看曾经有人提过类似的问题,先参考一下别人是怎么解决的)

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

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

## 其他

Expand Down
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/issue_template_suggest.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ assignees: getActivity

## 建议收集

* issue 是否有人曾提过类似的问题?(必答项,一旦出现重复提问我将不会再次解答)
* issue 是否有人曾提过类似的问题?【必答】(一旦出现重复提问我将不会再次解答)

* 你觉得框架有什么不足之处?(必答项,你可以描述框架有什么令你不满意的地方)
* 你觉得框架有什么不足之处?【必答】(你可以描述框架有什么令你不满意的地方)

* 你觉得该怎么去完善会比较好?(非必答项,你可以提供一下自己的想法或者做法供作者参考)
* 你觉得该怎么去完善会比较好?【非必答】(你可以提供一下自己的想法或者做法供作者参考)
38 changes: 36 additions & 2 deletions HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@

* [如何对接口路径进行动态化拼接](#如何对接口路径进行动态化拼接)

* [如何动态化整个请求的 url](#如何动态化整个请求的-url)

* [Https 如何配置信任所有证书](#https-如何配置信任所有证书)

* [我不想一个接口写一个类怎么办](#我不想一个接口写一个类怎么办)
Expand All @@ -88,6 +90,8 @@

* [如何设置自定义的 UA 标识](#如何设置自定义的-ua-标识)

* [我想修改请求回调所在的线程该怎么办](#我想修改请求回调所在的线程该怎么办)

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

* [搭配 RxJava](#搭配-rxjava)
Expand Down Expand Up @@ -281,7 +285,7 @@ public final class XxxApi implements IRequestServer, IRequestApi {

#### 发起请求

* 需要配置请求状态及生命周期处理,具体封装可以参考 [BaseActivity](app/src/main/java/com/hjq/http/demo/BaseActivity.java)
* 需要配置请求状态及生命周期处理,具体封装可以参考 [BaseActivity](app/src/main/java/com/hjq/easy/demo/BaseActivity.java)

```java
EasyHttp.post(this)
Expand Down Expand Up @@ -1170,6 +1174,20 @@ public final class XxxApi implements IRequestApi {
}
```

#### 如何动态化整个请求的 url

```java
EasyHttp.post(this)
.api(new RequestUrl("https://xxxx.com/aaaa"))
.request(new HttpCallback<Xxx>(this) {

@Override
public void onSucceed(Xxx result) {

}
});
```

#### Https 如何配置信任所有证书

* 在初始化 OkHttp 的时候这样设置
Expand Down Expand Up @@ -1240,7 +1258,7 @@ EasyHttp.post(new ActivityLifecycle(this))
* 如果以上条件都不满足,但是你就是想在某个地方请求网络,那么你可以这样写

```java
EasyHttp.post(new ApplicationLifecycle())
EasyHttp.post(ApplicationLifecycle.getInstance())
.api(new XxxApi())
.tag("abc")
.request(new OnHttpListener<HttpData<XxxBean>>() {
Expand Down Expand Up @@ -1422,6 +1440,22 @@ EasyHttp.post(this)

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

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

```
EasyHttp.post(this)
.api(new XxxApi())
// 表示回调是在子线程中进行
.schedulers(ThreadSchedulers.IOThread)
.request(new HttpCallback<HttpData<Xxx>>(this) {
@Override
public void onSucceed(HttpData<Xxx> result) {
}
});
```

#### 我想自定义一个 RequestBody 进行请求该怎么办

* 在一些极端的情况下,框架无法满足使用的前提下,这个时候需要自定义 `RequestBody` 来实现,那么怎么使用自定义 `RequestBody` 呢?框架其实有开放方法,具体使用示例如下:
Expand Down
8 changes: 4 additions & 4 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.0/EasyHttp.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/11.2/EasyHttp.apk)

![](picture/demo_code.png)

Expand Down Expand Up @@ -61,7 +61,7 @@ android {
dependencies {
// 网络请求框架:https://github.com/getActivity/EasyHttp
implementation 'com.github.getActivity:EasyHttp:11.0'
implementation 'com.github.getActivity:EasyHttp:11.2'
// OkHttp 框架:https://github.com/square/okhttp
// noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
Expand All @@ -76,9 +76,9 @@ dependencies {

| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
| :----: | :------: | :-----: | :-----: |
| 对应版本 | 11.0 | 2.9.0 | 3.0.4 |
| 对应版本 | 11.2 | 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 包大小** | 80 KB | 123 KB | 131 KB |
| **aar 包大小** | 86 KB | 123 KB | 131 KB |
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
| 配置多域名 ||||
| **动态 Host** ||||
Expand Down
16 changes: 9 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId 'com.hjq.easy.demo'
minSdkVersion 16
targetSdkVersion 31
versionCode 1100
versionName '11.0'
versionCode 1120
versionName '11.2'
}

// 支持 JDK 1.8
Expand Down Expand Up @@ -63,31 +63,33 @@ dependencies {

// AndroidX 库:https://github.com/androidx/androidx
implementation 'androidx.appcompat:appcompat:1.4.0'
// Material 库:https://github.com/material-components/material-components-android
implementation 'com.google.android.material:material:1.4.0'

// OkHttp 框架:https://github.com/square/okhttp
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
// noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.13'

// 吐司框架:https://github.com/getActivity/ToastUtils
implementation 'com.github.getActivity:ToastUtils:10.3'
implementation 'com.github.getActivity:ToastUtils:10.5'

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

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:9.3'
implementation 'com.github.getActivity:TitleBar:9.5'

// Json 解析框架:https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.9.0'
// Gson 解析容错:https://github.com/getActivity/GsonFactory
implementation 'com.github.getActivity:GsonFactory:6.0'
implementation 'com.github.getActivity:GsonFactory:6.2'

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

// 日志调试框架:https://github.com/getActivity/Logcat
debugImplementation 'com.github.getActivity:Logcat:10.3'
debugImplementation 'com.github.getActivity:Logcat:10.6'

// 内存泄漏监测框架:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.8.1'
Expand Down
9 changes: 7 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 @@ -24,6 +24,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.listener.HttpCallback;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.listener.OnUpdateListener;
Expand Down Expand Up @@ -184,7 +185,7 @@ public void onSucceed(HttpData<SearchBlogsApi.Bean> result) {
// }

// 如果是放到外部存储的应用专属目录则不需要适配分区存储特性
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "EasyHttp.png");
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "我是测试专用的图片.png");

if (!file.exists()) {
// 生成图片到本地
Expand Down Expand Up @@ -286,7 +287,11 @@ public void onComplete(File file) {

@Override
public void onError(File file, Exception e) {
ToastUtils.show("下载出错:" + e.getMessage());
ToastUtils.show(e.getMessage());
if (e instanceof FileMD5Exception) {
// 如果是文件 md5 校验失败,则删除文件
file.delete();
}
}

@Override
Expand Down
27 changes: 24 additions & 3 deletions app/src/main/java/com/hjq/easy/demo/http/model/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +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.HttpException;
import com.hjq.http.exception.NetworkException;
import com.hjq.http.exception.NullBodyException;
Expand Down Expand Up @@ -61,9 +62,8 @@ public Object requestSucceed(@NonNull HttpRequest<?> httpRequest, @NonNull Respo
}

if (!response.isSuccessful()) {
// 返回响应异常
throw new ResponseException(mApplication.getString(R.string.http_response_error) + ", responseCode: " +
response.code() + ", message: " + response.message(), response);
throw new ResponseException(String.format(mApplication.getString(R.string.http_response_error),
response.code(), response.message()), response);
}

if (Headers.class.equals(type)) {
Expand Down Expand Up @@ -171,6 +171,27 @@ public Exception requestFail(@NonNull HttpRequest<?> httpRequest, @NonNull Excep
return new HttpException(e.getMessage(), e);
}

@NonNull
@Override
public Exception downloadFail(@NonNull HttpRequest<?> httpRequest, @NonNull Exception e) {
if (e instanceof ResponseException) {
ResponseException responseException = ((ResponseException) e);
Response response = responseException.getResponse();
responseException.setMessage(String.format(mApplication.getString(R.string.http_response_error),
response.code(), response.message()));
return responseException;
} else if (e instanceof NullBodyException) {
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);
fileMd5Exception.setMessage(mApplication.getString(R.string.http_response_md5_error));
return fileMd5Exception;
}
return requestFail(httpRequest, e);
}

@Nullable
@Override
public Object readCache(@NonNull HttpRequest<?> httpRequest, @NonNull Type type, long cacheTime) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<string name="http_data_explain_error">数据解析异常,请稍后</string>
<string name="http_server_out_time">服务器请求超时,请稍后再试</string>
<string name="http_network_error">请求失败,请检查网络设置</string>
<string name="http_response_error">服务器响应异常,请稍后再试</string>
<string name="http_response_error">服务器响应异常,请稍后再试,响应码:%d,响应信息:%s</string>
<string name="http_server_error">服务器连接异常,请稍后再试</string>
<string name="http_request_cancel">请求被中断,请重试</string>
<string name="http_response_null_body">服务器数据返回异常,请稍后再试</string>
<string name="http_response_md5_error">下载失败,文件 md5 校验失败</string>

</resources>
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 1100
versionName "11.0"
versionCode 1120
versionName "11.2"
}

// 使用 JDK 1.8
Expand Down
23 changes: 20 additions & 3 deletions library/src/main/java/com/hjq/http/EasyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hjq.http.config.IRequestServer;
import com.hjq.http.config.LogStrategy;
import com.hjq.http.config.RequestServer;
import com.hjq.http.model.ThreadSchedulers;

import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -57,6 +58,9 @@ public static EasyConfig with(OkHttpClient client) {
/** 通用请求头 */
private HashMap<String, String> mHeaders;

/** 线程调度器 */
private ThreadSchedulers mThreadSchedulers = ThreadSchedulers.MainThread;

/** 日志开关 */
private boolean mLogEnabled = true;
/** 日志 TAG */
Expand Down Expand Up @@ -144,6 +148,15 @@ public EasyConfig removeParam(String key) {
return this;
}

public EasyConfig setThreadSchedulers(ThreadSchedulers schedulers) {
if (mThreadSchedulers == null) {
// 线程调度器不能为空
throw new NullPointerException("Thread schedulers cannot be empty");
}
mThreadSchedulers = schedulers;
return this;
}

public EasyConfig setLogStrategy(ILogStrategy strategy) {
mLogStrategy = strategy;
return this;
Expand Down Expand Up @@ -201,6 +214,10 @@ public HashMap<String, String> getHeaders() {
return mHeaders;
}

public ThreadSchedulers getThreadSchedulers() {
return mThreadSchedulers;
}

public ILogStrategy getLogStrategy() {
return mLogStrategy;
}
Expand All @@ -223,15 +240,15 @@ public long getRetryTime() {

public void into() {
if (mClient == null) {
throw new IllegalArgumentException("The OkHttp client object cannot be empty");
throw new IllegalArgumentException("Please set up the OkHttpClient object");
}

if (mServer == null) {
throw new IllegalArgumentException("The host configuration cannot be empty");
throw new IllegalArgumentException("Please set up the RequestServer object");
}

if (mHandler == null) {
throw new IllegalArgumentException("The object being processed by the request cannot be empty");
throw new IllegalArgumentException("Please set the RequestHandler object");
}

try {
Expand Down

0 comments on commit b2b10da

Please sign in to comment.