首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Retrofit响应解析JSON对象

从Retrofit响应解析JSON对象
EN

Stack Overflow用户
提问于 2021-10-23 02:58:50
回答 1查看 55关注 0票数 -1

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

代码语言:javascript
复制
        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) {
            }
        });
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-23 04:43:08

我认为您应该为从api获得的所有数据创建一个模型类,这样您就可以更容易地处理数据。

这篇文章解释了,

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69684767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档