Android改造-无法添加对列表/ recyclerview的响应

问题描述 投票:1回答:1

当我得到response.body()时,将不允许我将其设置为列表。

SAMSON 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"
    },

班级

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

错误指出不兼容的类型:必需的Java.util.list-找到的WeatherResponse

我这样做:

list = new ArrayList(Collections.singleton((((response.body())))));;这似乎奏效了。但是现在的问题是响应嵌套了Json。它的一个对象带有5个项目的列表。当我尝试在recyclerview中显示5个项目时,它仅显示一个对象,而不显示我实际想要的5个项目的嵌套列表

java android android-recyclerview retrofit
1个回答
0
投票
您正在尝试将响应正文放入数组,但无法正常工作使用

mWeatherResponseList = response.body().listGetterHere();

而不是

mWeatherResponseList = response.body();

© www.soinside.com 2019 - 2024. All rights reserved.