Skip to content

Commit

Permalink
新增 Gson 容错处理
Browse files Browse the repository at this point in the history
新增 Lifecycle 管控
新增支持上传文件列表
优化日志打印策略
优化请求重试机制的逻辑
优化全局和局部参数的优先级
优化上传文件的 MIME 类型
  • Loading branch information
getActivity committed May 16, 2020
1 parent df189b5 commit 5cb32e9
Show file tree
Hide file tree
Showing 47 changed files with 1,147 additions and 422 deletions.
Binary file modified EasyHttp.apk
Binary file not shown.
26 changes: 6 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 网络请求框架

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

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

dependencies {
implementation 'com.hjq:http:6.0'
implementation 'com.hjq:http:6.5'
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
implementation 'com.google.code.gson:gson:2.8.5'
}
Expand All @@ -29,10 +31,6 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<!-- 外部存储读写权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

#### 服务器配置

public class RequestServer implements IRequestServer {
Expand Down Expand Up @@ -71,7 +69,7 @@
//.addParam("token", "6666666")
// 添加全局请求头
//.addHeader("time", "20191030")
// 启用配置
// 启用配置
.into();

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

#### 配置接口

public class LoginApi implements IRequestApi {
@Keep
public final class LoginApi implements IRequestApi {

@Override
public String getApi() {
Expand Down Expand Up @@ -204,19 +203,6 @@
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-dontwarn okio.**

# 保护 IRequestApi 类字段名不被混淆
-keepclassmembernames class * implements com.hjq.http.config.IRequestApi {
<fields>;
}
# 保护 Bean 类不被混淆(请注意修改包名路径)
-keepclassmembernames class xxx.xxx.xxx.xxx.xxx.response.** {
<fields>;
}
# 保护模型类的字段不被混淆(请注意修改包名路径)
-keepclassmembernames class xxx.xxx.xxx.xxx.xxx.xxx.HttpData {
<fields>;
}

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

Expand Down
22 changes: 14 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ android {
applicationId "com.hjq.http.demo"
minSdkVersion 14
targetSdkVersion 28
versionCode 60
versionName "6.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionCode 65
versionName "6.5"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
Expand All @@ -33,13 +34,18 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'

// 标题栏:https://github.com/getActivity/TitleBar
implementation 'com.hjq:titlebar:6.0'
// 吐司工具类:https://github.com/getActivity/ToastUtils
implementation 'com.hjq:toast:8.0'
implementation 'com.hjq:toast:8.2'
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.hjq:xxpermissions:6.2'
// Json 解析框架:https://github.com/google/gson
Expand All @@ -48,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.0'
debugImplementation 'com.hjq:logcat:6.5'
}
18 changes: 18 additions & 0 deletions app/src/androidTest/assets/NoSpecification.json
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"
}
18 changes: 18 additions & 0 deletions app/src/androidTest/assets/Specification.json
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
}
23 changes: 23 additions & 0 deletions app/src/androidTest/java/com/hjq/http/test/JsonBean.java
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 app/src/androidTest/java/com/hjq/http/test/JsonUnitTest.java
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;
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<!-- 适配 Android 7.0 文件意图 -->
<provider
android:name="android.support.v4.content.FileProvider"
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/com/hjq/http/demo/BaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.hjq.http.demo;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;

import com.hjq.http.EasyHttp;
import androidx.appcompat.app.AppCompatActivity;

import com.hjq.http.demo.http.model.HttpData;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;

import okhttp3.Call;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2019/05/19
* desc : 基类封装
*/
public class BaseActivity extends AppCompatActivity implements OnHttpListener {

/** 加载对话框 */
Expand Down Expand Up @@ -75,10 +81,4 @@ public void onFail(Exception e) {
public void onEnd(Call call) {
hideDialog();
}

@Override
protected void onDestroy() {
EasyHttp.cancel(this);
super.onDestroy();
}
}
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 @@ -9,11 +9,12 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ProgressBar;

import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import com.hjq.http.EasyHttp;
import com.hjq.http.demo.http.model.HttpData;
import com.hjq.http.demo.http.request.SearchAuthorApi;
Expand Down Expand Up @@ -57,7 +58,6 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.btn_main_post).setOnClickListener(this);
findViewById(R.id.btn_main_update).setOnClickListener(this);
findViewById(R.id.btn_main_download).setOnClickListener(this);

requestPermission();
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/hjq/http/demo/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public void onCreate() {

EasyConfig.with(new OkHttpClient())
// 是否打印日志
.setLogEnabled(BuildConfig.DEBUG)
//.setLogEnabled(BuildConfig.DEBUG)
// 设置服务器配置
.setServer(server)
// 设置请求处理策略
.setHandler(new RequestHandler())
.setHandler(new RequestHandler(this))
// 设置请求重试次数
.setRetryCount(3)
.setRetryCount(1)
// 添加全局请求参数
//.addParam("token", "6666666")
// 添加全局请求头
Expand Down
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);
}
}
Loading

0 comments on commit 5cb32e9

Please sign in to comment.