Skip to content

Commit af97154

Browse files
committed
优化框架类名命名、变量命名、包结构
优化 getGenericType 方法中的代码实现 优化默认日志打印策略日志分割打印的长度 新增 EasyHttp 请求缓存实现代码示例
1 parent fbf341a commit af97154

21 files changed

+240
-163
lines changed

HelpDoc.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128
#### Http 明文请求
129129

130130
* **Android 9.0** 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。
131-
如果当前应用的请求是 http 请求,而非 https ,这样就会导系统禁止当前应用进行该请求,如果 WebView 的 url 用 http 协议,同样会出现加载失败,https 不受影响
131+
132+
* 如果当前应用的请求是 http 请求,而非 https,这样就会导系统禁止当前应用进行该请求,如果 WebView 的 url 用 http 协议,同样会出现加载失败,https 不受影响
132133

133134
* 在 res 下新建一个 xml 目录,然后创建一个名为:`network_security_config.xml` 文件 ,该文件内容如下
134135

@@ -139,7 +140,7 @@
139140
</network-security-config>
140141
```
141142

142-
* 然后在 AndroidManifest.xml application 标签内应用上面的xml配置
143+
* 然后在 AndroidManifest.xml application 标签内应用上面的 xml 配置
143144

144145
```xml
145146
<application
@@ -1136,7 +1137,7 @@ EasyConfig.getInstance().setLogEnabled(false);
11361137

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

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

