How to handle Dynamic JSON in Retrofit?

后端 未结 10 2364
余生分开走
余生分开走 2020-11-22 16:23

I am using the retrofit efficient networking library, but I am unable to handle Dynamic JSON which contains single prefix responseMessage which changes to

10条回答
  •  无人共我
    2020-11-22 16:52

    The accepted answer seemed over complicated for me, I solve it this way:

    Call call = client.request(params);
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Response response) {
            if (response.isSuccess()) {
                Gson gson = new Gson();
                ResponseBody repsonseBody = response.body().string();
                if (isEmail) {
                    EmailReport reports = gson.fromJson(responseBody, EmailReport.class);
                } else{
                    PhoneReport reports = gson.fromJson(repsonseBody, PhoneReport.class);
                }
            }
        }
        @Override
        public void onFailure(Throwable t) {
            Log.e(LOG_TAG, "message =" + t.getMessage());
        }
    });
    

    This is just an example in attempt to show you how you can use different model.

    The variable isEmail is just a boolean for your condition to use the appropriate model.

提交回复
热议问题