Wednesday, 8 May 2019

Create Dynamic Object Request In Retrofit

How can I create Dynamic Object for retrofit request. e.g example request 1:

 "answer" :     {
        "Aggravating factor" :         {
            "value_dropdown" : "None"
        },
        "Associated factors" :         {
            "value" : "1"
        }
}

example request 2:

 "answer" :     {
            "Intensity :         {
                "value_dropdown" : "Major"
            },
            "Duration" :         {
                "value" : "5"
            }
    }

so every time The request model is changing, I have all these parameternames and values that i collect from an answer form filled by end user, then how can is use it to create request model with dynamic values i.e. "answer" : parameter name.

My effort: a static model with it's serialization output:

public class SampleModel {


    @SerializedName("answer")
    public ArrayList<QuestionAnswers> answer;

    public SampleModel() {
        answer = new ArrayList<>();
    }


    public static class QuestionAnswers {
        public String question_title;
        public List<KeyValuePaire> questions;

        public QuestionAnswers() {
            questions = new ArrayList<>();
        }
    }

    public static class KeyValuePaire {
        public String _type;
        public String _value;
    }

}

this model request prints below log :

{
  "answer": [
    {
      "question_title": "Aggravating factor",
      "questions": [
        {
          "_type": "value_dropdown",
          "_value": "None"
        }
      ]
    }
  ]
}



from Create Dynamic Object Request In Retrofit

No comments:

Post a Comment