Narayanan S Narayanan S - 5 months ago 20
JSON Question

Get Retrofit result as Json Object in Android

I am using Retrofit API for communication. I want JSON Object to be stored directly in the Retrofit Result Bean. I tried the below process and it produced in null value. but while using postman the result is proper only.

The below is the output of the API

{
"status": 101,
"message": "Ok",
"type": 1,
"data": {
"value": "adakdj4lkj43"
}
}


The data field above keeps on changing as per the input in one case value on other case date this data depends on the type

below is my ResponseBean

public class ResponseBean {
private int status = 0;
private String message = "";
private int type = 0;
private JSONObject data = new JSONObject();
}


When performing the retrofit call I am getting this data as {} which means there is no data. Please help me to solve this. Thanks in Advance.

Answer

The concept you are trying to do is perfect. But the only thing here went wrong is using JSONObject in the Retrofit Bean. Retrofit is a library which convert the JSONObject to Bean Object. Better you try Object in stead of JSONObject. I think Retrofit does work on convert a part of JSONObject to Object. So try

            public class ResponseBean {
                            private int status = 0;
                            private String message = "";
                            private int type = 0;
                            private Object data = new Object();
            }

Instead of using this as DataType.

private JSONObject data = new JSONObject();