繁体   English   中英

将参数化类型转换为Gson的类

[英]Casting a parameterized type to a class for Gson

我在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);
        }
    }

您完成了。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM