【问题标题】:How to resolve a Null Web service Response using Retrofit2?如何使用 Retrofit2 解决空 Web 服务响应?
【发布时间】:2018-12-26 04:51:55
【问题描述】:

我需要使用 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();

我得到的回应:

【问题讨论】:

  • 从图中,你的response body不为空,点击body左边的箭头
  • 我已经编辑了图像,你现在可以检查一下,问题是我只有一个正确的属性,即“cod”元素,我不明白为什么我的“list”元素是空
  • 分享List.java
  • 公共类列表 { private Main main;私有 java.util.List 天气 = null;私有字符串 dtTxt; public Main getMain() { return main; } public java.util.List getWeather() { 返回天气; } 公共字符串 getDtTxt() { 返回 dtTxt; } }
  • 这是用于显示“主要”天气温度的“主”类--> public class Main { private Double temp; public Double getTemp() { return temp; } }

标签: android json web-services parsing retrofit2


【解决方案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;
}

}

【讨论】:

  • 我得到相同的空响应。
  • 查看编辑后的答案更改,像这样@SerializedName("weather") private java.util.List&lt;Weather&gt; list =null;
  • 哇,你真是个天才!它有效,但我有最后一个问题..为什么你没有定义“列表”:WeatherResponse 类中的 [] 元素.. xD
  • 你是如何在没有定义“list”元素的情况下访问“weather”数组的
  • 由于您的回复中没有名为list的元素,如果工作也请接受答案
猜你喜欢
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
相关资源
最近更新 更多