Skip to content

Commit

Permalink
新增支持参数嵌套解析
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Jun 15, 2020
1 parent 5cb32e9 commit e1d9a22
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 53 deletions.
Binary file modified EasyHttp.apk
Binary file not shown.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

> 码云地址:[Gitee](https://gitee.com/getActivity/EasyHttp)
![](EasyHttp.jpg)
> [点击此处下载Demo](EasyHttp.apk)
[点击此处下载Demo](https://raw.githubusercontent.com/getActivity/EasyHttp/master/EasyHttp.apk)
![](EasyHttp.jpg)

#### Gradle 集成

Expand All @@ -17,7 +17,7 @@
}

dependencies {
implementation 'com.hjq:http:6.5'
implementation 'com.hjq:http:6.8'
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
implementation 'com.google.code.gson:gson:2.8.5'
}
Expand Down Expand Up @@ -47,7 +47,7 @@

@Override
public BodyType getType() {
// 参数以 Json 格式提交(默认是表单)
// 参数以 Json 格式提交(默认是表单)
return BodyType.JSON;
}
}
Expand Down Expand Up @@ -204,6 +204,11 @@
-dontwarn okhttp3.**
-dontwarn okio.**

# 不混淆这个包下的字段名
-keepclassmembernames class com.hjq.http.demo.http.** {
<fields>;
}

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

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

