Skip to content

Commit 2f4d2a5

Browse files
committed
修复 Gson 解析泛型失败的问题
1 parent 28668d4 commit 2f4d2a5

File tree

7 files changed

+19
-29
lines changed

7 files changed

+19
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#### Gradle 集成
88

99
dependencies {
10-
implementation 'com.hjq:http:3.0'
10+
implementation 'com.hjq:http:3.2'
1111
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
1212
implementation 'com.google.code.gson:gson:2.8.5'
1313
}

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.http.demo"
88
minSdkVersion 14
99
targetSdkVersion 28
10-
versionCode 30
11-
versionName "3.0"
10+
versionCode 32
11+
versionName "3.2"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void onFail(Exception e) {
125125

126126
@Override
127127
public void onSucceed(HttpData<SearchBean> result) {
128-
ToastUtils.show("请求成功");
128+
ToastUtils.show("请求成功" + result.getData().getSize());
129129
}
130130

131131
@Override

app/src/main/java/com/hjq/http/demo/http/model/RequestHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void requestEnd(Context context, Call call) {
8787
}
8888

8989
@Override
90-
public Object requestSucceed(Context context, Response response, Class clazz) throws Exception {
90+
public Object requestSucceed(Context context, Response response, Type type) throws Exception {
9191
if (!response.isSuccessful()) {
9292
// 返回响应异常
9393
throw new ResponseException(context.getString(R.string.http_server_error), response);
@@ -98,11 +98,11 @@ public Object requestSucceed(Context context, Response response, Class clazz) th
9898
return null;
9999
}
100100

101-
if (Response.class.equals(clazz)) {
101+
if (Response.class.equals(type)) {
102102
return response;
103103
}
104104

105-
if (Bitmap.class.equals(clazz)) {
105+
if (Bitmap.class.equals(type)) {
106106
// 如果这是一个 Bitmap 对象
107107
return BitmapFactory.decodeStream(body.byteStream());
108108
}
@@ -119,17 +119,17 @@ public Object requestSucceed(Context context, Response response, Class clazz) th
119119
EasyLog.json(text);
120120

121121
final Object result;
122-
if (String.class.equals(clazz)) {
122+
if (String.class.equals(type)) {
123123
// 如果这是一个 String 对象
124124
result = text;
125-
} else if (JSONObject.class.equals(clazz)) {
125+
} else if (JSONObject.class.equals(type)) {
126126
try {
127127
// 如果这是一个 JSONObject 对象
128128
result = new JSONObject(text);
129129
} catch (JSONException e) {
130130
throw new DataException(context.getString(R.string.http_data_explain_error), e);
131131
}
132-
} else if (JSONArray.class.equals(clazz)) {
132+
} else if (JSONArray.class.equals(type)) {
133133
try {
134134
// 如果这是一个 JSONArray 对象
135135
result = new JSONArray(text);
@@ -139,7 +139,7 @@ public Object requestSucceed(Context context, Response response, Class clazz) th
139139
} else {
140140

141141
try {
142-
result = GSON.fromJson(text, clazz);
142+
result = GSON.fromJson(text, type);
143143
} catch (JsonSyntaxException e) {
144144
// 返回结果读取异常
145145
throw new DataException(context.getString(R.string.http_data_explain_error), e);

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
defaultConfig {
99
minSdkVersion 11
1010
targetSdkVersion 26
11-
versionCode 30
12-
versionName "3.0"
11+
versionCode 32
12+
versionName "3.2"
1313
}
1414
}
1515

@@ -22,7 +22,7 @@ publish {
2222
userOrg = 'getactivity'//填写bintray用户名,注意大小写
2323
groupId = 'com.hjq'//定义的maven group id最终引用形式
2424
artifactId = 'http'//maven的artifact id
25-
version = '3.0'//maven 上发布版本号
25+
version = '3.'//maven 上发布版本号
2626
description = 'Easy-to-use network request framework'//描述,自己定义
2727
website = "https://github.com/getActivity/EasyHttp"//项目在github中的地址
2828
}

library/src/main/java/com/hjq/http/callback/DefaultCallback.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,7 @@ protected void onResponse(Response response) throws Exception {
4242
type = ((ParameterizedType) mListener.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
4343
}
4444

45-
Class clazz;
46-
if (type instanceof Class) {
47-
// 如果这个是简单的 Class(String、JSONObject、JSONArray)
48-
clazz = (Class) type;
49-
} else if (type instanceof ParameterizedType) {
50-
// 如果这个是复杂的 Class(HttpData<Bean>)
51-
clazz = (Class) ((ParameterizedType) type).getRawType();
52-
} else {
53-
// 如果这个是其他类型 Class,就直接用 Void 代替
54-
clazz = Void.TYPE;
55-
}
56-
57-
final Object result = EasyConfig.getInstance().getHandler().requestSucceed(mContext, response, clazz);
45+
final Object result = EasyConfig.getInstance().getHandler().requestSucceed(mContext, response, type);
5846
EasyUtils.runOnUiThread(mListener != null, new Runnable() {
5947

6048
@SuppressWarnings("unchecked")

library/src/main/java/com/hjq/http/config/IRequestHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.content.Context;
44

5+
import java.lang.reflect.Type;
6+
57
import okhttp3.Call;
68
import okhttp3.Response;
79

@@ -34,11 +36,11 @@ public interface IRequestHandler {
3436
*
3537
* @param context 上下文对象
3638
* @param response 响应对象
37-
* @param clazz 解析类型
39+
* @param type 解析类型
3840
* @return 返回结果
3941
* @throws Exception 回调失败方法
4042
*/
41-
Object requestSucceed(Context context, Response response, Class clazz) throws Exception;
43+
Object requestSucceed(Context context, Response response, Type type) throws Exception;
4244

4345
/**
4446
* 请求失败

0 commit comments

Comments
 (0)