11411142
```java
11421143
EasyConfig.with(okHttpClient)

README.md

Lines changed: 16 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.0/EasyHttp.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/12.2/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:12.0'
64+
implementation 'com.github.getActivity:EasyHttp:12.2'
6565
// OkHttp 框架:https://github.com/square/okhttp
6666
// noinspection GradleDependency
6767
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
@@ -70,13 +70,21 @@ dependencies {
7070

7171
* 需要注意的是:由于使用了 Lifecycle 特性,目前框架只支持 AndroidX 的项目集成
7272

73+
#### 框架混淆规则
74+
75+
* 在混淆规则文件 `proguard-rules.pro` 中加入
76+
77+
```text
78+
-keep class com.hjq.http.** {*;}
79+
```
80+
7381
## [框架的具体用法请点击这里查看](HelpDoc.md)
7482

7583
### 不同网络请求框架之间的对比
7684

7785
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
7886
| :----: | :------: | :-----: | :-----: |
79-
| 对应版本 | 12.0 | 2.9.0 | 3.0.4 |
87+
| 对应版本 | 12.2 | 2.9.0 | 3.0.4 |
8088
| 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) |
8189
| **aar 包大小** | 90 KB | 123 KB | 131 KB |
8290
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
@@ -229,14 +237,18 @@ EasyHttp.post(this)
229237

230238
* 悬浮窗框架:[EasyWindow](https://github.com/getActivity/EasyWindow) ![](https://img.shields.io/github/stars/getActivity/EasyWindow.svg) ![](https://img.shields.io/github/forks/getActivity/EasyWindow.svg)
231239

232-
* Shape 框架:[ShapeView](https://github.com/getActivity/ShapeView) ![](https://img.shields.io/github/stars/getActivity/ShapeView.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)
240+
* ShapeView 框架:[ShapeView](https://github.com/getActivity/ShapeView) ![](https://img.shields.io/github/stars/getActivity/ShapeView.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)
241+
242+
* ShapeDrawable 框架:[ShapeDrawable](https://github.com/getActivity/ShapeDrawable) ![](https://img.shields.io/github/stars/getActivity/ShapeDrawable.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeDrawable.svg)
233243

234244
* 语种切换框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages) ![](https://img.shields.io/github/stars/getActivity/MultiLanguages.svg) ![](https://img.shields.io/github/forks/getActivity/MultiLanguages.svg)
235245

236246
* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory) ![](https://img.shields.io/github/stars/getActivity/GsonFactory.svg) ![](https://img.shields.io/github/forks/getActivity/GsonFactory.svg)
237247

238248
* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat) ![](https://img.shields.io/github/stars/getActivity/Logcat.svg) ![](https://img.shields.io/github/forks/getActivity/Logcat.svg)
239249

250+
* 嵌套滚动布局框架:[NestedScrollLayout](https://github.com/getActivity/NestedScrollLayout) ![](https://img.shields.io/github/stars/getActivity/NestedScrollLayout.svg) ![](https://img.shields.io/github/forks/getActivity/NestedScrollLayout.svg)
251+
240252
* Android 版本适配:[AndroidVersionAdapter](https://github.com/getActivity/AndroidVersionAdapter) ![](https://img.shields.io/github/stars/getActivity/AndroidVersionAdapter.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidVersionAdapter.svg)
241253

242254
* Android 代码规范:[AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard) ![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)

app/build.gradle

Lines changed: 7 additions & 7 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 1200
18-
versionName '12.0'
17+
versionCode 1202
18+
versionName '12.2'
1919
}
2020

2121
// 支持 JDK 1.8
@@ -73,28 +73,28 @@ dependencies {
7373
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
7474

7575
// 吐司框架:https://github.com/getActivity/Toaster
76-
implementation 'com.github.getActivity:Toaster:12.2'
76+
implementation 'com.github.getActivity:Toaster:12.3'
7777

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

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

8484
// Json 解析框架:https://github.com/google/gson
8585
implementation 'com.google.code.gson:gson:2.10.1'
8686
// Gson 解析容错:https://github.com/getActivity/GsonFactory
87-
implementation 'com.github.getActivity:GsonFactory:6.5'
87+
implementation 'com.github.getActivity:GsonFactory:8.0'
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.2'
93+
debugImplementation 'com.github.getActivity:Logcat:11.8'
9494

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

9898
// 内存泄漏监测框架:https://github.com/square/leakcanary
99-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
99+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'
100100
}

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
<category android:name="android.intent.category.LAUNCHER" />
7272
</intent-filter>
7373
</activity>
74-
7574
</application>
7675

7776
<!-- Android 11 软件包可见性适配 -->

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
import android.os.Environment;
1313
import android.view.View;
1414
import android.widget.ProgressBar;
15-
1615
import androidx.annotation.NonNull;
1716
import androidx.core.content.ContextCompat;
1817
import androidx.core.content.FileProvider;
19-
2018
import com.hjq.bar.OnTitleBarListener;
2119
import com.hjq.bar.TitleBar;
2220
import com.hjq.easy.demo.http.api.SearchAuthorApi;
@@ -36,13 +34,11 @@
3634
import com.hjq.permissions.Permission;
3735
import com.hjq.permissions.XXPermissions;
3836
import com.hjq.toast.Toaster;
39-
4037
import java.io.File;
4138
import java.io.IOException;
4239
import java.io.OutputStream;
4340
import java.util.ArrayList;
4441
import java.util.List;
45-
4642
import okhttp3.Call;
4743

4844
/**
@@ -133,7 +129,7 @@ public void onHttpSuccess(HttpData<List<SearchAuthorApi.Bean>> result) {
133129

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

136-
EasyHttp.post(MainActivity.this)
132+
EasyHttp.post(this)
137133
.api(new SearchBlogsApi()
138134
.setKeyword("搬砖不再有"))
139135
.request(new HttpCallbackProxy<HttpData<SearchBlogsApi.Bean>>(MainActivity.this) {

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

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,74 @@
1515
*/
1616
public final class HttpCacheManager {
1717

18-
private volatile static MMKV sMmkv;
18+
private static final MMKV HTTP_CACHE_CONTENT = MMKV.mmkvWithID("http_cache_content");;
19+
20+
private static final MMKV HTTP_CACHE_TIME = MMKV.mmkvWithID("http_cache_time");
21+
22+
/**
23+
* 生成缓存的 key
24+
*/
25+
public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
26+
IRequestApi requestApi = httpRequest.getRequestApi();
27+
return "请替换成当前的用户 id" + "\n" + requestApi.getApi() + "\n" + GsonFactory.getSingletonGson().toJson(requestApi);
28+
}
1929

2030
/**
21-
* 获取单例的 MMKV 实例
31+
* 读取缓存
2232
*/
23-
public static MMKV getMmkv() {
24-
if(sMmkv == null) {
25-
synchronized (RequestHandler.class) {
26-
if (sMmkv == null) {
27-
sMmkv = MMKV.mmkvWithID("http_cache_id");
28-
}
29-
}
33+
public static String readHttpCache(String cacheKey) {
34+
String cacheValue = HTTP_CACHE_CONTENT.getString(cacheKey, null);
35+
if ("".equals(cacheValue) || "{}".equals(cacheValue)) {
36+
return null;
3037
}
31-
return sMmkv;
38+
return cacheValue;
3239
}
3340

3441
/**
35-
* 生成缓存的 key
42+
* 写入缓存
3643
*/
37-
public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
38-
IRequestApi requestApi = httpRequest.getRequestApi();
39-
return "用户 id" + "\n" + requestApi.getApi() + "\n" + GsonFactory.getSingletonGson().toJson(requestApi);
44+
public static boolean writeHttpCache(String cacheKey, String cacheValue) {
45+
return HTTP_CACHE_CONTENT.putString(cacheKey, cacheValue).commit();
46+
}
47+
48+
/**
49+
* 清理缓存
50+
*/
51+
public static void clearCache() {
52+
HTTP_CACHE_CONTENT.clearMemoryCache();
53+
HTTP_CACHE_CONTENT.clearAll();
54+
55+
HTTP_CACHE_TIME.clearMemoryCache();
56+
HTTP_CACHE_TIME.clearAll();
57+
}
58+
59+
/**
60+
* 获取 Http 写入缓存的时间
61+
*/
62+
public static long getHttpCacheTime(String cacheKey) {
63+
return HTTP_CACHE_TIME.getLong(cacheKey, 0);
64+
}
65+
66+
/**
67+
* 设置 Http 写入缓存的时间
68+
*/
69+
public static boolean setHttpCacheTime(String cacheKey, long cacheTime) {
70+
return HTTP_CACHE_TIME.putLong(cacheKey, cacheTime).commit();
71+
}
72+
73+
/**
74+
* 判断缓存是否过期
75+
*/
76+
public static boolean isCacheInvalidate(String cacheKey, long maxCacheTime) {
77+
if (maxCacheTime == Long.MAX_VALUE) {
78+
// 表示缓存长期有效,永远不会过期
79+
return false;
80+
}
81+
long httpCacheTime = getHttpCacheTime(cacheKey);
82+
if (httpCacheTime == 0) {
83+
// 表示不知道缓存的时间,这里默认当做已经过期了
84+
return true;
85+
}
86+
return httpCacheTime + maxCacheTime < System.currentTimeMillis();
4087
}
4188
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,21 @@ public Exception downloadFail(@NonNull HttpRequest<?> httpRequest, @NonNull Exce
200200
@Override
201201
public Object readCache(@NonNull HttpRequest<?> httpRequest, @NonNull Type type, long cacheTime) {
202202
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
203-
String cacheValue = HttpCacheManager.getMmkv().getString(cacheKey, null);
203+
String cacheValue = HttpCacheManager.readHttpCache(cacheKey);
204204
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
205205
return null;
206206
}
207-
EasyLog.printLog(httpRequest, "----- readCache cacheKey -----");
207+
EasyLog.printLog(httpRequest, "----- read cache key -----");
208208
EasyLog.printJson(httpRequest, cacheKey);
209-
EasyLog.printLog(httpRequest, "----- readCache cacheValue -----");
209+
EasyLog.printLog(httpRequest, "----- read cache value -----");
210210
EasyLog.printJson(httpRequest, cacheValue);
211+
EasyLog.printLog(httpRequest, "cacheTime = " + cacheTime);
212+
boolean cacheInvalidate = HttpCacheManager.isCacheInvalidate(cacheKey, cacheTime);
213+
EasyLog.printLog(httpRequest, "cacheInvalidate = " + cacheInvalidate);
214+
if (cacheInvalidate) {
215+
// 表示缓存已经过期了,直接返回 null 给外层,表示缓存不可用
216+
return null;
217+
}
211218
return GsonFactory.getSingletonGson().fromJson(cacheValue, type);
212219
}
213220

@@ -218,16 +225,19 @@ public boolean writeCache(@NonNull HttpRequest<?> httpRequest, @NonNull Response
218225
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
219226
return false;
220227
}
221-
EasyLog.printLog(httpRequest, "----- writeCache cacheKey -----");
228+
EasyLog.printLog(httpRequest, "----- write cache key -----");
222229
EasyLog.printJson(httpRequest, cacheKey);
223-
EasyLog.printLog(httpRequest, "----- writeCache cacheValue -----");
230+
EasyLog.printLog(httpRequest, "----- write cache value -----");
224231
EasyLog.printJson(httpRequest, cacheValue);
225-
return HttpCacheManager.getMmkv().putString(cacheKey, cacheValue).commit();
232+
boolean writeHttpCacheResult = HttpCacheManager.writeHttpCache(cacheKey, cacheValue);
233+
EasyLog.printLog(httpRequest, "writeHttpCacheResult = " + writeHttpCacheResult);
234+
boolean refreshHttpCacheTimeResult = HttpCacheManager.setHttpCacheTime(cacheKey, System.currentTimeMillis());
235+
EasyLog.printLog(httpRequest, "refreshHttpCacheTimeResult = " + refreshHttpCacheTimeResult);
236+
return writeHttpCacheResult && refreshHttpCacheTimeResult;
226237
}
227238

228239
@Override
229240
public void clearCache() {
230-
HttpCacheManager.getMmkv().clearMemoryCache();
231-
HttpCacheManager.getMmkv().clearAll();
241+
HttpCacheManager.clearCache();
232242
}
233243
}

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 1200
9-
versionName "12.0"
8+
versionCode 1202
9+
versionName "12.2"
1010
}
1111

1212
// 使用 JDK 1.8

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.hjq.http;
22

3-
import com.hjq.http.config.DefaultHttpLogStrategy;
4-
import com.hjq.http.config.IHttpLogStrategy;
53
import com.hjq.http.config.IRequestHandler;
64
import com.hjq.http.config.IRequestInterceptor;
5+
import com.hjq.http.config.IRequestLogStrategy;
76
import com.hjq.http.config.IRequestServer;
8-
import com.hjq.http.config.RequestServer;
7+
import com.hjq.http.config.impl.EasyHttpLogStrategy;
8+
import com.hjq.http.config.impl.EasyRequestServer;
99
import com.hjq.http.model.ThreadSchedulers;
10-
1110
import java.net.MalformedURLException;
1211
import java.net.URL;
1312
import java.util.HashMap;
14-
1513
import okhttp3.OkHttpClient;
1614

1715
/**
@@ -48,7 +46,7 @@ public static EasyConfig with(OkHttpClient client) {
4846
/** 请求拦截器 */
4947
private IRequestInterceptor mInterceptor;
5048
/** 日志打印策略 */
51-
private IHttpLogStrategy mLogStrategy;
49+
private IRequestLogStrategy mLogStrategy;
5250

5351
/** OkHttp 客户端 */
5452
private OkHttpClient mClient;
@@ -78,7 +76,7 @@ private EasyConfig(OkHttpClient client) {
7876
}
7977

8078
public EasyConfig setServer(String host) {
81-
return setServer(new RequestServer(host));
79+
return setServer(new EasyRequestServer(host));
8280
}
8381

8482
public EasyConfig setServer(IRequestServer server) {
@@ -157,7 +155,7 @@ public EasyConfig setThreadSchedulers(ThreadSchedulers schedulers) {
157155
return this;
158156
}
159157

160-
public EasyConfig setLogStrategy(IHttpLogStrategy strategy) {
158+
public EasyConfig setLogStrategy(IRequestLogStrategy strategy) {
161159
mLogStrategy = strategy;
162160
return this;
163161
}
@@ -218,7 +216,7 @@ public ThreadSchedulers getThreadSchedulers() {
218216
return mThreadSchedulers;
219217
}
220218

221-
public IHttpLogStrategy getLogStrategy() {
219+
public IRequestLogStrategy getLogStrategy() {
222220
return mLogStrategy;
223221
}
224222

@@ -260,7 +258,7 @@ public void into() {
260258
}
261259

262260
if (mLogStrategy == null) {
263-
mLogStrategy = new DefaultHttpLogStrategy();
261+
mLogStrategy = new EasyHttpLogStrategy();
264262
}
265263
EasyConfig.setInstance(this);
266264
}

0 commit comments

Comments
 (0)