Android 改造 - 将响应放入数组列表

Android Retrofit - Putting the response into an arraylist

大家好,我已经改造成功地从 rest 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
},

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

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

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();

快乐编码

要将响应转换为可用的 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()