buildTypes {
release {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Expand All @@ -43,16 +43,16 @@ dependencies {
implementation 'com.google.android.material:material:1.1.0'

// 标题栏:https://github.com/getActivity/TitleBar
implementation 'com.hjq:titlebar:6.0'
implementation 'com.hjq:titlebar:6.5'
// 吐司工具类:https://github.com/getActivity/ToastUtils
implementation 'com.hjq:toast:8.2'
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.hjq:xxpermissions:6.2'
implementation 'com.hjq:xxpermissions:6.5'
// Json 解析框架:https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.8.5'
// OkHttp 框架:https://github.com/square/okhttp
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
// 日志调试:https://github.com/getActivity/Logcat
debugImplementation 'com.hjq:logcat:6.5'
debugImplementation 'com.hjq:logcat:6.8'
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/hjq/http/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void hasPermission(List<String> granted, boolean isAll) {
public void noPermission(List<String> denied, boolean quick) {
if (quick) {
ToastUtils.show("授权失败,请手动授予权限");
XXPermissions.gotoPermissionSettings(this, true);
XXPermissions.startPermissionActivity(this, true);
} else {
ToastUtils.show("请先授予权限");
requestPermission();
Expand All @@ -90,7 +90,7 @@ public void noPermission(List<String> denied, boolean quick) {
@Override
protected void onRestart() {
super.onRestart();
if (XXPermissions.isHasPermission(this, Permission.Group.STORAGE)) {
if (XXPermissions.hasPermission(this, Permission.Group.STORAGE)) {
hasPermission(null, true);
} else {
requestPermission();
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/hjq/http/demo/http/model/HttpData.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.hjq.http.demo.http.model;

import androidx.annotation.Keep;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/EasyHttp
* time : 2019/05/19
* desc : 统一接口数据结构
*/
@Keep
public class HttpData<T> {

/** 返回码 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Object requestSucceed(LifecycleOwner lifecycle, Response response, Type t

if (!response.isSuccessful()) {
// 返回响应异常
throw new ResponseException(mApplication.getString(R.string.http_server_error), response);
throw new ResponseException(mApplication.getString(R.string.http_response_error) + ",responseCode:" + response.code() + ",message:" + response.message(), response);
}

ResponseBody body = response.body();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.hjq.http.demo.http.request;

import androidx.annotation.Keep;

import com.hjq.http.config.IRequestApi;

/**
Expand All @@ -10,7 +8,6 @@
* time : 2019/11/18
* desc : 按照作者昵称搜索文章
*/
@Keep
public final class SearchAuthorApi implements IRequestApi {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.hjq.http.demo.http.request;

import androidx.annotation.Keep;

import com.hjq.http.annotation.HttpRename;
import com.hjq.http.config.IRequestApi;

Expand All @@ -11,7 +9,6 @@
* time : 2019/06/07
* desc : 搜索文章
*/
@Keep
public final class SearchBlogsApi implements IRequestApi {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.hjq.http.demo.http.request;

import androidx.annotation.Keep;

import com.hjq.http.config.IRequestApi;
import com.hjq.http.config.IRequestHost;

Expand All @@ -13,7 +11,6 @@
* time : 2019/12/14
* desc : 上传图片
*/
@Keep
public final class UpdateImageApi implements IRequestHost, IRequestApi {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.hjq.http.demo.http.response;

import androidx.annotation.Keep;

import java.util.List;

/**
Expand All @@ -10,7 +8,6 @@
* time : 2019/11/18
* desc : 搜索结果
*/
@Keep
public final class SearchBean {

/**
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<string name="http_data_explain_error">数据解析异常,请稍后</string>
<string name="http_server_out_time">服务器请求超时,请稍后再试</string>
<string name="http_network_error">请求失败,请检查网络设置</string>
<string name="http_server_error">服务器响应异常,请稍后再试</string>
<string name="http_response_error">服务器响应异常,请稍后再试</string>
<string name="http_server_error">服务器连接异常,请稍后再试</string>
<string name="http_request_cancel">请求被中断,请重试</string>

</resources>
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 65
versionName "6.5"
versionCode 68
versionName "6.8"
}
}

Expand All @@ -27,7 +27,7 @@ publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'http'
version = '6.5'
version = '6.8'
description = 'Easy-to-use network request framework'
website = "https://github.com/getActivity/EasyHttp"
}
Expand Down
83 changes: 83 additions & 0 deletions library/src/main/java/com/hjq/http/EasyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
import android.os.Handler;
import android.os.Looper;

import com.hjq.http.annotation.HttpIgnore;
import com.hjq.http.annotation.HttpRename;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.RequestBody;

/**
* author : Android 轮子哥
Expand Down Expand Up @@ -105,4 +115,77 @@ public static String getFileMD5(File file) {
}
return null;
}

/**
* 判断对象是否为 Bean 类
*/
public static boolean isBeanType(Object object) {
// Number:Long、Integer、Short、Double、Float、Byte
// CharSequence:String、StringBuilder、StringBuilder
return !(object instanceof Number || object instanceof CharSequence ||
object instanceof Boolean || object instanceof List ||
object instanceof Map || object instanceof File ||
object instanceof InputStream || object instanceof RequestBody ||
object instanceof Character);
}

/**
* Bean 类转 HashMap
*/
public static HashMap<String, Object> beanToHashMap(Object object) {
HashMap<String, Object> data = null;

Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
// 允许访问私有字段
field.setAccessible(true);
try {

// 获取字段的对象
Object value = field.get(object);

// 前提是这个字段值不能为空(基本数据类型有默认的值,而对象默认的值为 null)
if (value == null) {
// 遍历下一个字段
continue;
}

// 获取字段的名称
String key;
if (field.isAnnotationPresent(HttpRename.class)) {
key = field.getAnnotation(HttpRename.class).value();
} else {
key = field.getName();
// 如果是内部类则会出现一个字段名为 this$0 的外部类对象,会导致无限递归,这里要忽略掉
// 如果使用静态内部类则不会出现这个问题
if (object.getClass().toString().startsWith(field.getType().toString())) {
//"class com.hjq.http.demo.http.request.SearchBlogsApi$TestBean".startsWith("class com.hjq.http.demo.http.request.SearchBlogsApi")
continue;
}
}

// 如果这个字段需要忽略,则进行忽略
// 前提是这个字段值不能为空(基本数据类型有默认的值,而对象默认的值为 null)
if (field.isAnnotationPresent(HttpIgnore.class)) {
// 遍历下一个字段
continue;
}

if (data == null) {
data = new HashMap<>(fields.length);
}

if (isBeanType(value)) {
data.put(key, beanToHashMap(value));
} else {
data.put(key, value);
}

} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void onResponse(Response response) throws Exception {
// 如果这个监听对象是直接实现了接口
type = ((ParameterizedType) types[0]).getActualTypeArguments()[0];
} else {
// 如果这个监听对象有通过类继承
// 如果这个监听对象是通过类继承
type = ((ParameterizedType) mListener.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

Expand Down
Loading

0 comments on commit e1d9a22

Please sign in to comment.