我是android dev的新手,我首先开发了一个天气应用程序,它可以显示天气预报和当前天气预报。我想添加一个允许用户查看每日天气预报的按钮,但从API调用看,它似乎返回了一个JSON对象。我已经被困在这个问题上好几天了,我不知道该怎么办。

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.weatherapi.com/v1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
APICall apiCall = retrofit.create(APICall.class);
Call<WeatherModel> call = apiCall.getHourAndAstroDetails(latlong, "7");
System.out.println(call.request().url());
call.enqueue(new Callback<WeatherModel>() {
@Override
public void onResponse(Call<WeatherModel> call, Response<WeatherModel> response) {
if (days == 1) {
weatherList = response.body().getForecast().getForecastday().get(0).getHour();
System.out.println("this is the size of weatherList" + weatherList.size());
setAdapter(weatherList);
} else if (days == 7) {
dailyForcast = response.body().getForecast().getForecastday().get(0).getDay();
}
String sunriseTime = response.body().getForecast().getForecastday().get(0).getAstro().getSunrise();
String sunsetTime = response.body().getForecast().getForecastday().get(0).getAstro().getSunset();
sunriseTextView.setText(sunriseTime);
sunsetTextView.setText(sunsetTime);
}
@Override
public void onFailure(Call<WeatherModel> call, Throwable t) {
}
});
}发布于 2021-10-23 04:43:08
我认为您应该为从api获得的所有数据创建一个模型类,这样您就可以更容易地处理数据。
这篇文章解释了,
https://medium.com/swlh/basic-usage-of-retrofit-in-kotlin-for-fetching-json-8b9d37999058
https://stackoverflow.com/questions/69684767
复制相似问题