我正在使用Android上的loopj获取HTTP响应。我想做的是通过自己的类型并返回该类型的响应。这是一个单一的国家模型
class Country:BaseModel() {
@PrimaryKey
@SerializedName("id")
var id: Int=0
@Column
@SerializedName("name")
var name: String? = null
@Column
@SerializedName("code")
var code: String? = null
@SerializedName("country_code")
@Column
var country_code: String? = null
}
我所做的类使使用gson序列化的json数组序列化的类别如下
public class LoopGsonResponseHandler<T> extends JsonHttpResponseHandler {
public T Model;
//Prototype to handle the Gson list of objects
public void onSuccess(int statusCode, Header[] headers, T single){
}
//Prototype to handle the Gson list of objects
public void onSuccess(int statusCode, Header[] headers, ArrayList<T> list){
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
//call on succecc with parameter 3 being model
Type collectionType = new TypeToken<T>(){}.getType();
T data = new Gson().fromJson(response.toString(), collectionType);
this.onSuccess(statusCode,headers,data);
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
//call on succecc with parameter 3 being list
Log.e("Country","We got array");
Type collectionType = new TypeToken<ArrayList<T>>(){}.getType();
ArrayList<T> data = new Gson().fromJson(response.toString(), collectionType);
this.onSuccess(statusCode,headers,data);
}
}
我成功地解析了对GSON对象的响应,但我无法将其解析为参数化类型。我得到的错误是这个
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.neverest.client.models.MyJson
at com.neverest.client.utils.LoopGsonResponseHandler.onSuccess(LoopGsonResponseHandler.java:34)
at com.neverest.client.utils.LoopGsonResponseHandler.onSuccess(LoopGsonResponseHandler.java:55)
at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:154)
使用像这样的构造函数
通过public class LoopGsonResponseHandler<T> extends JsonHttpResponseHandler{
public T model;
private final Class<T> classOfT;
public LoopGsonResponseHandler(Class<T> classOfT){
this.classOfT = classOfT;
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response){
super.onSuccess(statusCode, headers, response);
T data = new Gson().fromJson(response.toString(), classOfT);
this.onSuccess(statusCode, headers, data);
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
//call on succecc with parameter 3 being list
Log.e("Country","We got array");
Type collectionType = new ListParametrizedType(classOfT);
ArrayList<T> data = new Gson().fromJson(response.toString(), collectionType);
this.onSuccess(statusCode,headers,data);
}
}
然后使LoopGsonResponseHandler
对象这样。
新的loopgsonresponsehandler(country.class(;
对于ArrayList,您将需要此类
private static class ListParametrizedType implements ParameterizedType {
private Type type;
public ListParametrizedType(Type type) {
this.type = type;
}
@Override
public Type[] getActualTypeArguments() {
return new Type[]{type};
}
@Override
public Type getRawType() {
return ArrayList.class;
}
@Override
public Type getOwnerType() {
return null;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}
你就完成了。:(