Android 改造 - 模型是来自 API 的 GSON 数组
Android Retrofit - Models are GSON arrays from API
当从开放天气接收数据时 API 我收到空值。我在 android 中使用 retroFit2 来获取数据。这是来自日志的消息:
onResponse: server response Response{protocol=http/1.1, code=200, message=OK,
onResponse: WeatherResponse{weather=com.weatherapp.models.Weather@197d237}
这是 GSON:
{
"coord": {
"lon": -5.93,
"lat": 54.58
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 293.15,
"feels_like": 288.01,
"temp_min": 293.15,
"temp_max": 293.15,
"pressure": 1024,
"humidity": 37
},
"visibility": 10000,
"wind": {
"speed": 5.7,
"deg": 160
},
"clouds": {
"all": 96
},
"dt": 1590770221,
"sys": {
"type": 1,
"id": 1376,
"country": "GB",
"sunrise": 1590724667,
"sunset": 1590785095
},
型号
@SerializedName("coord")
@Expose
private CoOrdinateModel coOrdinateModel;
@SerializedName("weather")
@Expose
private WeatherModel[] weatherModel;
@SerializedName("main")
@Expose
private MainModel main;
@SerializedName("visibility")
@Expose
private String visibility;
@SerializedName("wind")
@Expose
private WindModel wind;
回应
public class WeatherResponse {
@SerializedName("coord")
@Expose()
private Weather weather;
public Weather getWeather() {
return weather;
}
@Override
public String toString() {
return "WeatherResponse{" +
"weather=" + weather +
'}';
}
}
API
@GET("/data/2.5/weather")
Call<WeatherResponse> getWeather(
@Query("id") String ID,
@Query("APPID") String API_KEY
);
理想情况下,我需要将来自 GSON 的几乎所有数据解析为 android 应用程序中的 java 对象。我以前用更简单的 GSON 做过这个,但是这个里面有数组,我不确定我的模型 class 是否正确完成以适应这个。
如果能帮助确定空值的根本原因,我们将不胜感激
你说的 "arrays" 不是数组,它们是嵌套对象(好吧,weather
是一个对象数组,但仍然如此)。为了处理这个问题,您需要在模型 class 上创建嵌套 classes 来处理嵌套对象类型,如下所示:
public class Weather {
public class Coordinate {
@SerializedName("lat")
@Expose
public float lat;
@SerializedName("long")
@Expose
public float long;
}
// ...
@SerializedName("coord")
@Expose
public Coordinate coOrd;
// repeat for every nested object type
}
对于 weather
键,请记住这是一个 数组 对象,因此模型中的字段应声明为:
public WeatherData[] weather; // or whatever you name the nested class as
// I wouldn't recommend Weather, since you've
// already named your model that
您的响应模型不正确 -
@SerializedName("main")
@Expose
private float main[];
应该是 -
@SerializedName("main")
@Expose
private MainModel main;
而您的 MainModel
将是您尝试解析的 POJO class -
public class MainModel {
@SerializedName("temp")
public Double temp;
@SerializedName("feels_like")
public Double feelsLike;
}
对其他人也一样引用 wind
因为它们不能用字符串 class.
解析
当从开放天气接收数据时 API 我收到空值。我在 android 中使用 retroFit2 来获取数据。这是来自日志的消息:
onResponse: server response Response{protocol=http/1.1, code=200, message=OK,
onResponse: WeatherResponse{weather=com.weatherapp.models.Weather@197d237}
这是 GSON:
{
"coord": {
"lon": -5.93,
"lat": 54.58
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 293.15,
"feels_like": 288.01,
"temp_min": 293.15,
"temp_max": 293.15,
"pressure": 1024,
"humidity": 37
},
"visibility": 10000,
"wind": {
"speed": 5.7,
"deg": 160
},
"clouds": {
"all": 96
},
"dt": 1590770221,
"sys": {
"type": 1,
"id": 1376,
"country": "GB",
"sunrise": 1590724667,
"sunset": 1590785095
},
型号
@SerializedName("coord")
@Expose
private CoOrdinateModel coOrdinateModel;
@SerializedName("weather")
@Expose
private WeatherModel[] weatherModel;
@SerializedName("main")
@Expose
private MainModel main;
@SerializedName("visibility")
@Expose
private String visibility;
@SerializedName("wind")
@Expose
private WindModel wind;
回应
public class WeatherResponse {
@SerializedName("coord")
@Expose()
private Weather weather;
public Weather getWeather() {
return weather;
}
@Override
public String toString() {
return "WeatherResponse{" +
"weather=" + weather +
'}';
}
}
API
@GET("/data/2.5/weather")
Call<WeatherResponse> getWeather(
@Query("id") String ID,
@Query("APPID") String API_KEY
);
理想情况下,我需要将来自 GSON 的几乎所有数据解析为 android 应用程序中的 java 对象。我以前用更简单的 GSON 做过这个,但是这个里面有数组,我不确定我的模型 class 是否正确完成以适应这个。
如果能帮助确定空值的根本原因,我们将不胜感激
你说的 "arrays" 不是数组,它们是嵌套对象(好吧,weather
是一个对象数组,但仍然如此)。为了处理这个问题,您需要在模型 class 上创建嵌套 classes 来处理嵌套对象类型,如下所示:
public class Weather {
public class Coordinate {
@SerializedName("lat")
@Expose
public float lat;
@SerializedName("long")
@Expose
public float long;
}
// ...
@SerializedName("coord")
@Expose
public Coordinate coOrd;
// repeat for every nested object type
}
对于 weather
键,请记住这是一个 数组 对象,因此模型中的字段应声明为:
public WeatherData[] weather; // or whatever you name the nested class as
// I wouldn't recommend Weather, since you've
// already named your model that
您的响应模型不正确 -
@SerializedName("main")
@Expose
private float main[];
应该是 -
@SerializedName("main")
@Expose
private MainModel main;
而您的 MainModel
将是您尝试解析的 POJO class -
public class MainModel {
@SerializedName("temp")
public Double temp;
@SerializedName("feels_like")
public Double feelsLike;
}
对其他人也一样引用 wind
因为它们不能用字符串 class.