Skip to content

Commit

Permalink
修改网络请求回调接口的参数类型
Browse files Browse the repository at this point in the history
新增框架内部删除接口缓存的逻辑
优化框架内部读写接口缓存的逻辑
优化框架内部 API 命名及代码写法
修复同步请求没有回调 interceptResponse 方法的问题
修复同步请求在设置优先使用缓存的情况下解析泛型异常的问题
  • Loading branch information
getActivity committed Nov 1, 2024
1 parent d49f3c5 commit 0e113c3
Show file tree
Hide file tree
Showing 22 changed files with 283 additions and 167 deletions.
230 changes: 154 additions & 76 deletions HelpDoc.md

Large diffs are not rendered by default.

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

![](picture/demo_code.png)

Expand Down Expand Up @@ -63,7 +63,7 @@ android {
dependencies {
// 网络请求框架:https://github.com/getActivity/EasyHttp
implementation 'com.github.getActivity:EasyHttp:12.8'
implementation 'com.github.getActivity:EasyHttp:13.0'
// OkHttp 框架:https://github.com/square/okhttp
// noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
Expand Down Expand Up @@ -100,6 +100,10 @@ dependencies {
-keep public class * implements com.hjq.http.listener.OnHttpListener {
*;
}
-keep public class * extends com.hjq.http.model.ResponseClass {
*;
}
```

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

| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
| :----: | :------: | :-----: | :-----: |
| 对应版本 | 12.8 | 2.9.0 | 3.0.4 |
| 对应版本 | 13.0 | 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 包大小** | 95 KB | 123 KB | 131 KB |
| **aar 包大小** | 96 KB | 123 KB | 131 KB |
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
| 配置多域名 ||||
| **动态 Host** ||||
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId 'com.hjq.easy.demo'
minSdkVersion 21
targetSdkVersion 31
versionCode 1208
versionName '12.8'
versionCode 1300
versionName '13.0'
}

// 支持 JDK 1.8
Expand Down Expand Up @@ -76,13 +76,13 @@ dependencies {
implementation 'com.github.getActivity:Toaster:12.6'

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

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

// Gson 解析容错:https://github.com/getActivity/GsonFactory
implementation 'com.github.getActivity:GsonFactory:9.5'
implementation 'com.github.getActivity:GsonFactory:9.6'
// Json 解析框架:https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.10.1'
// Kotlin 反射库:用于反射 Kotlin data class 类对象
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
android:networkSecurityConfig="@xml/network_security_config"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="LockedOrientationActivity,UnusedAttribute"
tools:targetApi="n">
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/hjq/easy/demo/BaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.hjq.easy.demo;

import android.app.ProgressDialog;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.Toaster;
import okhttp3.Call;

/**
* author : Android 轮子哥
Expand Down Expand Up @@ -57,20 +58,20 @@ public void hideDialog() {
}

@Override
public void onHttpStart(Call call) {
public void onHttpStart(@NonNull IRequestApi api) {
showDialog();
}

@Override
public void onHttpSuccess(Object result) {}
public void onHttpSuccess(@NonNull Object result) {}

@Override
public void onHttpFail(Throwable throwable) {
public void onHttpFail(@NonNull Throwable throwable) {
Toaster.show(throwable.getMessage());
}

@Override
public void onHttpEnd(Call call) {
public void onHttpEnd(@NonNull IRequestApi api) {
hideDialog();
}
}
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 @@ -23,6 +23,7 @@
import com.hjq.easy.demo.http.model.HttpData;
import com.hjq.http.EasyHttp;
import com.hjq.http.EasyUtils;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.http.listener.OnDownloadListener;
import com.hjq.http.listener.OnUpdateListener;
Expand All @@ -38,7 +39,6 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;

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

@Override
public void onHttpSuccess(HttpData<List<SearchAuthorApi.Bean>> result) {
public void onHttpSuccess(@NonNull HttpData<List<SearchAuthorApi.Bean>> result) {
Toaster.show("Get 请求成功,请看日志");
}
});

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

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

@Override
public void onHttpSuccess(HttpData<SearchBlogsApi.Bean> result) {
public void onHttpSuccess(@NonNull HttpData<SearchBlogsApi.Bean> result) {
Toaster.show("Post 请求成功,请看日志");
}
});
Expand Down Expand Up @@ -204,7 +204,7 @@ public void onHttpSuccess(HttpData<SearchBlogsApi.Bean> result) {
.request(new OnUpdateListener<Void>() {

@Override
public void onUpdateStart(Call call) {
public void onUpdateStart(@NonNull IRequestApi api) {
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.VISIBLE);
}
Expand All @@ -215,17 +215,17 @@ public void onUpdateProgressChange(int progress) {
}

@Override
public void onUpdateSuccess(Void result) {
public void onUpdateSuccess(@NonNull Void result) {
Toaster.show("上传成功");
}

@Override
public void onUpdateFail(Throwable throwable) {
public void onUpdateFail(@NonNull Throwable throwable) {
Toaster.show("上传失败");
}

@Override
public void onUpdateEnd(Call call) {
public void onUpdateEnd(@NonNull IRequestApi api) {
mProgressBar.setVisibility(View.GONE);
}
});
Expand Down Expand Up @@ -273,30 +273,30 @@ public void onUpdateEnd(Call call) {
.listener(new OnDownloadListener() {

@Override
public void onDownloadStart(File file) {
public void onDownloadStart(@NonNull File file) {
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.VISIBLE);
}

@Override
public void onDownloadProgressChange(File file, int progress) {
public void onDownloadProgressChange(@NonNull File file, int progress) {
mProgressBar.setProgress(progress);
}

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

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

@Override
public void onDownloadEnd(File file) {
public void onDownloadEnd(@NonNull File file) {
mProgressBar.setVisibility(View.GONE);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SearchAuthorApi setId(int id) {
return this;
}

public final static class Bean {
public static final class Bean {

private int courseId;
private int id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hjq.easy.demo.http.model;

import androidx.annotation.NonNull;

import com.hjq.gson.factory.GsonFactory;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.request.HttpRequest;
Expand All @@ -22,6 +21,7 @@ public final class HttpCacheManager {
/**
* 生成缓存的 key
*/
@NonNull
public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
IRequestApi requestApi = httpRequest.getRequestApi();
return "请替换成当前的用户 id" + "\n" + requestApi.getApi() + "\n" + GsonFactory.getSingletonGson().toJson(requestApi);
Expand All @@ -30,9 +30,9 @@ public static String generateCacheKey(@NonNull HttpRequest<?> httpRequest) {
/**
* 读取缓存
*/
public static String readHttpCache(String cacheKey) {
public static String readHttpCache(@NonNull String cacheKey) {
String cacheValue = HTTP_CACHE_CONTENT.getString(cacheKey, null);
if ("".equals(cacheValue) || "{}".equals(cacheValue)) {
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
return null;
}
return cacheValue;
Expand All @@ -45,6 +45,13 @@ public static boolean writeHttpCache(String cacheKey, String cacheValue) {
return HTTP_CACHE_CONTENT.putString(cacheKey, cacheValue).commit();
}

/**
* 删除缓存
*/
public static boolean deleteHttpCache(String cacheKey) {
return HTTP_CACHE_CONTENT.remove(cacheKey).commit();
}

/**
* 清理缓存
*/
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/hjq/easy/demo/http/model/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public Throwable downloadFail(@NonNull HttpRequest<?> httpRequest, @NonNull Thro
public Object readCache(@NonNull HttpRequest<?> httpRequest, @NonNull Type type, long cacheTime) {
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
String cacheValue = HttpCacheManager.readHttpCache(cacheKey);
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
return null;
}
EasyLog.printLog(httpRequest, "----- read cache key -----");
Expand All @@ -226,7 +226,7 @@ public Object readCache(@NonNull HttpRequest<?> httpRequest, @NonNull Type type,
public boolean writeCache(@NonNull HttpRequest<?> httpRequest, @NonNull Response response, @NonNull Object result) {
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
String cacheValue = GsonFactory.getSingletonGson().toJson(result);
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
if (cacheValue == null || cacheValue.isEmpty() || "{}".equals(cacheValue)) {
return false;
}
EasyLog.printLog(httpRequest, "----- write cache key -----");
Expand All @@ -240,6 +240,16 @@ public boolean writeCache(@NonNull HttpRequest<?> httpRequest, @NonNull Response
return writeHttpCacheResult && refreshHttpCacheTimeResult;
}

@Override
public boolean deleteCache(@NonNull HttpRequest<?> httpRequest) {
String cacheKey = HttpCacheManager.generateCacheKey(httpRequest);
EasyLog.printLog(httpRequest, "----- delete cache key -----");
EasyLog.printJson(httpRequest, cacheKey);
boolean deleteHttpCacheResult = HttpCacheManager.deleteHttpCache(cacheKey);
EasyLog.printLog(httpRequest, "deleteHttpCacheResult = " + deleteHttpCacheResult);
return deleteHttpCacheResult;
}

@Override
public void clearCache() {
HttpCacheManager.clearCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ContentResolverUriStore {
// 使用字符串作为缓存键
String cacheKey = "ContentResolver insert: " + "Uri = " + url + ", ContentValues = " + convertContentValuesToString(values);
String oldUriString = sharedPreferences.getString(cacheKey, "");
if (oldUriString != null && !"".equals(oldUriString)) {
if (oldUriString != null && !oldUriString.isEmpty()) {
Cursor cursor = null;
try {
Uri oldUri = Uri.parse(oldUriString);
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 16
versionCode 1208
versionName "12.8"
versionCode 1300
versionName "13.0"
}

// 使用 JDK 1.8
Expand Down
5 changes: 0 additions & 5 deletions library/src/main/java/com/hjq/http/callback/BaseCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public BaseCallback(@NonNull HttpRequest<?> request) {
() -> HttpLifecycleManager.register(mHttpRequest.getLifecycleOwner()));
}

public BaseCallback setCallProxy(CallProxy callProxy) {
mCallProxy = callProxy;
return this;
}

public BaseCallback setCallProxyFactory(CallProxy.Factory factory) {
mCallProxyFactory = factory;
return this;
Expand Down
17 changes: 7 additions & 10 deletions library/src/main/java/com/hjq/http/callback/DownloadCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public final class DownloadCallback extends BaseCallback {
private static final String FILE_MD5_REGEX = "^[\\w]{32}$";

/** 保存的文件 */
private File mFile;
@NonNull
private final File mFile;

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

public DownloadCallback(@NonNull HttpRequest<?> request) {
public DownloadCallback(@NonNull HttpRequest<?> request, @NonNull File file) {
super(request);
mHttpRequest = request;
}

public DownloadCallback setFile(File file) {
mFile = file;
return this;
}

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

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

public boolean verifyFileMd5() {
try {
return mFile.isFile() && mMd5 != null && !"".equals(mMd5) &&
return mFile.isFile() && mMd5 != null && !mMd5.isEmpty() &&
mMd5.equalsIgnoreCase(EasyUtils.getFileMd5(EasyUtils.openFileInputStream(mFile)));
} catch (FileNotFoundException e) {
e.printStackTrace();
Expand Down
Loading

0 comments on commit 0e113c3

Please sign in to comment.