问题
I am using OpenWeatherMap API to retrieve forecast information for 16 days.
http://www.json-generator.com/api/json/get/cuDhZDicMO?indent=2
From a JSON like that, I am able to retrieve the value for cod. That means everything is working fine with the API. I have tested it while debugging and the API is working fine as I am able to retrieve the value of cod.
But there is something wrong with my list in the ForecastInfo data model. Could you please look at that and tell me where I've gone wrong? Because it's not retrieving the data that comes under that list. I have attached my forecastInfo which is the data model for the JSON. Thank you so much!
import java.util.ArrayList;
import java.util.List;
public class ForecastInfo {
public final double cod;
public List<LIST> list = new ArrayList<LIST>();
public List<LIST> getList() {
return list;
}
public ForecastInfo(List<LIST> list,double cod)
{
this.cod=cod;
this.list=list;
}
class LIST {
public final long dt;
public final Temp temp;
public LIST(long dt,Temp temp) {
this.dt = dt;
this.temp=temp;
}
class Temp
{
public final double day;
public Temp(double day){
this.day=day;
}
}
}
}
回答1:
Because your model needs to have all keys that your response json file contains and name of keys should be very same. Why don't you use http://www.jsonschema2pojo.org/ site to to convert json to pojo check source type to JSON and annotation type to gson. May it can help you.Or simply get the response in String type and then convert it like
LoginResponse loginResponse = new Gson().fromJson(json, LoginResponse.class);
来源:https://stackoverflow.com/questions/43472796/retrofit2-data-model-something-wrong-with-the-list