gpt4 book ai didi

java - 期望使用改造开始对象错误

转载 作者:行者123 更新时间:2023-11-29 23:00:56 25 4
gpt4 key购买 nike

我想从 forecast api 中检索每小时数据,但出现错误

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 221 path $.list[0].weather

老实说这有点奇怪,或者我根本不了解 POJO。所以,我创建了一个 POJO 类

public class hourlyModel {

@SerializedName("list")
List<ListPOJO> listList;

public List<ListPOJO> getList() {
return listList;
}

class ListPOJO{

@SerializedName("main")
Main main;
@SerializedName("weather")
Weather weather;
@SerializedName("wind")
Wind wind;

@SerializedName("dt")
long time;

public Main getMain() {
return main;
}

public Weather getWeather() {
return weather;
}

public Wind getWind() {
return wind;
}

public long getTime() {
return time;
}
}

class Main {

@SerializedName("temp")
private Double actualTemperature;
@SerializedName("pressure")
private Double airPressure;
@SerializedName("humidity")
private Double airHumidity;
@SerializedName("temp_min")
private Double minTemp;
@SerializedName("temp_max")
private Double maxTemp;

public Double getActualTemperature() {
return actualTemperature;
}

public Double getAirPressure() {
return airPressure;
}

public Double getAirHumidity() {
return airHumidity;
}

public Double getMinTemp() {
return minTemp;
}

public Double getMaxTemp() {
return maxTemp;
}
}

class Wind{
@SerializedName("speed")
private Double windSpeed;

public Double getWindSpeed() {
return windSpeed;
}
}


class Weather{
@SerializedName("icon")
private String forecastIcon;

public String getForecastIcon() {
return forecastIcon;
}
}

从这个 JSON 响应中检索数据

 {
"cod": "200",
"message": 0.006,
"cnt": 40,
"list": [
{
"dt": 1562522400,
"main": {
"temp": 12.29,
"temp_min": 12.29,
"temp_max": 17.05,
"pressure": 1010.39,
"sea_level": 1010.39,
"grnd_level": 973.92,
"humidity": 98,
"temp_kf": -4.76
},
"weather": [
{
"id": 501,
"main": "Rain",
"description": "moderate rain",
"icon": "10d"
}
],
"clouds": {
"all": 100
},
"wind": {
"speed": 1.23,
"deg": 113.621
},
"rain": {
"3h": 6.687
},
"sys": {
"pod": "d"
},
"dt_txt": "2019-07-07 18:00:00"
},
and so on...

我在下面的方法中调用它

public void retrofitCall(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();

ApiCalls api = retrofit.create(ApiCalls.class);
Call<hourlyModel> call = api.getHourlyForecast(latitude,longitude,APP_ID);
call.enqueue(new Callback<hourlyModel>() {
@Override
public void onResponse(Call<hourlyModel> call, Response<hourlyModel> response) {
if (!response.isSuccessful()){
Log.i(TAG, "onResponse: "+response.code());
}
Log.i(TAG, "onResponse: "+response.code());

List<hourlyModel.ListPOJO> list = response.body().getList();

for (hourlyModel.ListPOJO model: list){
temperatureString = String.valueOf(model.getMain().getActualTemperature());
System.out.println(temperatureString+"\n");
}
}
@Override
public void onFailure(Call<hourlyModel> call, Throwable t) {
Log.i(TAG, "onFailure: "+t.getMessage());
}
});

为什么会出现此错误?难道我不必先进入“列表”数组对象,然后从那里调用我想要的任何数据吗?

最佳答案

你的错误说它需要一个天气数组,但在你的类中你已经将它声明为对象 Weather weather .您必须将其声明为 List<Weather>因为 JSON 包含一个天气数组。

关于java - 期望使用改造开始对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56924782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com