期望通过改型开始出现对象错误

时间:2019-07-07 18:04:01

标签: java android json api retrofit

我想从预测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...

我在Follow方法中调用它

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());
                }
        });

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

2 个答案:

答案 0 :(得分:1)

您的错误表明它需要一个“天气数组”,但是在您的类中已将其声明为对象Weather weather。由于JSON包含天气数组,因此必须将其声明为List<Weather>

答案 1 :(得分:0)

更改自

class ListPOJO {

    // Other fields

    @SerializedName("weather")
    Weather weather;

    // Other fields
}

收件人

class ListPOJO {

    // Other fields

    @SerializedName("weather")
    List<Weather> weather;

    // Other fields
}