1

When I get my response.body() it will not allow me to set it to a List.

SAMPLE GSON:

 {
"cod": "200",
"message": 0,
"cnt": 5,
"list": [
    {
        "dt": 1590861600,
        "main": {
            "temp": 17.74,
            "feels_like": 14.52,
            "temp_min": 15.99,
            "temp_max": 17.74,
            "pressure": 1022,
            "sea_level": 1022,
            "grnd_level": 1021,
            "humidity": 51,
            "temp_kf": 1.75
        },
        "weather": [
            {
                "id": 803,
                "main": "Clouds",
                "description": "broken clouds",
                "icon": "04d"
            }
        ],
        "clouds": {
            "all": 82
        },
        "wind": {
            "speed": 3.75,
            "deg": 124
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2020-05-30 18:00:00"
    },

Class

public class WeatherResponse {

@SerializedName("cod")
@Expose
private String cod;
@SerializedName("message")
@Expose
private Long message;
@SerializedName("cnt")
@Expose
private Long cnt;
@SerializedName("list")
@Expose
private List<WeatherList> results;
@SerializedName("city")
@Expose
private City city;

CODE

  private List<WeatherResponse> mWeatherResponseList;


  public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {

            Log.d(TAG, "onResponse: server response " + response.toString());

            // response code 200 means a successful request
            // if successful store the response body in the log
            if (response.code() == 200) {

              mWeatherResponseList = response.body();   --Throws an error

The error states incompatible types: Required Java.util.list - Found WeatherResponse

I did this:

list = new ArrayList(Collections.singleton(((response.body())))); Which appears to have worked. But the problem now is the response has nested Json. Its one object with a list of 5 items. When I try to display the 5 items in the recyclerview it only displays the one object but not the nested list of 5 items which I actually want

AndroidDev123
  • 280
  • 3
  • 24

2 Answers2

0

Try this declare this arraylist like this

private List<WeatherList> mWeatherResponseList=new ArrayList();

now in onResponse() use this

mWeatherResponseList = response.body().getResults();
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • That will get me access to the inner nested lists, but it will stop me getting necessary information from the outer list – AndroidDev123 May 30 '20 at 20:56
0

you are trying to access the entire JSON object but you are required with only the list of results in the JSON object. so you are getting the single JSON object response in the call, so you could get the list of results this way.

private List<WeatherList> mWeatherResponseList;

public void onResponse(Call<WeatherResponse> call, Response<WeatherResponse> response) {

        Log.d(TAG, "onResponse: server response " + response.toString());

        if (response.code() == 200) {

          mWeatherResponseList = response.body().results;   

This way you are getting the list.

Nitin Tej
  • 353
  • 1
  • 7
  • Theres another list nested in that list. any ideas how I access that? – AndroidDev123 May 31 '20 at 08:36
  • there are two ways as i know, you could parse the response again in java like in this blog post https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java. or you can probably change the type of the variable to whatever type is like i changed the WeatherList. and then use response.body().results.getWeather and getClouds for individual result – Nitin Tej May 31 '20 at 12:38