Skip to content

Commit df189b5

Browse files
committed
优化日志打印逻辑
优化 md5 校验逻辑 新增支持自定义日志 TAG 新增支持打印请求耗时 删除一些已经标记过时方法 纠正某个英文单词书写错误 修复参数解析逻辑上的 Bug
1 parent 12fd135 commit df189b5

23 files changed

+455
-432
lines changed

EasyHttp.apk

210 KB
Binary file not shown.

README.md

+265-257
Large diffs are not rendered by default.

app/build.gradle

+51-49
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
1-
apply plugin: 'com.android.application'
2-
3-
android {
4-
compileSdkVersion 28
5-
6-
// 支持 JDK 1.8
7-
compileOptions {
8-
targetCompatibility JavaVersion.VERSION_1_8
9-
sourceCompatibility JavaVersion.VERSION_1_8
10-
}
11-
12-
defaultConfig {
13-
applicationId "com.hjq.http.demo"
14-
minSdkVersion 14
15-
targetSdkVersion 28
16-
versionCode 50
17-
versionName "5.0"
18-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
19-
}
20-
buildTypes {
21-
release {
22-
minifyEnabled false
23-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24-
}
25-
debug {
26-
minifyEnabled false
27-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
28-
}
29-
}
30-
}
31-
32-
dependencies {
33-
implementation fileTree(include: ['*.jar'], dir: 'libs')
34-
implementation project(':library')
35-
36-
implementation 'com.android.support:appcompat-v7:28.0.0'
37-
implementation 'com.android.support:design:28.0.0'
38-
39-
// 标题栏:https://github.com/getActivity/TitleBar
40-
implementation 'com.hjq:titlebar:6.0'
41-
// 吐司工具类:https://github.com/getActivity/ToastUtils
42-
implementation 'com.hjq:toast:8.0'
43-
// 权限请求框架:https://github.com/getActivity/XXPermissions
44-
implementation 'com.hjq:xxpermissions:6.0'
45-
46-
// Json 解析框架:https://github.com/google/gson
47-
implementation 'com.google.code.gson:gson:2.8.5'
48-
// OkHttp 网络框架:https://github.com/square/okhttp
49-
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
6+
// 支持 JDK 1.8
7+
compileOptions {
8+
targetCompatibility JavaVersion.VERSION_1_8
9+
sourceCompatibility JavaVersion.VERSION_1_8
10+
}
11+
12+
defaultConfig {
13+
applicationId "com.hjq.http.demo"
14+
minSdkVersion 14
15+
targetSdkVersion 28
16+
versionCode 60
17+
versionName "6.0"
18+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
19+
}
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24+
}
25+
debug {
26+
minifyEnabled false
27+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
28+
}
29+
}
30+
}
31+
32+
dependencies {
33+
implementation fileTree(include: ['*.jar'], dir: 'libs')
34+
implementation project(':library')
35+
36+
implementation 'com.android.support:appcompat-v7:28.0.0'
37+
implementation 'com.android.support:design:28.0.0'
38+
39+
// 标题栏:https://github.com/getActivity/TitleBar
40+
implementation 'com.hjq:titlebar:6.0'
41+
// 吐司工具类:https://github.com/getActivity/ToastUtils
42+
implementation 'com.hjq:toast:8.0'
43+
// 权限请求框架:https://github.com/getActivity/XXPermissions
44+
implementation 'com.hjq:xxpermissions:6.2'
45+
// Json 解析框架:https://github.com/google/gson
46+
implementation 'com.google.code.gson:gson:2.8.5'
47+
// OkHttp 框架:https://github.com/square/okhttp
48+
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
49+
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
50+
// 日志调试:https://github.com/getActivity/Logcat
51+
debugImplementation 'com.hjq:logcat:6.0'
5052
}

app/src/main/AndroidManifest.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
android:resource="@xml/file_paths" />
3737
</provider>
3838

39-
<activity android:name=".MainActivity">
39+
<activity
40+
android:name=".MainActivity"
41+
android:configChanges="orientation|screenSize|keyboardHidden"
42+
android:launchMode="singleTop"
43+
android:screenOrientation="portrait">
4044
<intent-filter>
4145
<action android:name="android.intent.action.MAIN" />
4246
<action android:name="android.intent.action.VIEW" />

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void onClick(View v) {
103103
case R.id.btn_main_get:
104104
EasyHttp.get(this)
105105
.api(new SearchAuthorApi()
106-
.setAuthor("鸿洋"))
106+
.setAuthor("鸿洋"))
107107
.request(new HttpCallback<HttpData<SearchBean>>(this) {
108108

109109
@Override
@@ -115,12 +115,12 @@ public void onSucceed(HttpData<SearchBean> result) {
115115
case R.id.btn_main_post:
116116
EasyHttp.post(this)
117117
.api(new SearchBlogsApi()
118-
.setKeyword("搬砖不再有"))
118+
.setKeyword("搬砖不再有"))
119119
.request(new HttpCallback<HttpData<SearchBean>>(this) {
120120

121121
@Override
122122
public void onSucceed(HttpData<SearchBean> result) {
123-
ToastUtils.show("请求成功" + result.getData().getSize());
123+
ToastUtils.show("请求成功");
124124
}
125125
});
126126
break;

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

-3
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ public Exception requestFail(Context context, Exception e) {
147147
e = new HttpException(e.getMessage(), e);
148148
}
149149
}
150-
151-
// 打印错误信息
152-
EasyLog.print(e);
153150
return e;
154151
}
155152
}

