如何使用Retrofit2解决Null Web服务响应?

时间:2018-07-18 10:01:25

标签: android json web-services parsing retrofit2

我需要使用Retrofit2 **解析的JSON响应

{
    "cod": "200",
    "message": 0.0062,
    "cnt": 4,
    "list": [
        {
            "dt": 1531915200,
            "main": {
                "temp": 308,
                "temp_min": 307.876,
                "temp_max": 308,
                "pressure": 1013.67,
                "sea_level": 1028.96,
                "grnd_level": 1013.67,
                "humidity": 32,
                "temp_kf": 0.13
            },
            "weather": [
                {
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01d"
                }
            ],
            "clouds": {
                "all": 0
            },
            "wind": {
                "speed": 1.96,
                "deg": 90.0012
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2018-07-18 12:00:00"
        },
        {
            "dt": 1531926000,
            "main": {
                "temp": 309.55,
                "temp_min": 309.452,
                "temp_max": 309.55,
                "pressure": 1012.51,
                "sea_level": 1027.69,
                "grnd_level": 1012.51,
                "humidity": 30,
                "temp_kf": 0.09
            },
            "weather": [
                {
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01d"
                }
            ],
            "clouds": {
                "all": 0
            },
            "wind": {
                "speed": 2.63,
                "deg": 72.5014
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2018-07-18 15:00:00"
        },
        {
            "dt": 1531936800,
            "main": {
                "temp": 306.87,
                "temp_min": 306.808,
                "temp_max": 306.87,
                "pressure": 1012.06,
                "sea_level": 1027.49,
                "grnd_level": 1012.06,
                "humidity": 32,
                "temp_kf": 0.06
            },
            "weather": [
                {
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01d"
                }
            ],
            "clouds": {
                "all": 0
            },
            "wind": {
                "speed": 2.42,
                "deg": 68.0013
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2018-07-18 18:00:00"
        },
        {
            "dt": 1531947600,
            "main": {
                "temp": 297.77,
                "temp_min": 297.738,
                "temp_max": 297.77,
                "pressure": 1012.84,
                "sea_level": 1028.22,
                "grnd_level": 1012.84,
                "humidity": 59,
                "temp_kf": 0.03
            },
            "weather": [
                {
                    "id": 801,
                    "main": "Clouds",
                    "description": "few clouds",
                    "icon": "02n"
                }
            ],
            "clouds": {
                "all": 24
            },
            "wind": {
                "speed": 2.32,
                "deg": 120.5
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2018-07-18 21:00:00"
        }
    ],
    "city": {
        "id": 2464470,
        "name": "Tunis",
        "coord": {
            "lat": 36.8001,
            "lon": 10.1848
        },
        "country": "TN",
        "population": 693210
    }
}

我的Java类“ WeatherResponse”,用​​于实例化响应对象:

import com.google.gson.annotations.SerializedName;



public class WeatherResponse {

    @SerializedName("cod")
    private String cod;

    @SerializedName("message")
    private double message;

    @SerializedName("cnt")
    private double cnt;
   @SerializedName("list")
    private java.util.List<List> list =null;

    public java.util.List<List> getList() {
        return list;
    }

}

我分配响应正文的代码行:

WeatherResponse wD = response.body();

我得到的回复

enter image description here

1 个答案:

答案 0 :(得分:1)

WeatherResponse.java中进行更改

更改

@SerializedName("list")
private java.util.List<List> list =null;

@SerializedName("weather")
private java.util.List<Weather> list =null;

WeatherResponse.java

public class WeatherResponse {

    @SerializedName("cod")
    private String cod;

    @SerializedName("message")
    private double message;

    @SerializedName("cnt")
    private int cnt;

    @SerializedName("weather")
    private java.util.List<Weather> list =null;


    public java.util.List<Weather> getList() {
        return list;
    }

}

Weather.java

    public class Weather {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("main")
@Expose
private String main;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getMain() {
return main;
}

public void setMain(String main) {
this.main = main;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

}