繁体   English   中英

Android Retrofit - 将响应放入 arraylist

[英]Android Retrofit - Putting the response into an arraylist

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

// 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 object,您必须使用 Gson 或使用 ZB979402Z23 自动执行它76A0354。 由于您从呼叫中获得通用Response ,这意味着您必须自己做。 所以首先将 json 模式转换为 POJO 然后而不是

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

利用

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

但是,您应该正确设置 Retrofit 以便它为您进行转换,而不是手动进行。 然后您将直接从 retrofit 电话中取回您的 object -

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM