Android retrofit 2 | get list data from JSON response

ZookKep :

I'm using retrofit2. I'm making HTTP calls and get this kind of JSON data as a response:

[
    {
        "ActualTotalLoadByMonthValue": "4264156.37",
        "AreaName": "Hungary",
        "AreaTypeCode": "CTY",
        "Dataset": "ActualTotalLoad",
        "MapCode": "HU",
        "Month": "1",
        "ResolutionCode": "PT15M",
        "Source": "entso-e",
        "Year": "2018"
    }
]

I want to take the context of this JSON and present it in an array, but for starters I don't know how to get the context of my response. I tried with response.body() but it doesn't seem to work. There is my code:

val call = RequestManager.service.getactualtotalload(areaName,resolution, datetype ,"2018")
            call.enqueue(object : Callback<List<Response1>> {
                override fun onResponse(call: Call<List<Response1>>, response: Response<List<Response1>>)
                {
                    loaderout.visibility = View.GONE
                    if (response.isSuccessful) {
                        Log.d("deee","Json " + response.body().toString())
                        openTableActivity()
                    } else {
                    }
                }

                override fun onFailure(call: Call<List<Response1>>, t: Throwable) {
                    loaderout.visibility = View.GONE
                }
            })

while Response1 is like this:

public class Response1 {

    @SerializedName("ActualTotalLoadByMonthValue")
    @Expose
    private String actualTotalLoadByMonthValue;
    @SerializedName("AreaName")
    @Expose
    private String areaName;
    @SerializedName("AreaTypeCode")
    @Expose
    private String areaTypeCode;
    @SerializedName("Dataset")
    @Expose
    private String dataset;
    @SerializedName("MapCode")
    @Expose
    private String mapCode;
    @SerializedName("Month")
    @Expose
    private String month;
    @SerializedName("ResolutionCode")
    @Expose
    private String resolutionCode;
    @SerializedName("Source")
    @Expose
    private String source;
    @SerializedName("Year")
    @Expose
    private String year;

    public String getActualTotalLoadByMonthValue() {
        return actualTotalLoadByMonthValue;
    }

    public void setActualTotalLoadByMonthValue(String actualTotalLoadByMonthValue) {
        this.actualTotalLoadByMonthValue = actualTotalLoadByMonthValue;
    }

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getAreaTypeCode() {
        return areaTypeCode;
    }

    public void setAreaTypeCode(String areaTypeCode) {
        this.areaTypeCode = areaTypeCode;
    }

    public String getDataset() {
        return dataset;
    }

    public void setDataset(String dataset) {
        this.dataset = dataset;
    }

    public String getMapCode() {
        return mapCode;
    }

    public void setMapCode(String mapCode) {
        this.mapCode = mapCode;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getResolutionCode() {
        return resolutionCode;
    }

    public void setResolutionCode(String resolutionCode) {
        this.resolutionCode = resolutionCode;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

Also my RequestManager:

object RequestManager {
    val interceptor = HttpLoggingInterceptor()
    val client = OkHttpClient.Builder().addInterceptor(interceptor).build()


    init {
        //TODO must be None at live
        interceptor.level = HttpLoggingInterceptor.Level.BODY
    }


    val retrofit = Retrofit.Builder()
        .baseUrl("http://c6913be7.ngrok.io/energy/api/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build()

    val service = retrofit.create(Api::class.java)

}

Any tips on how to access the data and then how to pass them on to a table array layout?

Divakar Murugesh :

@ZookKep - response.body() will return list of objects. You can directly use that. Don't need to any other logic.

I mean don't make .toString() call on response.body().

You can check with log like follows

val listOfModels = response.body()

if (listOfModels != null) {
    for ((index, model) in listOfModels.withIndex()) {
        Log.d("----", "----")
        Log.d("INDEX $index", "ActualTotalLoadByMonthValue ${model.actualTotalLoadByMonthValue}")
        Log.d("INDEX $index", "AreaName ${model.areaName}")
        Log.d("INDEX $index", "AreaTypeCode ${model.areaTypeCode}")
        Log.d("INDEX $index", "Dataset ${model.dataset}")
        Log.d("INDEX $index", "MapCode ${model.mapCode}")
        Log.d("INDEX $index", "Month ${model.month}")
        Log.d("INDEX $index", "ResolutionCode ${model.resolutionCode}")
        Log.d("INDEX $index", "Source ${model.source}")
        Log.d("INDEX $index", "Year ${model.year}")
        Log.d("----", "----")
       }
}

Still you have doubt on this please let me know in comments section. I am happy to help :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10524&siteId=1