我从天气API中提取数据,当我将完整的网址放入浏览器时,我获得了所有数据,结构如下:
{
"data":{
"current_condition":[
{
"cloudcover":"75",
"FeelsLikeC":"0",
"FeelsLikeF":"32",
"humidity":"87",
"observation_time":"10:29 PM",
"precipMM":"0.0",
"pressure":"1021",
"temp_C":"4",
"temp_F":"39",
"visibility":"10",
"weatherCode":"116",
"weatherDesc":[
{
"value":"Partly Cloudy"
}
],
"weatherIconUrl":[
{
"value":"http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0004_black_low_cloud.png"
}
],
"winddir16Point":"SSW",
"winddirDegree":"210",
"windspeedKmph":"20",
"windspeedMiles":"13"
}
],
"request":[
{ }
],
"weather":[
{
"astronomy":[ ],
"date":"2015-12-11",
"hourly":[
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ }
],
"maxtempC":"8",
"maxtempF":"46",
"mintempC":"4",
"mintempF":"38",
"uvIndex":"0"
}
]
}
}
由于空间原因,每小时对象被最小化。
我的问题是,当我通过Retrofit 2.0在我的应用中使用此api调用时,current_condition
大小为0.请参阅我的调试器的屏幕截图:
我的对象是正确的,基于我使用http://www.jsonschema2pojo.org/的浏览器返回以获得正确的结构和对象。我的改造代码如下:
public interface OpenWeatherApiInterface {
@GET("?&format=json&num_of_days=5&key=API_KEY&")
Call<OpenWeather> getWeatherForLocation(@Query("q") String city);
}
public class RestClient {
private static OpenWeatherApiInterface mOpenWeatherApiInterface;
private static String mBaseUrl = "http://api.worldweatheronline.com/free/v2/weather.ashx";
public static OpenWeatherApiInterface getClient(){
if(mOpenWeatherApiInterface==null){
Retrofit client = new Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
mOpenWeatherApiInterface = client.create(OpenWeatherApiInterface.class);
}
return mOpenWeatherApiInterface;
}
}
然后在我的主要活动中测试它,我将其称为:
Call<OpenWeather> call = RestClient.getClient().getWeatherForLocation("London");
call.enqueue(new Callback<OpenWeather>() {
@Override
public void onResponse(Response<OpenWeather> response, Retrofit retrofit) {
Log.i("Main Activity", response.body().toString());
if (response.isSuccess()) {
OpenWeather weather = response.body();
Data data = weather.getData();
List<CurrentCondition> conditionList = data.getCurrentCondition();
Log.i("MainA", "blah");
} else {
try {
Log.i("Main Activity", response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Throwable t) {
Log.i("Main Activity", "Failure");
}
});
我的班级OpenWeather
public class OpenWeather {
private Data data;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The data
*/
public Data getData() {
return data;
}
/**
*
* @param data
* The data
*/
public void setData(Data data) {
this.data = data;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
我的班级数据:
public class Data {
private List<CurrentCondition> currentCondition = new ArrayList<CurrentCondition>();
private List<Request> request = new ArrayList<Request>();
private List<Weather> weather = new ArrayList<Weather>();
/**
*
* @return
* The currentCondition
*/
public List<CurrentCondition> getCurrentCondition() {
return currentCondition;
}
/**
*
* @param currentCondition
* The current_condition
*/
public void setCurrentCondition(List<CurrentCondition> currentCondition) {
this.currentCondition = currentCondition;
}
/**
*
* @return
* The request
*/
public List<Request> getRequest() {
return request;
}
/**
*
* @param request
* The request
*/
public void setRequest(List<Request> request) {
this.request = request;
}
/**
*
* @return
* The weather
*/
public List<Weather> getWeather() {
return weather;
}
/**
*
* @param weather
* The weather
*/
public void setWeather(List<Weather> weather) {
this.weather = weather;
}
}
这是我第一次使用Retrofit,我发现数组大小为0很奇怪。我想如果模型出现问题我应该崩溃
有人可以帮忙吗?
答案 0 :(得分:2)
这是因为您有不同的JSON密钥和对象属性名称。
即
fetch()
json键应匹配Data类的键,但您的字段名称为current_condition
如果您将其更改为
currentCondition
我认为它会起作用