Skip to content

Commit 0e113c3

Browse files
committed
修改网络请求回调接口的参数类型
新增框架内部删除接口缓存的逻辑 优化框架内部读写接口缓存的逻辑 优化框架内部 API 命名及代码写法 修复同步请求没有回调 interceptResponse 方法的问题 修复同步请求在设置优先使用缓存的情况下解析泛型异常的问题
1 parent d49f3c5 commit 0e113c3

22 files changed

+283
-167
lines changed

HelpDoc.md

Lines changed: 154 additions & 76 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 8 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/12.8/EasyHttp.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/13.0/EasyHttp.apk)
88

99
![](picture/demo_code.png)
1010

@@ -63,7 +63,7 @@ android {
6363
6464
dependencies {
6565
// 网络请求框架:https://github.com/getActivity/EasyHttp
66-
implementation 'com.github.getActivity:EasyHttp:12.8'
66+
implementation 'com.github.getActivity:EasyHttp:13.0'
6767
// OkHttp 框架:https://github.com/square/okhttp
6868
// noinspection GradleDependency
6969
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
@@ -100,6 +100,10 @@ dependencies {
100100
-keep public class * implements com.hjq.http.listener.OnHttpListener {
101101
*;
102102
}
103+
104+
-keep public class * extends com.hjq.http.model.ResponseClass {
105+
*;
106+
}
103107
```
104108

105109
* 不混淆某个包下的 Bean 类
@@ -119,9 +123,9 @@ dependencies {
119123

120124
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
121125
| :----: | :------: | :-----: | :-----: |
122-
| 对应版本 | 12.8 | 2.9.0 | 3.0.4 |
126+
| 对应版本 | 13.0 | 2.9.0 | 3.0.4 |
123127
| 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) |
124-
| **aar 包大小** | 95 KB | 123 KB | 131 KB |
128+
| **aar 包大小** | 96 KB | 123 KB | 131 KB |
125129
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
126130
| 配置多域名 ||||
127131
| **动态 Host** ||||

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ android {
1414
applicationId 'com.hjq.easy.demo'
1515
minSdkVersion 21
1616
targetSdkVersion 31
17-
versionCode 1208
18-
versionName '12.8'
17+
versionCode 1300
18+
versionName '13.0'
1919
}
2020

2121
// 支持 JDK 1.8
@@ -76,13 +76,13 @@ dependencies {
7676
implementation 'com.github.getActivity:Toaster:12.6'
7777

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

8181
// 标题栏框架:https://github.com/getActivity/TitleBar
8282
implementation 'com.github.getActivity:TitleBar:10.5'
8383

8484
// Gson 解析容错:https://github.com/getActivity/GsonFactory
85-
implementation 'com.github.getActivity:GsonFactory:9.5'
85+
implementation 'com.github.getActivity:GsonFactory:9.6'
8686
// Json 解析框架:https://github.com/google/gson
8787
implementation 'com.google.code.gson:gson:2.10.1'
8888
// Kotlin 反射库:用于反射 Kotlin data class 类对象

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
android:networkSecurityConfig="@xml/network_security_config"
2424
android:requestLegacyExternalStorage="true"
2525
android:roundIcon="@mipmap/ic_launcher_round"
26+
android:supportsRtl="true"
2627
android:theme="@style/AppTheme"
2728
tools:ignore="LockedOrientationActivity,UnusedAttribute"
2829
tools:targetApi="n">

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.hjq.easy.demo;
22

33
import android.app.ProgressDialog;
4+
import androidx.annotation.NonNull;
45
import androidx.appcompat.app.AppCompatActivity;
6+
import com.hjq.http.config.IRequestApi;
57
import com.hjq.http.listener.OnHttpListener;
68
import com.hjq.toast.Toaster;
7-
import okhttp3.Call;
89

910
/**
1011
* author : Android 轮子哥
@@ -57,20 +58,20 @@ public void hideDialog() {
5758
}
5859

5960
@Override
60-
public void onHttpStart(Call call) {
61+
public void onHttpStart(@NonNull IRequestApi api) {
6162
showDialog();
6263
}
6364

6465
@Override
65-
public void onHttpSuccess(Object result) {}
66+
public void onHttpSuccess(@NonNull Object result) {}
6667

6768
@Override
68-
public void onHttpFail(Throwable throwable) {
69+
public void onHttpFail(@NonNull Throwable throwable) {
6970
Toaster.show(throwable.getMessage());
7071
}
7172

7273
@Override
73-
public void onHttpEnd(Call call) {
74+
public void onHttpEnd(@NonNull IRequestApi api) {
7475
hideDialog();
7576
}
7677
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.hjq.easy.demo.http.model.HttpData;
2424
import com.hjq.http.EasyHttp;
2525
import com.hjq.http.EasyUtils;
26+
import com.hjq.http.config.IRequestApi;
2627
import com.hjq.http.listener.HttpCallbackProxy;
2728
import com.hjq.http.listener.OnDownloadListener;
2829
import com.hjq.http.listener.OnUpdateListener;
@@ -38,7 +39,6 @@
3839
import java.io.OutputStream;
3940
import java.util.ArrayList;
4041
import java.util.List;
41-
import okhttp3.Call;
4242

4343
/**
4444
* author : Android 轮子哥
@@ -121,20 +121,20 @@ public void onClick(View view) {
121121
.request(new HttpCallbackProxy<HttpData<List<SearchAuthorApi.Bean>>>(this) {
122122

123123
@Override
124-
public void onHttpSuccess(HttpData<List<SearchAuthorApi.Bean>> result) {
124+
public void onHttpSuccess(@NonNull HttpData<List<SearchAuthorApi.Bean>> result) {
125125
Toaster.show("Get 请求成功,请看日志");
126126
}
127127
});
128128

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

131131
EasyHttp.post(this)
132-
.api(new SearchBlogsApi()
133-
.setKeyword("搬砖不再有"))
132+
.api(new SearchBlogsApi()
133+
.setKeyword("搬砖不再有"))
134134
.request(new HttpCallbackProxy<HttpData<SearchBlogsApi.Bean>>(MainActivity.this) {
135135

136136
@Override
137-
public void onHttpSuccess(HttpData<SearchBlogsApi.Bean> result) {
137+
public void onHttpSuccess(@NonNull HttpData<SearchBlogsApi.Bean> result) {
138138
Toaster.show("Post 请求成功,请看日志");
139139
}
140140
});
@@ -204,7 +204,7 @@ public void onHttpSuccess(HttpData<SearchBlogsApi.Bean> result) {
204204
.request(new OnUpdateListener<Void>() {
205205

206206
@Override
207-
public void onUpdateStart(Call call) {
207+
public void onUpdateStart(@NonNull IRequestApi api) {
208208
mProgressBar.setProgress(0);
209209
mProgressBar.setVisibility(View.VISIBLE);
210210
}
@@ -215,17 +215,17 @@ public void onUpdateProgressChange(int progress) {
215215
}
216216

217217
@Override
218-
public void onUpdateSuccess(Void result) {
218+
public void onUpdateSuccess(@NonNull Void result) {
219219
Toaster.show("上传成功");
220220
}
221221

222222
@Override
223-
public void onUpdateFail(Throwable throwable) {
223+
public void onUpdateFail(@NonNull Throwable throwable) {
224224
Toaster.show("上传失败");
225225
}
226226

227227
@Override
228-
public void onUpdateEnd(Call call) {
228+
public void onUpdateEnd(@NonNull IRequestApi api) {
229229
mProgressBar.setVisibility(View.GONE);
230230
}
231231
});
@@ -273,30 +273,30 @@ public void onUpdateEnd(Call call) {
273273
.listener(new OnDownloadListener() {
274274

275275
@Override
276-
public void onDownloadStart(File file) {
276+
public void onDownloadStart(@NonNull File file) {
277277
mProgressBar.setProgress(0);
278278
mProgressBar.setVisibility(View.VISIBLE);
279279
}
280280

281281
@Override
282-
public void onDownloadProgressChange(File file, int progress) {
282+
public void onDownloadProgressChange(@NonNull File file, int progress) {
283283
mProgressBar.setProgress(progress);
284284
}
285285

286286
@Override
287-
public void onDownloadSuccess(File file) {
287+
public void onDownloadSuccess(@NonNull File file) {
288288
Toaster.show("下载成功:" + file.getPath());
289289
installApk(MainActivity.this, file);
290290
}
291291

292292
@Override
293-
public void onDownloadFail(File file, Throwable throwable) {
293+
public void onDownloadFail(@NonNull File file, @NonNull Throwable throwable) {
294294
Toaster.show("下载失败:" + throwable.getMessage());
295295
file.delete();
296296
}
297297

298298
@Override
299-
public void onDownloadEnd(File file) {
299+
public void onDownloadEnd(@NonNull File file) {
300300
mProgressBar.setVisibility(View.GONE);
301301
}
302302
})

app/src/main/java/com/hjq/easy/demo/http/api/SearchAuthorApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public SearchAuthorApi setId(int id) {
2626
return this;
2727
}
2828

29-
public final static class Bean {
29+
public static final class Bean {
3030

3131
private int courseId;
3232
private int id;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.hjq.easy.demo.http.model;
22

33
import androidx.annotation.NonNull;
4-
54
import com.hjq.gson.factory.GsonFactory;
65
import com.hjq.http.config.IRequestApi;
76
import com.hjq.http.request.HttpRequest;
@@ -22,6 +21,7 @@ public final class HttpCacheManager {
2221
/**
2322
* 生成缓存的 key
2423
*/
24+
@NonNull
2525
public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
2626
IRequestApi requestApi = httpRequest.getRequestApi();
2727
return "请替换成当前的用户 id" + "\n" + requestApi.getApi() + "\n" + GsonFactory.getSingletonGson().toJson(requestApi);
@@ -30,9 +30,9 @@ public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
3030
/**
3131
* 读取缓存
3232
*/
33-
public static String readHttpCache(String cacheKey) {
33+
public static String readHttpCache(@NonNull String cacheKey) {
3434
String cacheValue = HTTP_CACHE_CONTENT.getString(cacheKey, null);
35-
if ("".equals(cacheValue) || "{}".equals(cacheValue)) {
35+
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
3636
return null;
3737
}
3838
return cacheValue;
@@ -45,6 +45,13 @@ public static boolean writeHttpCache(String cacheKey, String cacheValue) {
4545
return HTTP_CACHE_CONTENT.putString(cacheKey, cacheValue).commit();
4646
}
4747

48+
/**
49+
* 删除缓存
50+
*/
51+
public static boolean deleteHttpCache(String cacheKey) {
52+
return HTTP_CACHE_CONTENT.remove(cacheKey).commit();
53+
}
54+
4855
/**
4956
* 清理缓存
5057
*/

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public Throwable downloadFail(@NonNull HttpRequest<?> httpRequest, @NonNull Thro
205205
public Object readCache(@NonNull HttpRequest<?> httpRequest, @NonNull Type type, long cacheTime) {
206206
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
207207
String cacheValue = HttpCacheManager.readHttpCache(cacheKey);
208-
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
208+
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
209209
return null;
210210
}
211211
EasyLog.printLog(httpRequest, "----- read cache key -----");
@@ -226,7 +226,7 @@ public Object readCache(@NonNull HttpRequest<?> httpRequest, @NonNull Type type,
226226
public boolean writeCache(@NonNull HttpRequest<?> httpRequest, @NonNull Response response, @NonNull Object result) {
227227
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
228228
String cacheValue = GsonFactory.getSingletonGson().toJson(result);
229-
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
229+
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
230230
return false;
231231
}
232232
EasyLog.printLog(httpRequest, "----- write cache key -----");
@@ -240,6 +240,16 @@ public boolean writeCache(@NonNull HttpRequest<?> httpRequest, @NonNull Response
240240
return writeHttpCacheResult && refreshHttpCacheTimeResult;
241241
}
242242

243+
@Override
244+
public boolean deleteCache(@NonNull HttpRequest<?> httpRequest) {
245+
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
246+
EasyLog.printLog(httpRequest, "----- delete cache key -----");
247+
EasyLog.printJson(httpRequest, cacheKey);
248+
boolean deleteHttpCacheResult = HttpCacheManager.deleteHttpCache(cacheKey);
249+
EasyLog.printLog(httpRequest, "deleteHttpCacheResult = " + deleteHttpCacheResult);
250+
return deleteHttpCacheResult;
251+
}
252+
243253
@Override
244254
public void clearCache() {
245255
HttpCacheManager.clearCache();

app/src/main/java/com/hjq/easy/demo/other/ContentResolverUriStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class ContentResolverUriStore {
2727
// 使用字符串作为缓存键
2828
String cacheKey = "ContentResolver insert: " + "Uri = " + url + ", ContentValues = " + convertContentValuesToString(values);
2929
String oldUriString = sharedPreferences.getString(cacheKey, "");
30-
if (oldUriString != null && !"".equals(oldUriString)) {
30+
if (oldUriString != null && !oldUriString.isEmpty()) {
3131
Cursor cursor = null;
3232
try {
3333
Uri oldUri = Uri.parse(oldUriString);

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55

66
defaultConfig {
77
minSdkVersion 16
8-
versionCode 1208
9-
versionName "12.8"
8+
versionCode 1300
9+
versionName "13.0"
1010
}
1111

1212
// 使用 JDK 1.8

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ public BaseCallback(@NonNull HttpRequest<?> request) {
4141
() -> HttpLifecycleManager.register(mHttpRequest.getLifecycleOwner()));
4242
}
4343

44-
public BaseCallback setCallProxy(CallProxy callProxy) {
45-
mCallProxy = callProxy;
46-
return this;
47-
}
48-
4944
public BaseCallback setCallProxyFactory(CallProxy.Factory factory) {
5045
mCallProxyFactory = factory;
5146
return this;

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public final class DownloadCallback extends BaseCallback {
3838
private static final String FILE_MD5_REGEX = "^[\\w]{32}$";
3939

4040
/** 保存的文件 */
41-
private File mFile;
41+
@NonNull
42+
private final File mFile;
4243

4344
/** 校验的 MD5 */
4445
private String mMd5;
@@ -58,14 +59,10 @@ public final class DownloadCallback extends BaseCallback {
5859
/** 断点续传开关 */
5960
private boolean mResumableTransfer;
6061

61-
public DownloadCallback(@NonNull HttpRequest<?> request) {
62+
public DownloadCallback(@NonNull HttpRequest<?> request, @NonNull File file) {
6263
super(request);
6364
mHttpRequest = request;
64-
}
65-
66-
public DownloadCallback setFile(File file) {
6765
mFile = file;
68-
return this;
6966
}
7067

7168
public DownloadCallback setMd5(String md5) {
@@ -151,10 +148,10 @@ protected void onHttpResponse(Response response) throws Throwable {
151148
String contentRange = response.header("Content-Range");
152149
// 若能够找到 Content-Range,则表明服务器支持断点续传
153150
// 有些服务器还会返回 Accept-Ranges,输出结果 Accept-Ranges: bytes,说明服务器支持按字节下载
154-
if (acceptRanges != null && !"".equals(acceptRanges)) {
151+
if (acceptRanges != null && !acceptRanges.isEmpty()) {
155152
// Accept-Ranges:bytes
156153
supportResumableTransfer = "bytes".equalsIgnoreCase(acceptRanges);
157-
} else if (contentRange != null && !"".equals(contentRange)) {
154+
} else if (contentRange != null && !contentRange.isEmpty()) {
158155
// Content-Range: bytes 7897088-221048888/221048889
159156
supportResumableTransfer = contentRange.matches("bytes\\s+\\d+-\\d+/\\d+");
160157
}
@@ -203,7 +200,7 @@ protected void onHttpResponse(Response response) throws Throwable {
203200
EasyUtils.closeStream(response);
204201

205202
String md5 = EasyUtils.getFileMd5(EasyUtils.openFileInputStream(mFile));
206-
if (mMd5 != null && !"".equals(mMd5) && !mMd5.equalsIgnoreCase(md5)) {
203+
if (mMd5 != null && !mMd5.isEmpty() && !mMd5.equalsIgnoreCase(md5)) {
207204
// 文件 MD5 值校验失败
208205
throw new FileMd5Exception("File md5 hash verify failure", md5);
209206
}
@@ -224,7 +221,7 @@ protected void onHttpFailure(final Throwable throwable) {
224221

225222
public boolean verifyFileMd5() {
226223
try {
227-
return mFile.isFile() && mMd5 != null && !"".equals(mMd5) &&
224+
return mFile.isFile() && mMd5 != null && !mMd5.isEmpty() &&
228225
mMd5.equalsIgnoreCase(EasyUtils.getFileMd5(EasyUtils.openFileInputStream(mFile)));
229226
} catch (FileNotFoundException e) {
230227
e.printStackTrace();

0 commit comments

Comments
 (0)