I have this JSON response
{
"cod": "200",
"message": 0.0085,
"cnt": 40,
"list": [
{
"dt": 1562533200,
"main": {
"temp": 15.55,
"temp_min": 15.55,
"temp_max": 15.96,
"pressure": 1012.01,
"sea_level": 1012.01,
"grnd_level": 976.03,
"humidity": 97,
"temp_kf": -0.41
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}
],
"clouds": {
"all": 100
},
and so on... How do i get data from weather array? I've tried
ArrayList<hourlyModel.Weather> list = new ArrayList<>();
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 = (ArrayList<hourlyModel.Weather>) response.body().getWeather();
for (hourlyModel.Weather model : list){
forecastID = String.valueOf(model.getForecastIcon());
Toast.makeText(Hourly_weather.this,forecastID,Toast.LENGTH_LONG).show();
}
}
Here's the POJO
@SerializedName("list")
List<ListPOJO> listList;
@SerializedName("weather")
List<Weather> weather;
public List<Weather> getWeather() {
return weather;
}
public List<ListPOJO> getList() {
return listList;
}
class ListPOJO{
@SerializedName("main")
Main main;
@SerializedName("wind")
Wind wind;
@SerializedName("dt")
long time;
public Main getMain() {
return main;
}
public List<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;
}
public Main(Double actualTemperature, Double airPressure, Double airHumidity) {
this.actualTemperature = actualTemperature;
this.airPressure = airPressure;
this.airHumidity = airHumidity;
// this.weather.forecastIcon = forecastID;
}
}
class Wind{
@SerializedName("speed")
private Double windSpeed;
public Double getWindSpeed() {
return windSpeed;
}
}
class Weather{
@SerializedName("icon")
private String forecastIcon;
public String getForecastIcon() {
return forecastIcon;
}
}
I'm assume that, I'd want to go to hourlyModel.ListPOJO -> Weather, but kinda can't do that. Getting this error Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference What should I change, to make it works? I don't understand, why can't I reach the second array, from first one.
Your json isn't a valid json, and you can use this website to transform a valid one to Java object: http://www.jsonschema2pojo.org/
Also, your pojo isn't right since list and weather aren't on the same level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With