Skip to content

Commit

Permalink
新增支持设置请求重试延迟的时间
Browse files Browse the repository at this point in the history
优化 String 空字符串的解析逻辑
优化原生 JSONObject 的解析逻辑
优化 Demo 中 Json 容错的解析逻辑
  • Loading branch information
getActivity committed Aug 2, 2020
1 parent 5c57fbd commit 44c6ca9
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 115 deletions.
Binary file modified EasyHttp.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class XxxServer implements IRequestServer {
}
```

* 当然也支持对某个接口进行单独配置,和上面的雷同,这里略过
* 当然也支持对某个接口进行单独配置,和上个问题雷同,这里略过

* 表单和 Json 方式提交的优缺点对比

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

dependencies {
implementation 'com.hjq:http:6.9'
implementation 'com.hjq:http:8.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
implementation 'com.google.code.gson:gson:2.8.5'
}
Expand Down Expand Up @@ -119,6 +119,7 @@

* implements IRequestType:实现这个接口之后可以重新指定这个请求的提交方式

* 具体用法可以[点击这里查看](HelpDoc.md)

#### 发起请求

Expand Down Expand Up @@ -213,7 +214,7 @@

| 功能 | Retrofit 框架 | EasyHttp 框架 |
| :----: | :------: | :-----: |
| 动态 URL | 不支持 | 支持 |
| 动态 Host | 不支持 | 支持 |
| 全局参数 | 不支持 | 支持 |
| 超时重试 | 不支持 | 支持 |
| 极速下载 | 不支持 | 支持 |
Expand All @@ -222,8 +223,6 @@
| 上传文件 | RequestBody | File / InputStream |
| 请求管理 | 需要封装 | 自动管控 |

#### 具体用法可以[点击这里查看](HelpDoc.md)

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

* 架构工程:[AndroidProject](https://github.com/getActivity/AndroidProject)
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "com.hjq.http.demo"
minSdkVersion 14
targetSdkVersion 28
versionCode 69
versionName "6.9"
versionCode 80
versionName "8.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -34,7 +34,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')

testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

Expand All @@ -45,7 +45,7 @@ dependencies {
// 标题栏:https://github.com/getActivity/TitleBar
implementation 'com.hjq:titlebar:6.5'
// 吐司工具类:https://github.com/getActivity/ToastUtils
implementation 'com.hjq:toast:8.3'
implementation 'com.hjq:toast:8.6'
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.hjq:xxpermissions:6.5'
// Json 解析框架:https://github.com/google/gson
Expand All @@ -54,5 +54,5 @@ dependencies {
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
// 日志调试:https://github.com/getActivity/Logcat
debugImplementation 'com.hjq:logcat:6.8'
debugImplementation 'com.hjq:logcat:8.2'
}
25 changes: 22 additions & 3 deletions app/src/androidTest/assets/NoSpecification.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@
"listTest1" : [],
"listTest2" : {},
"listTest3" : "",
"listTest4" : null,
"booleanTest1" : 0,
"booleanTest2" : 1,
"booleanTest3" : null,
"booleanTest4" : "true",
"booleanTest5" : [],
"booleanTest6" : {},
"stringTest1" : null,
"stringTest2" : false,
"stringTest3" : 123,
"intTest1" : 2.2,
"stringTest4" : [],
"stringTest5" : {},
"intTest1" : 1.1,
"intTest2" : null,
"intTest3" : "",
"longTest1" : 2.2,
"intTest4" : [],
"intTest5" : {},
"longTest1" : 1.1,
"longTest2" : null,
"longTest3" : "22"
"longTest3" : "22",
"longTest4" : [],
"longTest5" : {},
"floatTest1" : 11,
"floatTest2" : null,
"floatTest3" : "22",
"floatTest4" : [],
"floatTest5" : {},
"doubleTest1" : 11,
"doubleTest2" : null,
"doubleTest3" : "22",
"doubleTest4" : [],
"doubleTest5" : {}
}
15 changes: 8 additions & 7 deletions app/src/androidTest/assets/Specification.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
"listTest1" : [1, 2],
"listTest2" : ["a", "b", "c"],
"listTest3" : [1, 2, 3],
"listTest4" : [true, false],
"booleanTest1" : false,
"booleanTest2" : true,
"booleanTest3" : false,
"booleanTest4" : true,
"stringTest1" : null,
"stringTest2" : "",
"stringTest3" : "字符串",
"intTest1" : 1,
"intTest2" : 2,
"intTest3" : 3,
"longTest1" : 12580,
"longTest2" : 10086,
"longTest3" : 101000
"intTest2" : -2,
"longTest1" : 90000000000000009,
"longTest2" : -90000000000000009,
"floatTest1" : 1.1,
"floatTest2" : -1.1,
"doubleTest1" : 2.11111111111111112,
"doubleTest2" : -2.1111111111111112
}
25 changes: 25 additions & 0 deletions app/src/androidTest/java/com/hjq/http/test/JsonBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,42 @@ public class JsonBean {
private List<String> listTest1;
private List<String> listTest2;
private List<Integer> listTest3;
private List<Boolean> listTest4;

private boolean booleanTest1;
private boolean booleanTest2;
private boolean booleanTest3;
private boolean booleanTest4;
private boolean booleanTest5;
private boolean booleanTest6;

private String stringTest1;
private String stringTest2;
private String stringTest3;
private String stringTest4;
private String stringTest5;

private int intTest1;
private int intTest2;
private int intTest3;
private int intTest4;
private int intTest5;

private long longTest1;
private long longTest2;
private long longTest3;
private long longTest4;
private long longTest5;

private float floatTest1;
private float floatTest2;
private float floatTest3;
private float floatTest4;
private float floatTest5;

private double doubleTest1;
private double doubleTest2;
private double doubleTest3;
private double doubleTest4;
private double doubleTest5;
}
6 changes: 3 additions & 3 deletions app/src/main/java/com/hjq/http/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void requestPermission() {
*/

@Override
public void hasPermission(List<String> granted, boolean isAll) {
public void hasPermission(List<String> granted, boolean all) {

}

Expand Down Expand Up @@ -104,7 +104,7 @@ public void onClick(View v) {
case R.id.btn_main_get:
EasyHttp.get(this)
.api(new SearchAuthorApi()
.setAuthor("鸿洋"))
.setAuthor("鸿洋"))
.request(new HttpCallback<HttpData<SearchBean>>(this) {

@Override
Expand All @@ -116,7 +116,7 @@ public void onSucceed(HttpData<SearchBean> result) {
case R.id.btn_main_post:
EasyHttp.post(this)
.api(new SearchBlogsApi()
.setKeyword("搬砖不再有"))
.setKeyword("搬砖不再有"))
.request(new HttpCallback<HttpData<SearchBean>>(this) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/05/05
* desc : boolean / Boolean 解析适配器 {@link com.google.gson.internal.bind.TypeAdapters#BOOLEAN}
* desc : boolean / Boolean 类型解析适配器,参考:{@link com.google.gson.internal.bind.TypeAdapters#BOOLEAN}
*/
public class BooleanTypeAdapter extends TypeAdapter<Boolean> {

@Override
public Boolean read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
case BOOLEAN:
return in.nextBoolean();
case STRING:
// 如果后台返回 "true" 或者 "TRUE",则默认处理为 true
// 如果后台返回 "true" 或者 "TRUE",则处理为 true,否则为 false
return Boolean.parseBoolean(in.nextString());
case NUMBER:
// 如果这个后台返回是 1,则表示 true,否则表示 false
// 如果这个后台返回是 1 则处理为 true,否则为 false
return in.nextInt() == 1;
case NULL:
in.nextNull();
return null;
default:
return in.nextBoolean();
in.skipValue();
return null;
}
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/05/05
* desc : double / Double 解析适配器 {@link com.google.gson.internal.bind.TypeAdapters#DOUBLE}
* desc : double / Double 类型解析适配器,参考:{@link com.google.gson.internal.bind.TypeAdapters#DOUBLE}
*/
public class DoubleTypeAdapter extends TypeAdapter<Number> {

@Override
public Number read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
case NUMBER:
// 如果后台返回数值,则按照正常逻辑解析
return in.nextDouble();
case STRING:
try {
Expand All @@ -30,6 +26,9 @@ public Number read(JsonReader in) throws IOException {
// 如果是空字符串则会抛出这个异常
return 0;
}
case NULL:
in.nextNull();
return null;
default:
in.skipValue();
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/05/05
* desc : float / Float 解析适配器 {@link com.google.gson.internal.bind.TypeAdapters#FLOAT}
* desc : float / Float 类型解析适配器,参考:{@link com.google.gson.internal.bind.TypeAdapters#FLOAT}
*/
public class FloatTypeAdapter extends DoubleTypeAdapter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/05/05
* desc : int / Integer 解析适配器 {@link com.google.gson.internal.bind.TypeAdapters#INTEGER}
* desc : int / Integer 类型解析适配器,参考:{@link com.google.gson.internal.bind.TypeAdapters#INTEGER}
*/
public class IntegerTypeAdapter extends DoubleTypeAdapter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/05/05
* desc : List 解析适配器
* desc : List 类型解析适配器
*/
public class ListTypeAdapter implements JsonDeserializer<List> {

Expand All @@ -36,7 +36,7 @@ public List deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
}
return list;
} else {
// 和接口类型不符,直接返回 null
// 类型不符,直接返回 null
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/05/05
* desc : long / Long 解析适配器 {@link com.google.gson.internal.bind.TypeAdapters#LONG}
* desc : long / Long 类型解析适配器,参考:{@link com.google.gson.internal.bind.TypeAdapters#LONG}
*/
public class LongTypeAdapter extends DoubleTypeAdapter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2020/05/05
* desc : String 解析适配器 {@link com.google.gson.internal.bind.TypeAdapters#STRING}
* desc : String 类型解析适配器,参考:{@link com.google.gson.internal.bind.TypeAdapters#STRING}
*/
public class StringTypeAdapter extends TypeAdapter<String> {

@Override
public String read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
case STRING:
case NUMBER:
return in.nextString();
case BOOLEAN:
// 对于布尔类型比较特殊,需要做针对性处理
return Boolean.toString(in.nextBoolean());
case NULL:
in.nextNull();
return null;
default:
// 其他类型的数据直接按照字符串来处理
return in.nextString();
in.skipValue();
return null;
}
}

Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 26
versionCode 69
versionName "6.9"
versionCode 80
versionName "8.0"
}
}

Expand All @@ -27,7 +27,7 @@ publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'http'
version = '6.9'
version = '8.0'
description = 'Easy-to-use network request framework'
website = "https://github.com/getActivity/EasyHttp"
}
Expand Down
Loading

0 comments on commit 44c6ca9

Please sign in to comment.