繁体   English   中英

从改造响应中解析 JSON 对象

[英]Parsing Through JSON Object From Retrofit Response

我对 android 开发人员还很陌生,我首先制作了一个天气应用程序,该应用程序可以显示当前的天气预报和天气预报。 我想添加一个按钮,允许用户查看每日预测,但从 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) {
            }
        });
    }

我认为您应该为从 api 获取的所有数据创建一个模型类,以便您可以更轻松地处理数据。

这篇文章解释说,

https://medium.com/swlh/basic-usage-of-retrofit-in-kotlin-for-fetching-json-8b9d37999058

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM