Retrofit2+GsonConverter Generic Type Auto-Converting

Asked 1 years ago, Updated 1 years ago, 71 views

public class ResultVO<T> {
    public Result result;

    public Result getResult() {
        return result;
    }

    public class Result {
        public String code;
        public ArrayList<T> data;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public ArrayList<T> getData() {
            return data;
        }
    }
}


@POST("point/getMyPointHistory.json")
    Call<ResultArrayVO<CheeseGoldHistoryVO>> getMyPointHistory(@Query("type") String type, @Query("year") String year, @Query("month") String month);

I'd like to receive it in the form of General as shown in the code above.

However, CastException occurs due to problems such as https://github.com/google/gson/blob/master/UserGuide.md#TOC-Serializing-and-Deserializing-Generic-Types.

The solution in the official document is to create a TypeToken...

Questions

Thank you.

android retrofit2 gson

2022-09-21 22:55

1 Answers

On the interface, Set to @POST("point/getMyPointHistory.json") Call< JsonObject or JsonArray > and

You can specify the return type of the function that generates the request as the desired type and cast Json Object/JsonArray accordingly.


2022-09-21 22:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.