-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增 Lifecycle 管控 新增支持上传文件列表 优化日志打印策略 优化请求重试机制的逻辑 优化全局和局部参数的优先级 优化上传文件的 MIME 类型
- Loading branch information
1 parent
df189b5
commit 5cb32e9
Showing
47 changed files
with
1,147 additions
and
422 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"listTest1" : [], | ||
"listTest2" : {}, | ||
"listTest3" : "", | ||
"booleanTest1" : 0, | ||
"booleanTest2" : 1, | ||
"booleanTest3" : null, | ||
"booleanTest4" : "true", | ||
"stringTest1" : null, | ||
"stringTest2" : false, | ||
"stringTest3" : 123, | ||
"intTest1" : 2.2, | ||
"intTest2" : null, | ||
"intTest3" : "", | ||
"longTest1" : 2.2, | ||
"longTest2" : null, | ||
"longTest3" : "22" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"listTest1" : [1, 2], | ||
"listTest2" : ["a", "b", "c"], | ||
"listTest3" : [1, 2, 3], | ||
"booleanTest1" : false, | ||
"booleanTest2" : true, | ||
"booleanTest3" : false, | ||
"booleanTest4" : true, | ||
"stringTest1" : null, | ||
"stringTest2" : "", | ||
"stringTest3" : "字符串", | ||
"intTest1" : 1, | ||
"intTest2" : 2, | ||
"intTest3" : 3, | ||
"longTest1" : 12580, | ||
"longTest2" : 10086, | ||
"longTest3" : 101000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.hjq.http.test; | ||
|
||
import java.util.List; | ||
|
||
public class JsonBean { | ||
|
||
private List<String> listTest1; | ||
private List<String> listTest2; | ||
private List<Integer> listTest3; | ||
private boolean booleanTest1; | ||
private boolean booleanTest2; | ||
private boolean booleanTest3; | ||
private boolean booleanTest4; | ||
private String stringTest1; | ||
private String stringTest2; | ||
private String stringTest3; | ||
private int intTest1; | ||
private int intTest2; | ||
private int intTest3; | ||
private long longTest1; | ||
private long longTest2; | ||
private long longTest3; | ||
} |
88 changes: 88 additions & 0 deletions
88
app/src/androidTest/java/com/hjq/http/test/JsonUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.hjq.http.test; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.internal.bind.TypeAdapters; | ||
import com.hjq.http.demo.http.json.BooleanTypeAdapter; | ||
import com.hjq.http.demo.http.json.DoubleTypeAdapter; | ||
import com.hjq.http.demo.http.json.FloatTypeAdapter; | ||
import com.hjq.http.demo.http.json.IntegerTypeAdapter; | ||
import com.hjq.http.demo.http.json.ListTypeAdapter; | ||
import com.hjq.http.demo.http.json.LongTypeAdapter; | ||
import com.hjq.http.demo.http.json.StringTypeAdapter; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
/** | ||
* Example local unit test, which will execute on the development machine (host). | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
public class JsonUnitTest { | ||
|
||
private Gson mGson; | ||
|
||
@Before | ||
public void onTestBefore() { | ||
mGson = new GsonBuilder() | ||
.registerTypeAdapterFactory(TypeAdapters.newFactory(String.class, new StringTypeAdapter())) | ||
.registerTypeAdapterFactory(TypeAdapters.newFactory(boolean.class, Boolean.class, new BooleanTypeAdapter())) | ||
.registerTypeAdapterFactory(TypeAdapters.newFactory(int.class, Integer.class, new IntegerTypeAdapter())) | ||
.registerTypeAdapterFactory(TypeAdapters.newFactory(long.class, Long.class, new LongTypeAdapter())) | ||
.registerTypeAdapterFactory(TypeAdapters.newFactory(float.class, Float.class, new FloatTypeAdapter())) | ||
.registerTypeAdapterFactory(TypeAdapters.newFactory(double.class, Double.class, new DoubleTypeAdapter())) | ||
.registerTypeHierarchyAdapter(List.class, new ListTypeAdapter()) | ||
.create(); | ||
} | ||
|
||
@Test | ||
public void onSpecification() { | ||
Context context = InstrumentationRegistry.getInstrumentation().getContext(); | ||
String json = getAssetsString(context, "Specification.json"); | ||
mGson.fromJson(json, JsonBean.class); | ||
} | ||
|
||
@Test | ||
public void onNoSpecification() { | ||
Context context = InstrumentationRegistry.getInstrumentation().getContext(); | ||
String json = getAssetsString(context, "NoSpecification.json"); | ||
mGson.fromJson(json, JsonBean.class); | ||
} | ||
|
||
@After | ||
public void onTestAfter() { | ||
mGson = null; | ||
} | ||
|
||
/** | ||
* 获取资产目录下面文件的字符串 | ||
*/ | ||
private static String getAssetsString(Context context, String file) { | ||
try { | ||
InputStream inputStream = context.getAssets().open(file); | ||
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[512]; | ||
int length; | ||
while ((length = inputStream.read(buffer)) != -1) { | ||
outStream.write(buffer, 0, length); | ||
} | ||
outStream.close(); | ||
inputStream.close(); | ||
return outStream.toString(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/hjq/http/demo/http/json/BooleanTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.hjq.http.demo.http.json; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* author : Android 轮子哥 | ||
* github : https://github.com/getActivity/EasyHttp | ||
* time : 2020/05/05 | ||
* 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 STRING: | ||
// 如果后台返回 "true" 或者 "TRUE",则默认处理为 true | ||
return Boolean.parseBoolean(in.nextString()); | ||
case NUMBER: | ||
// 如果这个后台返回是 1,则表示 true,否则表示 false | ||
return in.nextInt() == 1; | ||
default: | ||
return in.nextBoolean(); | ||
} | ||
} | ||
@Override | ||
public void write(JsonWriter out, Boolean value) throws IOException { | ||
out.value(value); | ||
} | ||
} |
Oops, something went wrong.