簡體   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