Handle different JSON response types from same endpoint in RetroFit

前端 未结 3 694
悲&欢浪女
悲&欢浪女 2021-01-17 16:04

I need help.

I have an endpoint that takes a parameter. Depending on this parameter, the JSON returned will be completely different.

Is it possi

3条回答
  •  长发绾君心
    2021-01-17 17:02

    Just to close this off.

    You can get the raw JSON back from RetroFit and use GSON to manually serialise to the class type you want.

    For example:

    RestClient.get().getDataFromServer(params, new Callback() {
        @Override
        public void success(JSONObject json, Response response) {
            if(isTypeA) {
                //Use GSON to serialise to TypeA        
            } else {
                //Use GSON to serialise to TypeB
            }
        }
    });
    

提交回复
热议问题