GsonResponseBodyConverter.java
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.huaheng.wms.https.convert;
import com.google.gson.Gson;
import com.huaheng.wms.WMSLog;
import org.json.JSONObject;
import java.io.IOException;
import java.lang.reflect.Type;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Converter;
public class GsonResponseBodyConverter<T> implements Converter<ResponseBody,T> {
private final Gson gson;
private final Type type;
public GsonResponseBodyConverter(Gson gson,Type type){
this.gson = gson;
this.type = type;
}
@Override
public T convert(ResponseBody value) throws IOException {
String response = value.string();
//先将返回的json数据解析到Response中,如果code==200,则解析到我们的实体基类中,否则抛异常
WMSLog.d("convert :" + response);
Response httpResult = gson.fromJson(response, Response.class);
// if (httpResult.code()==200) {
//200的时候就直接解析,不可能出现解析异常。因为我们实体基类中传入的泛型,就是数据成功时候的格式
return gson.fromJson(response, type);
// }
// }else {
// ErrorResponse errorResponse = gson.fromJson(response,ErrorResponse.class);
// //抛一个自定义ResultException 传入失败时候的状态码,和信息
// throw new ResultException(errorResponse.getCode(),errorResponse.getMsg());
// }
}
}