【问题标题】:Android Retrofit - Putting the response into an arraylistAndroid Retrofit - 将响应放入 arraylist
【发布时间】:2020-05-30 12:38:21
【问题描述】:

嘿,伙计们,我已经成功地从一个休息 API 获取数据并将其打印到日志中。但是,当我尝试将此响应捕获到数组列表中时,我收到一条错误消息:“无法推断参数(无法解析构造函数)。此错误显示在数组列表括号中

// Executes the network request on a background thread
        try {
            Response response = getWeather(locationCode, apiKey).execute();
            if (cancelRequest) {
                return;
            }
            if (response.code() == 200) {

                //  Weather weather = response.body().getWeather();
                Log.d(TAG, "onResponse: " + response.body().toString());

                List<Weather> list = new ArrayList<>(((Weather)response.body()));
                //mWeather.postValue(response);

GSON 响应

 {
"coord": {
    "lon": -5.93,
    "lat": 54.58
},
"weather": [
    {
        "id": 804,
        "main": "Clouds",
        "description": "overcast clouds",
        "icon": "04d"
    }
],
"base": "stations",
"main": {
    "temp": 291.56,
    "feels_like": 286.36,
    "temp_min": 291.15,
    "temp_max": 292.15,
    "pressure": 1024,
    "humidity": 45
},

如果有人能帮助我解决这个问题,将不胜感激。

【问题讨论】:

  • 您是否添加了如下改造的 gson 转换器? addConverterFactory(GsonConverterFactory.create())
  • 是的,gson 解析得很好,我可以在日志中看到它。它将它存储在数组列表中,这给我带来了麻烦

标签: java android retrofit2 android-mvvm


【解决方案1】:

我觉得你创建的 POJO 类可能不合适,因为你没有共享你的 POJO 类,请在你的代码中尝试下面的 POJO 类,看看错误是否仍然存在。

public class Weather {

private coordinate coord;
private ArrayList<weatherinfo> weather;
private String base;
private maininfo main;

public coordinate getCoord() {
    return coord;
}

public ArrayList<weatherinfo> getWeather() {
    return weather;
}

public String getBase() {
    return base;
}

public maininfo getMain() {
    return main;
}

public static class coordinate{
    String lon,lat;

    public String getLon() {
        return lon;
    }

    public String getLat() {
        return lat;
    }
}

public static class weatherinfo{
    String id,main,description,icon;

    public String getId() {
        return id;
    }

    public String getMain() {
        return main;
    }

    public String getDescription() {
        return description;
    }

    public String getIcon() {
        return icon;
    }
}

public static class maininfo{
    String temp,feels_like,temp_min,temp_max,pressure,humidity;

    public String getTemp() {
        return temp;
    }

    public String getFeels_like() {
        return feels_like;
    }

    public String getTemp_min() {
        return temp_min;
    }

    public String getTemp_max() {
        return temp_max;
    }

    public String getPressure() {
        return pressure;
    }

    public String getHumidity() {
        return humidity;
    }
}
}

然后在阅读您的回复时尝试这种方式。

ArrayList<Weather.weatherinfo> list = response.body().getWeather();

快乐编码

【讨论】:

  • 感谢您的回答,但 pojo 类工作并将数据返回到日志 np.不幸的是,您推荐的 ArrayList 语法不起作用。但是感谢您的帮助,我很感激。
【解决方案2】:

要将响应转换为可用的 java 对象,您必须使用 Gson 或使用 Retrofit 自动完成。由于您从电话中获得了通用的Response,这意味着您必须自己做。所以首先将 json 模式转换为 POJO 然后而不是

List<Weather> list = new ArrayList<>(((Weather)response.body()));

使用

ResponsePojo responsePojo = gson.fromJson(response.body().toString(), ResponsePojo.class); 

但是,您应该正确设置 Retrofit 以便它为您进行转换,而不是手动进行。然后你将直接从改造调用中取回你的对象 -

Response<ResponsePojo> response = getWeather(locationCode, apiKey).execute();
ResponsePojo responsePojo = response.body()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2020-09-18
    • 2019-09-03
    • 2020-10-26
    相关资源
    最近更新 更多