标签:
class GsonResponsePasare<T> {
T deal(String response) {
Type gsonType = new TypeToken<CommonResponse<T>>() {
}.getType();
CommonResponse<T> commonResponse = new Gson().fromJson(response, gsonType);
lg.e("Data is : " + commonResponse.data, "Class Type is : " + commonResponse.data.getClass().toString());
return commonResponse.data;
}
}
class CommonResponse<T> {
int status;
T data;
}
String strResult = new GsonResponsePasare<String>().deal("{\"status\":-4,\"data\":\"xiaoxuan948\"}");
lg.e("StringResult:" + strResult);
class DataInfo {
String name;
}
DataInfo dataInfoResult = new GsonResponsePasare<DataInfo>().deal("{\"status\":-4,\"data\":{\"name\":\"xiaoxuan948\"}}");
lg.e("DataInfo:" + dataInfoResult.toString(), "Value:" + dataInfoResult.name);
List<DataInfo> resultList = new GsonResponsePasare<List<DataInfo>>().deal("{\"status\":-4,\"data\":[{\"name\":\"xiaoxuan948\"},{\"name\":\"coca\"}]}");
for (DataInfo entity : resultList) {
lg.e("DataInfo:" + entity.toString(), "Value:" + entity.name);
}
class GsonResponsePasare<T> implements ParameterizedType {
private final UtilsLog lg = UtilsLog.getLogger(GsonResponsePasare.class);
public T deal(String response) {
// Type gsonType = new ParameterizedType() {//...};//不建议该方式,推荐采用GsonResponsePasare实现ParameterizedType.因为getActualTypeArguments这里涉及获取GsonResponsePasare的泛型集合
Type gsonType = this;
CommonResponse<T> commonResponse = new Gson().fromJson(response, gsonType);
lg.e("Data is : " + commonResponse.data, "Class Type is : " + commonResponse.data.getClass().toString());
return commonResponse.data;
}
@Override
public Type[] getActualTypeArguments() {
Class clz = this.getClass();
//这里必须注意在外面使用new GsonResponsePasare<GsonResponsePasare.DataInfo>(){};实例化时必须带上{},否则获取到的superclass为Object
Type superclass = clz.getGenericSuperclass(); //getGenericSuperclass()获得带有泛型的父类
if (superclass instanceof Class) {
throw new RuntimeException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
return parameterized.getActualTypeArguments();
}
@Override
public Type getOwnerType() {
return null;
}
@Override
public Type getRawType() {
return CommonResponse.class;
}
}
List<DataInfo> resultList = new GsonResponsePasare<List<DataInfo>>() {
}.deal("{\"status\":-4,\"data\":[{\"name\":\"xiaoxuan948\"},{\"name\":\"coca\"}]}");
for (DataInfo entity : resultList) {
lg.e("DataInfo:" + entity.toString(), "Value:" + entity.name);
}
GsonResponsePasare<DataInfo> pasare = new GsonResponsePasare<DataInfo>() {
};
DataInfo dataInfoResult = pasare.deal("{\"status\":-4,\"data\":{\"name\":\"xiaoxuan948\"}}");
lg.e("DataInfo:" + dataInfoResult.toString(), "Value:" + dataInfoResult.name);
java.lang.IllegalArgumentException
at com.google.gson.internal.$Gson$Preconditions.checkArgument($Gson$Preconditions.java:46)
at com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl.<init>($Gson$Types.java:448)
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:103)
at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
at com.google.gson.reflect.TypeToken.get(TypeToken.java:296)
at com.google.gson.Gson.fromJson(Gson.java:877)
at com.google.gson.Gson.fromJson(Gson.java:844)
at com.google.gson.Gson.fromJson(Gson.java:793)...
Android之基于Gson的ParameterizedType进行泛型解析
标签:
原文地址:http://www.cnblogs.com/linux007/p/5785046.html