【发布时间】:2021-12-09 13:32:00
【问题描述】:
我是 android dev 的新手,我首先制作了一个天气应用程序,它显示了 holly 预报和当前预报。我想添加一个按钮,允许用户查看每日预测,但从 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) {
}
});
}
【问题讨论】:
-
只是补充一下,因为我想我遗漏了一些信息。我需要进入 Day{},但我需要获取一个列表,以便我可以将它附加到一个 recyclerView。我要么是走错了路,要么是这太令人沮丧了。
标签: java android json retrofit