app/src/main/java/com/hjq/http/demo/http/request/SearchBlogsApi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ public SearchBlogsApi setKeyword(String keyword) {
2424
this.keyword = keyword;
2525
return this;
2626
}
27-
}
27+
}

library/build.gradle

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// 文件开头配置
21
apply plugin: 'com.android.library'
32
apply plugin: 'com.novoda.bintray-release'
43

@@ -14,26 +13,26 @@ android {
1413
defaultConfig {
1514
minSdkVersion 14
1615
targetSdkVersion 26
17-
versionCode 50
18-
versionName "5.0"
16+
versionCode 60
17+
versionName "6.0"
1918
}
2019
}
2120

2221
dependencies {
23-
compileOnly 'com.squareup.okhttp3:okhttp:3.12.1'
24-
compileOnly 'com.android.support:support-fragment:26.0.0'
22+
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
23+
implementation 'com.android.support:support-fragment:26.0.0'
2524
}
2625

2726
publish {
28-
userOrg = 'getactivity'//填写bintray用户名,注意大小写
29-
groupId = 'com.hjq'//定义的maven group id最终引用形式
30-
artifactId = 'http'//maven的artifact id
31-
version = '5.0'//maven 上发布版本号
32-
description = 'Easy-to-use network request framework'//描述,自己定义
33-
website = "https://github.com/getActivity/EasyHttp"//项目在github中的地址
27+
userOrg = 'getactivity'
28+
groupId = 'com.hjq'
29+
artifactId = 'http'
30+
version = '6.0'
31+
description = 'Easy-to-use network request framework'
32+
website = "https://github.com/getActivity/EasyHttp"
3433
}
3534

