Reputation:
I'd want to retrieve hourly data from forecast api, and I'm getting error
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 221 path $.list[0].weather
That's a bit strange to be honest, or I just don't understand the POJO at all. So, I've created a POJO class
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;
}
}
to retrieve data from this JSON response
{
"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...
I'm calling it in the follow method
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());
}
});
Why am I getting this error? Don't I have for first to enter to the "list" array object, and from there call any data that I want?
Upvotes: 1
Views: 52
Reputation: 4206
Your error says that it expects an Array of Weather but in your class you have declared it as object Weather weather
. You must declare it as List<Weather>
since the JSON holds an Array of Weather.
Upvotes: 1
Reputation: 4549
Change from
class ListPOJO {
// Other fields
@SerializedName("weather")
Weather weather;
// Other fields
}
To
class ListPOJO {
// Other fields
@SerializedName("weather")
List<Weather> weather;
// Other fields
}
Upvotes: 0