Skip to content

Commit 32d5d4b

Browse files
committed
修复调用下载无法携带参数的问题
优化框架部分代码写法及类命名 补充《如何修改日志打印策略》文档
1 parent ead06c8 commit 32d5d4b

File tree

19 files changed

+115
-65
lines changed

19 files changed

+115
-65
lines changed

HelpDoc.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
* [如何设置请求超时时间](#如何设置请求超时时间)
6666

6767
* [如何设置不打印日志](#如何设置不打印日志)
68+
69+
* [如何修改日志打印策略](#如何修改日志打印策略)
6870

6971
* [如何取消已发起的请求](#如何取消已发起的请求)
7072

@@ -1130,6 +1132,24 @@ public final class XxxApi implements IRequestApi, IRequestClient {
11301132
EasyConfig.getInstance().setLogEnabled(false);
11311133
```
11321134

1135+
#### 如何修改日志打印策略
1136+
1137+
* 可以先定义一个类实现 [IHttpLogStrategy](library/src/main/java/com/hjq/http/config/IHttpLogStrategy.java) 接口,然后在框架初始化的时候传入即可
1138+
1139+
```java
1140+
EasyConfig.with(okHttpClient)
1141+
.......
1142+
// 设置自定义的日志打印策略
1143+
.setLogStrategy(new XxxStrategy())
1144+
.into();
1145+
```
1146+
1147+
* 需要修改日志打印策略的场景
1148+
1149+
* 需要将请求的日志写入到本地
1150+
1151+
* 需要修改打印的请求日志格式
1152+
11331153
#### 如何取消已发起的请求
11341154

11351155
```java

README.md

Lines changed: 6 additions & 4 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/11.5/EasyHttp.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/11.6/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.5'
64+
implementation 'com.github.getActivity:EasyHttp:11.6'
6565
// OkHttp 框架:https://github.com/square/okhttp
6666
// noinspection GradleDependency
6767
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
@@ -76,9 +76,9 @@ 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.5 | 2.9.0 | 3.0.4 |
79+
| 对应版本 | 11.6 | 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) |
81-
| **aar 包大小** | 86 KB | 123 KB | 131 KB |
81+
| **aar 包大小** | 89 KB | 123 KB | 131 KB |
8282
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
8383
| 配置多域名 ||||
8484
| **动态 Host** ||||
@@ -251,6 +251,8 @@ EasyHttp.post(this)
251251

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

254+
* 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)
255+
254256
#### 微信公众号:Android轮子哥
255257

256258
![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/official_ccount.png)

app/build.gradle

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

66
android {
77
compileSdkVersion 31
@@ -14,8 +14,8 @@ android {
1414
applicationId 'com.hjq.easy.demo'
1515
minSdkVersion 21
1616
targetSdkVersion 31
17-
versionCode 1150
18-
versionName '11.5'
17+
versionCode 1160
18+
versionName '11.6'
1919
}
2020

2121
// 支持 JDK 1.8
@@ -76,24 +76,24 @@ dependencies {
7676
implementation 'com.github.getActivity:ToastUtils:11.2'
7777

7878
// 权限请求框架:https://github.com/getActivity/XXPermissions
79-
implementation 'com.github.getActivity:XXPermissions:16.5'
79+
implementation 'com.github.getActivity:XXPermissions:16.6'
8080

8181
// 标题栏框架:https://github.com/getActivity/TitleBar
82-
implementation 'com.github.getActivity:TitleBar:9.6'
82+
implementation 'com.github.getActivity:TitleBar:10.0'
8383

8484
// Json 解析框架:https://github.com/google/gson
85-
implementation 'com.google.code.gson:gson:2.9.1'
85+
implementation 'com.google.code.gson:gson:2.10.1'
8686
// Gson 解析容错:https://github.com/getActivity/GsonFactory
87-
implementation 'com.github.getActivity:GsonFactory:6.3'
87+
implementation 'com.github.getActivity:GsonFactory:6.5'
8888

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

9292
// 日志调试框架:https://github.com/getActivity/Logcat
93-
debugImplementation 'com.github.getActivity:Logcat:11.0'
93+
debugImplementation 'com.github.getActivity:Logcat:11.2'
9494

9595
// OkHttp 抓包框架:https://github.com/lygttpod/AndroidMonitor
96-
debugImplementation 'io.github.lygttpod:monitor:0.0.7'
96+
// debugImplementation 'io.github.lygttpod:monitor:0.0.7'
9797

9898
// 内存泄漏监测框架:https://github.com/square/leakcanary
9999
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public boolean isShowDialog() {
3636
public void showDialog() {
3737
if (mDialog == null) {
3838
mDialog = new ProgressDialog(this);
39-
mDialog.setMessage(getResources().getString(R.string.http_loading));
39+
mDialog.setMessage(getResources().getString(R.string.dialog_loading_hint));
4040
mDialog.setCancelable(false);
4141
mDialog.setCanceledOnTouchOutside(false);
4242
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.view.View;
1414
import android.widget.ProgressBar;
1515

16+
import androidx.annotation.NonNull;
1617
import androidx.core.content.ContextCompat;
1718
import androidx.core.content.FileProvider;
1819

@@ -39,6 +40,7 @@
3940
import java.io.File;
4041
import java.io.IOException;
4142
import java.io.OutputStream;
43+
import java.util.ArrayList;
4244
import java.util.List;
4345

4446
import okhttp3.Call;
@@ -90,13 +92,11 @@ private void requestPermission() {
9092
*/
9193

9294
@Override
93-
public void onGranted(List<String> permissions, boolean all) {
94-
95-
}
95+
public void onGranted(@NonNull List<String> permissions, boolean allGranted) {}
9696

9797
@Override
98-
public void onDenied(List<String> permissions, boolean never) {
99-
if (never) {
98+
public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
99+
if (doNotAskAgain) {
100100
ToastUtils.show("授权失败,请手动授予存储权限");
101101
XXPermissions.startPermissionActivity(this, permissions);
102102
} else {
@@ -109,7 +109,7 @@ public void onDenied(List<String> permissions, boolean never) {
109109
protected void onRestart() {
110110
super.onRestart();
111111
if (XXPermissions.isGranted(this, Permission.Group.STORAGE)) {
112-
onGranted(null, true);
112+
onGranted(new ArrayList<>(), true);
113113
} else {
114114
requestPermission();
115115
}
@@ -133,10 +133,10 @@ public void onSucceed(HttpData<List<SearchAuthorApi.Bean>> result) {
133133

134134
} else if (viewId == R.id.btn_main_post) {
135135

136-
EasyHttp.post(this)
136+
EasyHttp.post(MainActivity.this)
137137
.api(new SearchBlogsApi()
138138
.setKeyword("搬砖不再有"))
139-
.request(new HttpCallback<HttpData<SearchBlogsApi.Bean>>(this) {
139+
.request(new HttpCallback<HttpData<SearchBlogsApi.Bean>>(MainActivity.this) {
140140

141141
@Override
142142
public void onSucceed(HttpData<SearchBlogsApi.Bean> result) {
@@ -311,31 +311,31 @@ private void installApk(final Context context, final File file) {
311311
.permission(Permission.REQUEST_INSTALL_PACKAGES)
312312
.request(new OnPermissionCallback() {
313313
@Override
314-
public void onGranted(List<String> permissions, boolean all) {
315-
if (!all) {
314+
public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
315+
if (!allGranted) {
316316
return;
317317
}
318-
Intent intent = new Intent();
319-
intent.setAction(Intent.ACTION_VIEW);
318+
Intent intent = new Intent(Intent.ACTION_VIEW);
320319
Uri uri;
321320
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
322321
if (file instanceof FileContentResolver) {
323322
uri = ((FileContentResolver) file).getContentUri();
324323
} else {
325324
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
326325
}
327-
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
328326
} else {
329327
uri = Uri.fromFile(file);
330328
}
331329

332330
intent.setDataAndType(uri, "application/vnd.android.package-archive");
333331
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
332+
// 对目标应用临时授权该 Uri 读写权限
333+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
334334
context.startActivity(intent);
335335
}
336336

337337
@Override
338-
public void onDenied(List<String> permissions, boolean never) {}
338+
public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {}
339339
});
340340
}
341341
}

app/src/main/java/com/hjq/easy/demo/http/model/RequestHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public Exception requestFail(@NonNull HttpRequest<?> httpRequest, @NonNull Excep
166166
}
167167

168168
if (e instanceof IOException) {
169+
// 出现该异常的两种情况
170+
// 1. 调用 EasyHttp.cancel
171+
// 2. 网络请求被中断
169172
return new CancelException(mApplication.getString(R.string.http_request_cancel), e);
170173
}
171174

app/src/main/res/values-v23/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<resources>
23

34
<!-- Base application theme. -->

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<resources>
23
<string name="app_name">EasyHttp</string>
34

4-
<string name="http_loading">加载中&#8230;</string>
5-
6-
<string name="http_unknown_error">请求出错,未知错误</string>
7-
<string name="http_token_error">登录失效,请重新登录</string>
8-
<string name="http_data_explain_error">数据解析异常,请稍后</string>
9-
<string name="http_server_out_time">服务器请求超时,请稍后再试</string>
10-
<string name="http_network_error">请求失败,请检查网络设置</string>
11-
<string name="http_response_error">服务器响应异常,请稍后再试,响应码:%d,响应信息:%s</string>
12-
<string name="http_server_error">服务器连接异常,请稍后再试</string>
13-
<string name="http_request_cancel">请求被中断,请重试</string>
14-
<string name="http_response_null_body">服务器数据返回异常,请稍后再试</string>
15-
<string name="http_response_md5_error">下载失败,文件 md5 校验失败</string>
5+
<string name="dialog_loading_hint">加载中&#8230;</string>
166

177
</resources>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="http_unknown_error">请求出错,未知错误</string>
5+
<string name="http_token_error">登录失效,请重新登录</string>
6+
<string name="http_data_explain_error">数据解析异常,请稍后</string>
7+
<string name="http_server_out_time">服务器请求超时,请稍后再试</string>
8+
<string name="http_network_error">请求失败,请检查网络设置</string>
9+
<string name="http_response_error">服务器响应异常,请稍后再试,响应码:%d,响应信息:%s</string>
10+
<string name="http_server_error">服务器连接异常,请稍后再试</string>
11+
<string name="http_request_cancel">请求被中断,请重试</string>
12+
<string name="http_response_null_body">服务器数据返回异常,请稍后再试</string>
13+
<string name="http_response_md5_error">下载失败,文件 md5 校验失败</string>
14+
15+
</resources>

app/src/main/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<resources>
23

34
<!-- Base application theme. -->

0 commit comments

Comments
 (0)