Skip to content

Commit

Permalink
修复调用下载无法携带参数的问题
Browse files Browse the repository at this point in the history
优化框架部分代码写法及类命名
补充《如何修改日志打印策略》文档
  • Loading branch information
getActivity committed Feb 5, 2023
1 parent ead06c8 commit 32d5d4b
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 65 deletions.
20 changes: 20 additions & 0 deletions HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
* [如何设置请求超时时间](#如何设置请求超时时间)

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

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

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

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

#### 如何修改日志打印策略

* 可以先定义一个类实现 [IHttpLogStrategy](library/src/main/java/com/hjq/http/config/IHttpLogStrategy.java) 接口,然后在框架初始化的时候传入即可

```java
EasyConfig.with(okHttpClient)
.......
// 设置自定义的日志打印策略
.setLogStrategy(new XxxStrategy())
.into();
```

* 需要修改日志打印策略的场景

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

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

#### 如何取消已发起的请求

```java
Expand Down
10 changes: 6 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.5/EasyHttp.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/11.6/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.5'
implementation 'com.github.getActivity:EasyHttp:11.6'
// 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.5 | 2.9.0 | 3.0.4 |
| 对应版本 | 11.6 | 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 包大小** | 86 KB | 123 KB | 131 KB |
| **aar 包大小** | 89 KB | 123 KB | 131 KB |
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
| 配置多域名 ||||
| **动态 Host** ||||
Expand Down Expand Up @@ -251,6 +251,8 @@ EasyHttp.post(this)

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

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

![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/official_ccount.png)
Expand Down
18 changes: 9 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'monitor-plugin'
//apply plugin: 'monitor-plugin'

android {
compileSdkVersion 31
Expand All @@ -14,8 +14,8 @@ android {
applicationId 'com.hjq.easy.demo'
minSdkVersion 21
targetSdkVersion 31
versionCode 1150
versionName '11.5'
versionCode 1160
versionName '11.6'
}

// 支持 JDK 1.8
Expand Down Expand Up @@ -76,24 +76,24 @@ dependencies {
implementation 'com.github.getActivity:ToastUtils:11.2'

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

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

// Json 解析框架:https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.google.code.gson:gson:2.10.1'
// Gson 解析容错:https://github.com/getActivity/GsonFactory
implementation 'com.github.getActivity:GsonFactory:6.3'
implementation 'com.github.getActivity:GsonFactory:6.5'

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

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

// OkHttp 抓包框架:https://github.com/lygttpod/AndroidMonitor
debugImplementation 'io.github.lygttpod:monitor:0.0.7'
// debugImplementation 'io.github.lygttpod:monitor:0.0.7'

// 内存泄漏监测框架:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/hjq/easy/demo/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public boolean isShowDialog() {
public void showDialog() {
if (mDialog == null) {
mDialog = new ProgressDialog(this);
mDialog.setMessage(getResources().getString(R.string.http_loading));
mDialog.setMessage(getResources().getString(R.string.dialog_loading_hint));
mDialog.setCancelable(false);
mDialog.setCanceledOnTouchOutside(false);
}
Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/com/hjq/easy/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.View;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

Expand All @@ -39,6 +40,7 @@
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
Expand Down Expand Up @@ -90,13 +92,11 @@ private void requestPermission() {
*/

@Override
public void onGranted(List<String> permissions, boolean all) {

}
public void onGranted(@NonNull List<String> permissions, boolean allGranted) {}

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

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

EasyHttp.post(this)
EasyHttp.post(MainActivity.this)
.api(new SearchBlogsApi()
.setKeyword("搬砖不再有"))
.request(new HttpCallback<HttpData<SearchBlogsApi.Bean>>(this) {
.request(new HttpCallback<HttpData<SearchBlogsApi.Bean>>(MainActivity.this) {

@Override
public void onSucceed(HttpData<SearchBlogsApi.Bean> result) {
Expand Down Expand Up @@ -311,31 +311,31 @@ private void installApk(final Context context, final File file) {
.permission(Permission.REQUEST_INSTALL_PACKAGES)
.request(new OnPermissionCallback() {
@Override
public void onGranted(List<String> permissions, boolean all) {
if (!all) {
public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
if (!allGranted) {
return;
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (file instanceof FileContentResolver) {
uri = ((FileContentResolver) file).getContentUri();
} else {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
uri = Uri.fromFile(file);
}

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

@Override
public void onDenied(List<String> permissions, boolean never) {}
public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public Exception requestFail(@NonNull HttpRequest<?> httpRequest, @NonNull Excep
}

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

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-v23/styles.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- Base application theme. -->
Expand Down
14 changes: 2 additions & 12 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">EasyHttp</string>

<string name="http_loading">加载中&#8230;</string>

<string name="http_unknown_error">请求出错,未知错误</string>
<string name="http_token_error">登录失效,请重新登录</string>
<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">服务器响应异常,请稍后再试,响应码:%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>
<string name="dialog_loading_hint">加载中&#8230;</string>

</resources>
15 changes: 15 additions & 0 deletions app/src/main/res/values/strings_http.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="http_unknown_error">请求出错,未知错误</string>
<string name="http_token_error">登录失效,请重新登录</string>
<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">服务器响应异常,请稍后再试,响应码:%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>
1 change: 1 addition & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- Base application theme. -->
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
// OkHttp 抓包框架:https://github.com/lygttpod/AndroidMonitor
classpath 'io.github.lygttpod:monitor-plugin:0.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
// OkHttp 抓包框架:https://github.com/lygttpod/AndroidMonitor
// classpath 'io.github.lygttpod:monitor-plugin:0.0.1'
}
}

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 1150
versionName "11.5"
versionCode 1160
versionName "11.6"
}

// 使用 JDK 1.8
Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/com/hjq/http/EasyConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.hjq.http;

import com.hjq.http.config.ILogStrategy;
import com.hjq.http.config.DefaultHttpLogStrategy;
import com.hjq.http.config.IHttpLogStrategy;
import com.hjq.http.config.IRequestHandler;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.config.IRequestServer;
import com.hjq.http.config.LogStrategy;
import com.hjq.http.config.RequestServer;
import com.hjq.http.model.ThreadSchedulers;

Expand Down Expand Up @@ -48,7 +48,7 @@ public static EasyConfig with(OkHttpClient client) {
/** 请求拦截器 */
private IRequestInterceptor mInterceptor;
/** 日志打印策略 */
private ILogStrategy mLogStrategy;
private IHttpLogStrategy mLogStrategy;

/** OkHttp 客户端 */
private OkHttpClient mClient;
Expand Down Expand Up @@ -157,7 +157,7 @@ public EasyConfig setThreadSchedulers(ThreadSchedulers schedulers) {
return this;
}

public EasyConfig setLogStrategy(ILogStrategy strategy) {
public EasyConfig setLogStrategy(IHttpLogStrategy strategy) {
mLogStrategy = strategy;
return this;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ public ThreadSchedulers getThreadSchedulers() {
return mThreadSchedulers;
}

public ILogStrategy getLogStrategy() {
public IHttpLogStrategy getLogStrategy() {
return mLogStrategy;
}

Expand Down Expand Up @@ -260,7 +260,7 @@ public void into() {
}

if (mLogStrategy == null) {
mLogStrategy = new LogStrategy();
mLogStrategy = new DefaultHttpLogStrategy();
}
EasyConfig.setInstance(this);
}
Expand Down
3 changes: 1 addition & 2 deletions library/src/main/java/com/hjq/http/EasyLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ private static String getLogTag(HttpRequest<?> httpRequest) {
if (httpRequest == null) {
return logTag;
}
return logTag + " " + httpRequest.getRequestApi().getClass().getSimpleName() +
"@" + Integer.toHexString(httpRequest.hashCode());
return logTag + " " + httpRequest.generateLogTag();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,14 @@ private void callOnProgress() {
mListener.onByte(mFile, mTotalByte, mDownloadByte);
int progress = EasyUtils.getProgressProgress(mTotalByte, mDownloadByte);
// 只有下载进度发生改变的时候才回调此方法,避免引起不必要的 View 重绘
if (progress != mDownloadProgress) {
mDownloadProgress = progress;
mListener.onProgress(mFile, mDownloadProgress);
EasyLog.printLog(mHttpRequest, mFile.getPath() +
", downloaded: " + mDownloadByte + " / " + mTotalByte +
", progress: " + progress + " %");
if (progress == mDownloadProgress) {
return;
}
mDownloadProgress = progress;
mListener.onProgress(mFile, mDownloadProgress);
EasyLog.printLog(mHttpRequest, mFile.getPath() +
", downloaded: " + mDownloadByte + " / " + mTotalByte +
", progress: " + progress + " %");
}

private void callOnComplete(boolean cache) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* time : 2020/04/24
* desc : 网络请求日志打印默认实现
*/
public final class LogStrategy implements ILogStrategy {
public final class DefaultHttpLogStrategy implements IHttpLogStrategy {

@Override
public void printLog(String tag, String log) {
Expand Down
Loading

0 comments on commit 32d5d4b

Please sign in to comment.