【问题标题】:Getting Expected to begin object error using retrofit期望使用改造开始对象错误
【发布时间】:2019-11-17 08:53:16
【问题描述】:

我想从预测 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());
                }
        });

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

【问题讨论】:

    标签: java android json api retrofit


    【解决方案1】:

    您的错误表明它需要一个天气数组,但在您的类中您已将其声明为对象Weather weather。您必须将其声明为 List&lt;Weather&gt;,因为 JSON 包含一个天气数组。

    【讨论】:

      【解决方案2】:

      改变

      class ListPOJO {
      
          // Other fields
      
          @SerializedName("weather")
          Weather weather;
      
          // Other fields
      }
      

      class ListPOJO {
      
          // Other fields
      
          @SerializedName("weather")
          List<Weather> weather;
      
          // Other fields
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-03
        • 2018-07-20
        • 2020-11-20
        • 1970-01-01
        • 2020-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多