0

There is an issue in the retrofit library that I am facing in the dynamic data JSON response.

For example, if any key in the JSON is of int type and due to some reason the particular key type is changed to string. Now corresponding to the first JSON response when the key was of int type the model class was created. Now my question is I want to handle the updated string format JSON. How should I solve this task?

Below is the ApiClient class of the retrofit.

public class ApiClient 
{
    public static final String BASE_URL = "http:***********";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

I have got some solutions which use the rest adapter but I want to solve using the above format. How I should do please guide.

neelkanth_vyas
  • 192
  • 1
  • 7
  • you should set the type as the string, so you can convert to any format. – Subin Babu May 15 '18 at 05:07
  • I want to handle it dynamically. How I can check response's key data type in retrofit – neelkanth_vyas May 15 '18 at 05:10
  • you define as string and when getting server response you can change into any format .but you can not change dynamically json key. –  May 15 '18 at 05:12
  • Like in the swift development which provides the alamofire network library. if let result = jsonObject["result"] as? String { // you will be here if there is result key in jsonObject } else { // there is no result key in jsonObject } Can we handle this in android when using dynamic response. – neelkanth_vyas May 15 '18 at 05:18
  • Possible duplicate of [Create simple POJO classes (bytecode) at runtime (dynamically)](https://stackoverflow.com/questions/5178391/create-simple-pojo-classes-bytecode-at-runtime-dynamically) – Subin Babu May 15 '18 at 05:20
  • Can u give any simple code @SubinBabu – neelkanth_vyas May 15 '18 at 05:28

0 Answers0