Skip to content

Commit 5cb32e9

Browse files
committed
新增 Gson 容错处理
新增 Lifecycle 管控 新增支持上传文件列表 优化日志打印策略 优化请求重试机制的逻辑 优化全局和局部参数的优先级 优化上传文件的 MIME 类型
1 parent df189b5 commit 5cb32e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1147
-422
lines changed

EasyHttp.apk

360 KB
Binary file not shown.

README.md

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 网络请求框架
22

3+
> 码云地址:[Gitee](https://gitee.com/getActivity/EasyHttp)
4+
35
![](EasyHttp.jpg)
46

57
[点击此处下载Demo](https://raw.githubusercontent.com/getActivity/EasyHttp/master/EasyHttp.apk)
@@ -15,7 +17,7 @@
1517
}
1618

1719
dependencies {
18-
implementation 'com.hjq:http:6.0'
20+
implementation 'com.hjq:http:6.5'
1921
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
2022
implementation 'com.google.code.gson:gson:2.8.5'
2123
}
@@ -29,10 +31,6 @@
2931
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3032
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
3133

32-
<!-- 外部存储读写权限 -->
33-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
34-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
35-
3634
#### 服务器配置
3735

3836
public class RequestServer implements IRequestServer {
@@ -71,7 +69,7 @@
7169
//.addParam("token", "6666666")
7270
// 添加全局请求头
7371
//.addHeader("time", "20191030")
74-
// 启用配置
72+
// 启用配置
7573
.into();
7674

7775
> 上述是创建配置,更新配置可以使用
@@ -81,7 +79,8 @@
8179

8280
#### 配置接口
8381

84-
public class LoginApi implements IRequestApi {
82+
@Keep
83+
public final class LoginApi implements IRequestApi {
8584

8685
@Override
8786
public String getApi() {
@@ -204,19 +203,6 @@
204203
-keep interface okhttp3.** { *; }
205204
-dontwarn okhttp3.**
206205
-dontwarn okio.**
207-
208-
# 保护 IRequestApi 类字段名不被混淆
209-
-keepclassmembernames class * implements com.hjq.http.config.IRequestApi {
210-
<fields>;
211-
}
212-
# 保护 Bean 类不被混淆(请注意修改包名路径)
213-
-keepclassmembernames class xxx.xxx.xxx.xxx.xxx.response.** {
214-
<fields>;
215-
}
216-
# 保护模型类的字段不被混淆(请注意修改包名路径)
217-
-keepclassmembernames class xxx.xxx.xxx.xxx.xxx.xxx.HttpData {
218-
<fields>;
219-
}
220206

221207
#### 作者的其他开源项目
222208

app/build.gradle

+14-8
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ android {
1313
applicationId "com.hjq.http.demo"
1414
minSdkVersion 14
1515
targetSdkVersion 28
16-
versionCode 60
17-
versionName "6.0"
18-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
versionCode 65
17+
versionName "6.5"
18+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1919
}
20+
2021
buildTypes {
2122
release {
22-
minifyEnabled false
23+
minifyEnabled true
2324
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2425
}
2526
debug {
@@ -33,13 +34,18 @@ dependencies {
3334
implementation fileTree(include: ['*.jar'], dir: 'libs')
3435
implementation project(':library')
3536

36-
implementation 'com.android.support:appcompat-v7:28.0.0'
37-
implementation 'com.android.support:design:28.0.0'
37+
testImplementation 'junit:junit:4.12'
38+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
39+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
40+
41+
42+
implementation 'androidx.appcompat:appcompat:1.1.0'
43+
implementation 'com.google.android.material:material:1.1.0'
3844

3945
// 标题栏:https://github.com/getActivity/TitleBar
4046
implementation 'com.hjq:titlebar:6.0'
4147
// 吐司工具类:https://github.com/getActivity/ToastUtils
42-
implementation 'com.hjq:toast:8.0'
48+
implementation 'com.hjq:toast:8.2'
4349
// 权限请求框架:https://github.com/getActivity/XXPermissions
4450
implementation 'com.hjq:xxpermissions:6.2'
4551
// Json 解析框架:https://github.com/google/gson
@@ -48,5 +54,5 @@ dependencies {
4854
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
4955
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
5056
// 日志调试:https://github.com/getActivity/Logcat
51-
debugImplementation 'com.hjq:logcat:6.0'
57+
debugImplementation 'com.hjq:logcat:6.5'
5258
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"listTest1" : [],
3+
"listTest2" : {},
4+
"listTest3" : "",
5+
"booleanTest1" : 0,
6+
"booleanTest2" : 1,
7+
"booleanTest3" : null,
8+
"booleanTest4" : "true",
9+
"stringTest1" : null,
10+
"stringTest2" : false,
11+
"stringTest3" : 123,
12+
"intTest1" : 2.2,
13+
"intTest2" : null,
14+
"intTest3" : "",
15+
"longTest1" : 2.2,
16+
"longTest2" : null,
17+
"longTest3" : "22"
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"listTest1" : [1, 2],
3+
"listTest2" : ["a", "b", "c"],
4+
"listTest3" : [1, 2, 3],
5+
"booleanTest1" : false,
6+
"booleanTest2" : true,
7+
"booleanTest3" : false,
8+
"booleanTest4" : true,
9+
"stringTest1" : null,
10+
"stringTest2" : "",
11+
"stringTest3" : "字符串",
12+
"intTest1" : 1,
13+
"intTest2" : 2,
14+
"intTest3" : 3,
15+
"longTest1" : 12580,
16+
"longTest2" : 10086,
17+
"longTest3" : 101000
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hjq.http.test;
2+
3+
import java.util.List;
4+
5+
public class JsonBean {
6+
7+
private List<String> listTest1;
8+
private List<String> listTest2;
9+
private List<Integer> listTest3;
10+
private boolean booleanTest1;
11+
private boolean booleanTest2;
12+
private boolean booleanTest3;
13+
private boolean booleanTest4;
14+
private String stringTest1;
15+
private String stringTest2;
16+
private String stringTest3;
17+
private int intTest1;
18+
private int intTest2;
19+
private int intTest3;
20+
private long longTest1;
21+
private long longTest2;
22+
private long longTest3;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.hjq.http.test;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
7+
import com.google.gson.Gson;
8+
import com.google.gson.GsonBuilder;
9+
import com.google.gson.internal.bind.TypeAdapters;
10+
import com.hjq.http.demo.http.json.BooleanTypeAdapter;
11+
import com.hjq.http.demo.http.json.DoubleTypeAdapter;
12+
import com.hjq.http.demo.http.json.FloatTypeAdapter;
13+
import com.hjq.http.demo.http.json.IntegerTypeAdapter;
14+
import com.hjq.http.demo.http.json.ListTypeAdapter;
15+
import com.hjq.http.demo.http.json.LongTypeAdapter;
16+
import com.hjq.http.demo.http.json.StringTypeAdapter;
17+
18+
import org.junit.After;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.List;
26+
27+
/**
28+
* Example local unit test, which will execute on the development machine (host).
29+
*
30+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
31+
*/
32+
public class JsonUnitTest {
33+
34+
private Gson mGson;
35+
36+
@Before
37+
public void onTestBefore() {
38+
mGson = new GsonBuilder()
39+
.registerTypeAdapterFactory(TypeAdapters.newFactory(String.class, new StringTypeAdapter()))
40+
.registerTypeAdapterFactory(TypeAdapters.newFactory(boolean.class, Boolean.class, new BooleanTypeAdapter()))
41+
.registerTypeAdapterFactory(TypeAdapters.newFactory(int.class, Integer.class, new IntegerTypeAdapter()))
42+
.registerTypeAdapterFactory(TypeAdapters.newFactory(long.class, Long.class, new LongTypeAdapter()))
43+
.registerTypeAdapterFactory(TypeAdapters.newFactory(float.class, Float.class, new FloatTypeAdapter()))
44+
.registerTypeAdapterFactory(TypeAdapters.newFactory(double.class, Double.class, new DoubleTypeAdapter()))
45+
.registerTypeHierarchyAdapter(List.class, new ListTypeAdapter())
46+
.create();
47+
}
48+
49+
@Test
50+
public void onSpecification() {
51+
Context context = InstrumentationRegistry.getInstrumentation().getContext();
52+
String json = getAssetsString(context, "Specification.json");
53+
mGson.fromJson(json, JsonBean.class);
54+
}
55+
56+
@Test
57+
public void onNoSpecification() {
58+
Context context = InstrumentationRegistry.getInstrumentation().getContext();
59+
String json = getAssetsString(context, "NoSpecification.json");
60+
mGson.fromJson(json, JsonBean.class);
61+
}
62+
63+
@After
64+
public void onTestAfter() {
65+
mGson = null;
66+
}
67+
68+
/**
69+
* 获取资产目录下面文件的字符串
70+
*/
71+
private static String getAssetsString(Context context, String file) {
72+
try {
73+
InputStream inputStream = context.getAssets().open(file);
74+
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
75+
byte[] buffer = new byte[512];
76+
int length;
77+
while ((length = inputStream.read(buffer)) != -1) {
78+
outStream.write(buffer, 0, length);
79+
}
80+
outStream.close();
81+
inputStream.close();
82+
return outStream.toString();
83+
} catch (IOException e) {
84+
e.printStackTrace();
85+
return null;
86+
}
87+
}
88+
}

app/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
<!-- 适配 Android 7.0 文件意图 -->
2929
<provider
30-
android:name="android.support.v4.content.FileProvider"
30+
android:name="androidx.core.content.FileProvider"
3131
android:authorities="${applicationId}.provider"
3232
android:exported="false"
3333
android:grantUriPermissions="true">

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.hjq.http.demo;
22

33
import android.app.ProgressDialog;
4-
import android.support.v7.app.AppCompatActivity;
54

6-
import com.hjq.http.EasyHttp;
5+
import androidx.appcompat.app.AppCompatActivity;
6+
77
import com.hjq.http.demo.http.model.HttpData;
88
import com.hjq.http.listener.OnHttpListener;
99
import com.hjq.toast.ToastUtils;
1010

1111
import okhttp3.Call;
1212

13+
/**
14+
* author : Android 轮子哥
15+
* github : https://github.com/getActivity/EasyHttp
16+
* time : 2019/05/19
17+
* desc : 基类封装
18+
*/
1319
public class BaseActivity extends AppCompatActivity implements OnHttpListener {
1420

1521
/** 加载对话框 */
@@ -75,10 +81,4 @@ public void onFail(Exception e) {
7581
public void onEnd(Call call) {
7682
hideDialog();
7783
}
78-
79-
@Override
80-
protected void onDestroy() {
81-
EasyHttp.cancel(this);
82-
super.onDestroy();
83-
}
8484
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
import android.os.Build;
1010
import android.os.Bundle;
1111
import android.os.Environment;
12-
import android.support.v4.content.ContextCompat;
13-
import android.support.v4.content.FileProvider;
1412
import android.view.View;
1513
import android.widget.ProgressBar;
1614

15+
import androidx.core.content.ContextCompat;
16+
import androidx.core.content.FileProvider;
17+
1718
import com.hjq.http.EasyHttp;
1819
import com.hjq.http.demo.http.model.HttpData;
1920
import com.hjq.http.demo.http.request.SearchAuthorApi;
@@ -57,7 +58,6 @@ protected void onCreate(Bundle savedInstanceState) {
5758
findViewById(R.id.btn_main_post).setOnClickListener(this);
5859
findViewById(R.id.btn_main_update).setOnClickListener(this);
5960
findViewById(R.id.btn_main_download).setOnClickListener(this);
60-
6161
requestPermission();
6262
}
6363

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public void onCreate() {
2828

2929
EasyConfig.with(new OkHttpClient())
3030
// 是否打印日志
31-
.setLogEnabled(BuildConfig.DEBUG)
31+
//.setLogEnabled(BuildConfig.DEBUG)
3232
// 设置服务器配置
3333
.setServer(server)
3434
// 设置请求处理策略
35-
.setHandler(new RequestHandler())
35+
.setHandler(new RequestHandler(this))
3636
// 设置请求重试次数
37-
.setRetryCount(3)
37+
.setRetryCount(1)
3838
// 添加全局请求参数
3939
//.addParam("token", "6666666")
4040
// 添加全局请求头
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.hjq.http.demo.http.json;
2+
3+
import com.google.gson.TypeAdapter;
4+
import com.google.gson.stream.JsonReader;
5+
import com.google.gson.stream.JsonWriter;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* author : Android 轮子哥
11+
* github : https://github.com/getActivity/EasyHttp
12+
* time : 2020/05/05
13+
* desc : boolean / Boolean 解析适配器 {@link com.google.gson.internal.bind.TypeAdapters#BOOLEAN}
14+
*/
15+
public class BooleanTypeAdapter extends TypeAdapter<Boolean> {
16+
17+
@Override
18+
public Boolean read(JsonReader in) throws IOException {
19+
switch (in.peek()) {
20+
case NULL:
21+
in.nextNull();
22+
return null;
23+
case STRING:
24+
// 如果后台返回 "true" 或者 "TRUE",则默认处理为 true
25+
return Boolean.parseBoolean(in.nextString());
26+
case NUMBER:
27+
// 如果这个后台返回是 1,则表示 true,否则表示 false
28+
return in.nextInt() == 1;
29+
default:
30+
return in.nextBoolean();
31+
}
32+
}
33+
@Override
34+
public void write(JsonWriter out, Boolean value) throws IOException {
35+
out.value(value);
36+
}
37+
}

0 commit comments

Comments
 (0)