Skip to content

Commit ff69bb1

Browse files
committed
升级 EasyHttp 版本到 13.5
升级 OkHttp 版本到 5.3.0
1 parent 040118c commit ff69bb1

19 files changed

Lines changed: 206 additions & 139 deletions

app/src/main/java/com/hjq/demo/http/exception/ResultException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
*/
1313
public final class ResultException extends HttpException {
1414

15+
@NonNull
1516
private final HttpData<?> mData;
1617

17-
public ResultException(String message, HttpData<?> data) {
18+
public ResultException(@NonNull String message, @NonNull HttpData<?> data) {
1819
super(message);
1920
mData = data;
2021
}
2122

22-
public ResultException(String message, Throwable cause, HttpData<?> data) {
23+
public ResultException(@NonNull String message, @NonNull Throwable cause, @NonNull HttpData<?> data) {
2324
super(message, cause);
2425
mData = data;
2526
}

app/src/main/java/com/hjq/demo/http/exception/TokenException.java

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

3+
import androidx.annotation.NonNull;
34
import com.hjq.http.exception.HttpException;
45

56
/**
@@ -10,11 +11,11 @@
1011
*/
1112
public final class TokenException extends HttpException {
1213

13-
public TokenException(String message) {
14+
public TokenException(@NonNull String message) {
1415
super(message);
1516
}
1617

17-
public TokenException(String message, Throwable cause) {
18+
public TokenException(@NonNull String message, @NonNull Throwable cause) {
1819
super(message, cause);
1920
}
2021
}

app/src/main/java/com/hjq/demo/http/glide/GlideConfig.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder
6767
@Override
6868
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
6969
// Glide 默认使用的是 HttpURLConnection 来做网络请求,这里切换成更高效的 OkHttp
70-
registry.replace(GlideUrl.class, InputStream.class,
71-
new OkHttpLoader.Factory(EasyConfig.getInstance().getClient()));
70+
registry.replace(GlideUrl.class, InputStream.class, new OkHttpLoader.Factory(EasyConfig.getInstance().getClient()));
7271
}
7372

7473
@Override

app/src/main/java/com/hjq/demo/http/glide/OkHttpLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
public final class OkHttpLoader implements ModelLoader<GlideUrl, InputStream> {
1919

20+
@NonNull
2021
private final Call.Factory mFactory;
2122

2223
private OkHttpLoader(@NonNull Call.Factory factory) {
@@ -35,6 +36,7 @@ public LoadData<InputStream> buildLoadData(@NonNull GlideUrl model, int width, i
3536

3637
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
3738

39+
@NonNull
3840
private final Call.Factory mFactory;
3941

4042
Factory(@NonNull Call.Factory factory) {
Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hjq.demo.http.model;
22

33
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
45
import com.hjq.gson.factory.GsonFactory;
56
import com.hjq.http.config.IRequestApi;
67
import com.hjq.http.request.HttpRequest;
@@ -14,74 +15,85 @@
1415
*/
1516
public final class HttpCacheManager {
1617

17-
private static final MMKV HTTP_CACHE_CONTENT = MMKV.mmkvWithID("http_cache_content");;
18+
@NonNull
19+
private static final MMKV HTTP_CACHE_CONTENT = MMKV.mmkvWithID("http_cache_content");;
1820

19-
private static final MMKV HTTP_CACHE_TIME = MMKV.mmkvWithID("http_cache_time");
21+
@NonNull
22+
private static final MMKV HTTP_CACHE_TIME = MMKV.mmkvWithID("http_cache_time");
2023

21-
/**
22-
* 生成缓存的 key
23-
*/
24-
public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
25-
IRequestApi requestApi = httpRequest.getRequestApi();
26-
return "请替换成当前的用户 id" + "\n" + requestApi.getApi() + "\n" + GsonFactory.getSingletonGson().toJson(requestApi);
27-
}
24+
/**
25+
* 生成缓存的 key
26+
*/
27+
@NonNull
28+
public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
29+
IRequestApi requestApi = httpRequest.getRequestApi();
30+
return "请替换成当前的用户 id" + "\n" + requestApi.getApi() + "\n" + GsonFactory.getSingletonGson().toJson(requestApi);
31+
}
2832

29-
/**
30-
* 读取缓存
31-
*/
32-
public static String readHttpCache(String cacheKey) {
33-
String cacheValue = HTTP_CACHE_CONTENT.getString(cacheKey, null);
34-
if ("".equals(cacheValue) || "{}".equals(cacheValue)) {
35-
return null;
36-
}
37-
return cacheValue;
38-
}
33+
/**
34+
* 读取缓存
35+
*/
36+
@Nullable
37+
public static String readHttpCache(@NonNull String cacheKey) {
38+
String cacheValue = HTTP_CACHE_CONTENT.getString(cacheKey, null);
39+
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
40+
return null;
41+
}
42+
return cacheValue;
43+
}
3944

40-
/**
41-
* 写入缓存
42-
*/
43-
public static boolean writeHttpCache(String cacheKey, String cacheValue) {
44-
return HTTP_CACHE_CONTENT.putString(cacheKey, cacheValue).commit();
45-
}
45+
/**
46+
* 写入缓存
47+
*/
48+
public static boolean writeHttpCache(@NonNull String cacheKey, @NonNull String cacheValue) {
49+
return HTTP_CACHE_CONTENT.putString(cacheKey, cacheValue).commit();
50+
}
4651

47-
/**
48-
* 清理缓存
49-
*/
50-
public static void clearCache() {
51-
HTTP_CACHE_CONTENT.clearMemoryCache();
52-
HTTP_CACHE_CONTENT.clearAll();
52+
/**
53+
* 删除缓存
54+
*/
55+
public static boolean deleteHttpCache(@NonNull String cacheKey) {
56+
return HTTP_CACHE_CONTENT.remove(cacheKey).commit();
57+
}
5358

54-
HTTP_CACHE_TIME.clearMemoryCache();
55-
HTTP_CACHE_TIME.clearAll();
56-
}
59+
/**
60+
* 清理缓存
61+
*/
62+
public static void clearCache() {
63+
HTTP_CACHE_CONTENT.clearMemoryCache();
64+
HTTP_CACHE_CONTENT.clearAll();
5765

58-
/**
59-
* 获取 Http 写入缓存的时间
60-
*/
61-
public static long getHttpCacheTime(String cacheKey) {
62-
return HTTP_CACHE_TIME.getLong(cacheKey, 0);
63-
}
66+
HTTP_CACHE_TIME.clearMemoryCache();
67+
HTTP_CACHE_TIME.clearAll();
68+
}
6469

65-
/**
66-
* 设置 Http 写入缓存的时间
67-
*/
68-
public static boolean setHttpCacheTime(String cacheKey, long cacheTime) {
69-
return HTTP_CACHE_TIME.putLong(cacheKey, cacheTime).commit();
70-
}
70+
/**
71+
* 获取 Http 写入缓存的时间
72+
*/
73+
public static long getHttpCacheTime(@NonNull String cacheKey) {
74+
return HTTP_CACHE_TIME.getLong(cacheKey, 0);
75+
}
7176

72-
/**
73-
* 判断缓存是否过期
74-
*/
75-
public static boolean isCacheInvalidate(String cacheKey, long maxCacheTime) {
76-
if (maxCacheTime == Long.MAX_VALUE) {
77-
// 表示缓存长期有效,永远不会过期
78-
return false;
79-
}
80-
long httpCacheTime = getHttpCacheTime(cacheKey);
81-
if (httpCacheTime == 0) {
82-
// 表示不知道缓存的时间,这里默认当做已经过期了
83-
return true;
84-
}
85-
return httpCacheTime + maxCacheTime < System.currentTimeMillis();
86-
}
77+
/**
78+
* 设置 Http 写入缓存的时间
79+
*/
80+
public static boolean setHttpCacheTime(@NonNull String cacheKey, long cacheTime) {
81+
return HTTP_CACHE_TIME.putLong(cacheKey, cacheTime).commit();
82+
}
83+
84+
/**
85+
* 判断缓存是否过期
86+
*/
87+
public static boolean isCacheInvalidate(@NonNull String cacheKey, long maxCacheTime) {
88+
if (maxCacheTime == Long.MAX_VALUE) {
89+
// 表示缓存长期有效,永远不会过期
90+
return false;
91+
}
92+
long httpCacheTime = getHttpCacheTime(cacheKey);
93+
if (httpCacheTime == 0) {
94+
// 表示不知道缓存的时间,这里默认当做已经过期了
95+
return true;
96+
}
97+
return httpCacheTime + maxCacheTime < System.currentTimeMillis();
98+
}
8799
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.hjq.demo.http.model;
2+
3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
import com.hjq.gson.factory.GsonFactory;
6+
import com.hjq.http.EasyLog;
7+
import com.hjq.http.config.IHttpCacheStrategy;
8+
import com.hjq.http.request.HttpRequest;
9+
import java.lang.reflect.Type;
10+
import okhttp3.Response;
11+
12+
/**
13+
* author : Android 轮子哥
14+
* github : https://github.com/getActivity/AndroidProject
15+
* time : 2025/03/23
16+
* desc : 请求缓存策略实现类
17+
*/
18+
public final class HttpCacheStrategy implements IHttpCacheStrategy {
19+
20+
@Nullable
21+
@Override
22+
public Object readCache(@NonNull HttpRequest<?> httpRequest, @NonNull Type type, long cacheTime) {
23+
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
24+
String cacheValue = HttpCacheManager.readHttpCache(cacheKey);
25+
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
26+
return null;
27+
}
28+
EasyLog.printLog(httpRequest, "----- read cache key -----");
29+
EasyLog.printJson(httpRequest, cacheKey);
30+
EasyLog.printLog(httpRequest, "----- read cache value -----");
31+
EasyLog.printJson(httpRequest, cacheValue);
32+
EasyLog.printLog(httpRequest, "cacheTime = " + cacheTime);
33+
boolean cacheInvalidate = HttpCacheManager.isCacheInvalidate(cacheKey, cacheTime);
34+
EasyLog.printLog(httpRequest, "cacheInvalidate = " + cacheInvalidate);
35+
if (cacheInvalidate) {
36+
// 表示缓存已经过期了,直接返回 null 给外层,表示缓存不可用
37+
return null;
38+
}
39+
return GsonFactory.getSingletonGson().fromJson(cacheValue, type);
40+
}
41+
42+
@Override
43+
public boolean writeCache(@NonNull HttpRequest<?> httpRequest, @NonNull Response response, @NonNull Object result) {
44+
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
45+
String cacheValue = GsonFactory.getSingletonGson().toJson(result);
46+
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
47+
return false;
48+
}
49+
EasyLog.printLog(httpRequest, "----- write cache key -----");
50+
EasyLog.printJson(httpRequest, cacheKey);
51+
EasyLog.printLog(httpRequest, "----- write cache value -----");
52+
EasyLog.printJson(httpRequest, cacheValue);
53+
boolean writeHttpCacheResult = HttpCacheManager.writeHttpCache(cacheKey, cacheValue);
54+
EasyLog.printLog(httpRequest, "writeHttpCacheResult = " + writeHttpCacheResult);
55+
boolean refreshHttpCacheTimeResult = HttpCacheManager.setHttpCacheTime(cacheKey, System.currentTimeMillis());
56+
EasyLog.printLog(httpRequest, "refreshHttpCacheTimeResult = " + refreshHttpCacheTimeResult);
57+
return writeHttpCacheResult && refreshHttpCacheTimeResult;
58+
}
59+
60+
@Override
61+
public boolean deleteCache(@NonNull HttpRequest<?> httpRequest) {
62+
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
63+
EasyLog.printLog(httpRequest, "----- delete cache key -----");
64+
EasyLog.printJson(httpRequest, cacheKey);
65+
boolean deleteHttpCacheResult = HttpCacheManager.deleteHttpCache(cacheKey);
66+
EasyLog.printLog(httpRequest, "deleteHttpCacheResult = " + deleteHttpCacheResult);
67+
return deleteHttpCacheResult;
68+
}
69+
70+
@Override
71+
public void clearCache() {
72+
HttpCacheManager.clearCache();
73+
}
74+
}

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

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

3+
import androidx.annotation.NonNull;
34
import androidx.annotation.Nullable;
45
import java.util.Map;
56

@@ -17,8 +18,11 @@ public class HttpData<T> {
1718

1819
/** 返回码 */
1920
private int code;
21+
2022
/** 提示语 */
23+
@Nullable
2124
private String msg;
25+
2226
/** 数据 */
2327
@Nullable
2428
private T data;
@@ -36,7 +40,11 @@ public int getCode() {
3640
return code;
3741
}
3842

43+
@NonNull
3944
public String getMessage() {
45+
if (msg == null) {
46+
return "";
47+
}
4048
return msg;
4149
}
4250

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

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

3+
import androidx.annotation.Nullable;
34
import java.util.List;
45

56
/**
@@ -19,17 +20,21 @@ public static class ListBean<T> {
1920
/** 总数量 */
2021
private int totalNumber;
2122
/** 数据 */
23+
@Nullable
2224
private List<T> items;
2325

2426
/**
2527
* 判断是否是最后一页
2628
*/
2729
public boolean isLastPage() {
30+
if (items == null) {
31+
return true;
32+
}
2833
if (pageSize == 0) {
2934
// 避免出现除零异常
3035
return true;
3136
}
32-
return Math.ceil((float) totalNumber / (float) pageSize) <= pageIndex;
37+
return Math.ceil((float) totalNumber / pageSize) <= pageIndex;
3338
}
3439

3540
public int getTotalNumber() {
@@ -44,6 +49,7 @@ public int getPageSize() {
4449
return pageSize;
4550
}
4651

52+
@Nullable
4753
public List<T> getItems() {
4854
return items;
4955
}

0 commit comments

Comments
 (0)