【问题标题】:Android Retrofit - Unable to add response to a List / recyclerviewAndroid Retrofit - 无法将响应添加到 List / recyclerview
【发布时间】:2020-09-18 08:25:28
【问题描述】:

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

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;

代码

  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 - Found WeatherResponse

我这样做了:

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

【问题讨论】:

  • 你能展示示例 json 结构吗?
  • 我用示例 json 更新了问题
  • 也发布此类数据 WeatherResponse
  • 我之前开发过这个project。想看就看。
  • This 是我的 RecyclerView 适配器。这就是我显示 5 天天气的方式。 ArrayList => List

标签: java android android-recyclerview retrofit


【解决方案1】:

试试这个 像这样声明这个数组列表

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

现在在onResponse() 使用这个

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

【讨论】:

  • 这将使我能够访问内部嵌套列表,但它会阻止我从外部列表中获取必要的信息
【解决方案2】:

您正在尝试访问整个 JSON 对象,但您只需要 JSON 对象中的结果列表。 所以您在调用中获得了单个 JSON 对象响应,因此您可以通过这种方式获得结果列表。

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;   

这样你就得到了列表。

【讨论】:

  • 该列表中嵌套了另一个列表。任何想法我如何访问它?
  • 据我所知有两种方法,您可以像在这篇博文stackoverflow.com/questions/2591098/how-to-parse-json-in-java 中一样在 java 中再次解析响应。或者您可以将变量的类型更改为任何类型,就像我更改了 WeatherList 一样。然后使用 response.body().results.getWeather 和 getClouds 获得个别结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 2021-09-30
相关资源
最近更新 更多