36-
tasks.withType(Javadoc) {//防止编码问题
35+
tasks.withType(Javadoc) {
3736
options.addStringOption('Xdoclint:none', '-quiet')
3837
options.addStringOption('encoding', 'UTF-8')
3938
options.addStringOption('charSet', 'UTF-8')

library/src/main/java/com/hjq/http/EasyConfig.java

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static EasyConfig with(OkHttpClient client) {
5151

5252
/** 日志开关 */
5353
private boolean mLogEnabled = true;
54+
/** 日志 TAG */
55+
private String mLogTag = "EasyHttp";
5456
/** 重试次数 */
5557
private int mRetryCount;
5658

@@ -104,6 +106,11 @@ public EasyConfig setLogEnabled(boolean enabled) {
104106
return this;
105107
}
106108

109+
public EasyConfig setLogTag(String tag) {
110+
mLogTag = tag;
111+
return this;
112+
}
113+
107114
public EasyConfig setRetryCount(int count) {
108115
if (count < 0) {
109116
throw new IllegalArgumentException("The number of retries must be greater than 0");
@@ -136,6 +143,10 @@ public boolean isLogEnabled() {
136143
return mLogEnabled;
137144
}
138145

146+
public String getLogTag() {
147+
return mLogTag;
148+
}
149+
139150
public int getRetryCount() {
140151
return mRetryCount;
141152
}

library/src/main/java/com/hjq/http/EasyLog.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hjq.http;
22

3+
import android.text.TextUtils;
34
import android.util.Log;
45

56
/**
@@ -10,8 +11,6 @@
1011
*/
1112
public final class EasyLog {
1213

13-
private static final String TAG = "EasyHttp";
14-
1514
/**
1615
* 日志开关
1716
*/
@@ -24,7 +23,7 @@ public static boolean isEnable() {
2423
*/
2524
public static void print(String log) {
2625
if (isEnable()) {
27-
Log.d(TAG, log != null ? log : "null");
26+
Log.d(EasyConfig.getInstance().getLogTag(), log != null ? log : "null");
2827
}
2928
}
3029

@@ -33,7 +32,7 @@ public static void print(String log) {
3332
*/
3433
public static void print(Throwable throwable) {
3534
if (EasyConfig.getInstance().isLogEnabled()) {
36-
throwable.printStackTrace();
35+
Log.e(EasyConfig.getInstance().getLogTag(), throwable.getMessage(), throwable);
3736
}
3837
}
3938

@@ -57,7 +56,7 @@ public static void print() {
5756
public static void json(String json) {
5857
if (isEnable()) {
5958
String text = stringToJSON(json);
60-
if (text != null && !"".equals(text)) {
59+
if (!TextUtils.isEmpty(text)) {
6160
// 打印 Json 数据最好换一行再打印会好看一点
6261
text = " \n" + text;
6362

library/src/main/java/com/hjq/http/callback/DownloadCallback.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hjq.http.callback;
22

3+
import android.text.TextUtils;
4+
35
import com.hjq.http.EasyLog;
46
import com.hjq.http.EasyUtils;
57
import com.hjq.http.exception.MD5Exception;
@@ -23,6 +25,9 @@
2325
*/
2426
public final class DownloadCallback extends BaseCallback {
2527

28+
/** 文件 MD5 正则表达式 */
29+
private static final String FILE_MD5_REGEX = "^[\\w]{32}$";
30+
2631
/** 下载任务 */
2732
private DownloadInfo mDownloadInfo;
2833
/** 保存的文件 */
@@ -44,9 +49,14 @@ public DownloadCallback(CallProxy call, File file, String md5, OnDownloadListene
4449

4550
@Override
4651
protected void onResponse(Response response) throws Exception {
47-
if (mMD5 == null || "".equals(mMD5)) {
52+
// 如果没有指定文件的 md5 值
53+
if (mMD5 == null) {
4854
// 获取响应头中的文件 MD5 值
49-
mMD5 = response.header("Content-MD5");
55+
String md5 = response.header("Content-MD5");
56+
// 这个 md5 值必须是文件的 md5 值
57+
if (!TextUtils.isEmpty(md5) && md5.matches(FILE_MD5_REGEX)) {
58+
mMD5 = md5;
59+
}
5060
}
5161

5262
EasyUtils.createFolder(mFile.getParentFile());
@@ -62,7 +72,7 @@ protected void onResponse(Response response) throws Exception {
6272

6373
mDownloadInfo.setTotalLength(body.contentLength());
6474
// 如果这个文件已经下载过,并且经过校验 MD5 是同一个文件的话,就直接回调下载成功监听
65-
if (mMD5 != null && !"".equals(mMD5) && mFile.exists() && mFile.isFile() && mMD5.equalsIgnoreCase(EasyUtils.getFileMD5(mFile))) {
75+
if (!TextUtils.isEmpty(mMD5) && mFile.exists() && mFile.isFile() && mMD5.equalsIgnoreCase(EasyUtils.getFileMD5(mFile))) {
6676
EasyUtils.runOnUiThread(mListener != null, () -> {
6777
mDownloadInfo.setDownloadLength(mDownloadInfo.getTotalLength());
6878
mListener.onComplete(mDownloadInfo);
@@ -89,9 +99,9 @@ protected void onResponse(Response response) throws Exception {
8999
outputStream.flush();
90100

91101
EasyUtils.runOnUiThread(mListener != null, () -> {
92-
String fileMD5 = EasyUtils.getFileMD5(mDownloadInfo.getFile());
93-
if (mMD5 != null && !"".equals(mMD5) && !mMD5.equalsIgnoreCase(fileMD5)) {
94-
onFailure(new MD5Exception("MD5 verify failure", fileMD5));
102+
String md5 = EasyUtils.getFileMD5(mDownloadInfo.getFile());
103+
if (!TextUtils.isEmpty(mMD5) && !mMD5.equalsIgnoreCase(md5)) {
104+
onFailure(new MD5Exception("MD5 verify failure", md5));
95105
} else {
96106
mListener.onComplete(mDownloadInfo);
97107
mListener.onEnd(getCall());

library/src/main/java/com/hjq/http/callback/NormalCallback.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ public final class NormalCallback extends BaseCallback {
2323

2424
private Context mContext;
2525
private OnHttpListener mListener;
26+
private long mRequestTime;
2627

2728
public NormalCallback(Context context, CallProxy call, OnHttpListener listener) {
2829
super(call);
2930
mContext = context;
3031
mListener = listener;
32+
mRequestTime = System.currentTimeMillis();
3133

32-
EasyUtils.runOnUiThread(mListener != null, () -> {
33-
mListener.onStart(call);
34-
EasyConfig.getInstance().getHandler().requestStart(context, call);
35-
});
34+
EasyUtils.runOnUiThread(mListener != null, () -> mListener.onStart(call));
3635
}
3736

3837
@SuppressWarnings("unchecked")
@@ -48,11 +47,11 @@ protected void onResponse(Response response) throws Exception {
4847
type = ((ParameterizedType) mListener.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
4948
}
5049

50+
EasyLog.print("RequestTime:" + (System.currentTimeMillis() - mRequestTime) + " ms");
5151
final Object result = EasyConfig.getInstance().getHandler().requestSucceed(mContext, response, type);
5252
EasyUtils.runOnUiThread(mListener != null, () -> {
5353
mListener.onSucceed(result);
5454
mListener.onEnd(getCall());
55-
EasyConfig.getInstance().getHandler().requestEnd(mContext, getCall());
5655
});
5756
}
5857

@@ -63,7 +62,6 @@ protected void onFailure(Exception e) {
6362
EasyUtils.runOnUiThread(mListener != null, () -> {
6463
mListener.onFail(exception);
6564
mListener.onEnd(getCall());
66-
EasyConfig.getInstance().getHandler().requestEnd(mContext, getCall());
6765
});
6866
}
6967
}

0 commit comments

Comments
 (0)