第一次开通博客,不知写些什么。目前正在做一个Android项目,需要调用天气接口来显示连续几天的天气信息状况,但由于网上一些资料有点杂乱或不全(或许是自己水平不够,别人写的简洁),总之刚开始时无从下手。慢慢懂了之后,觉得有必要写点东西。为避免以后遗忘,也方便过后复制粘贴,于是记下其中的具体步骤,给予提示。同时,希望能给和我一样正在学习Android的人提供一点小帮助。若有错误,敬请指正!
1. 注册和风天气账号
注册地址:http://console.heweather.com/register
注册账号后会得到一个个人认证key
2. API地址:
城市接口:https://api.heweather.com/x3/weather?cityid=城市ID&key=你的认证key
(例:https://api.heweather.com/x3/weather?cityid=CN101010100&key=c0e7636c8e2f415???)
3.返回的JSON数据
。。。。。。。。。。。。。。(省略)
4.编写JavaBean类
根据json数据创建相应的类,类名分别为Weather、Aqi、Basic、Dailyforecast、HourlyForcast、Now、
Suggestion。为简单易懂,类数据成员访问控制方式都设为public。(也可以设为private,不过要获取数据就靠写多个get函数)
①Weather.java
public classWeather {
public Aqi aqi; public Basic basic; public Listdaily_forecast; public List hourly_forecast; public Now now; public String status; public Suggestion suggestion; }
②Aqi.java
public class Aqi {
public City city; public class City { public String aqi; private String co; private String no2; private String o3; private String pm10; public String pm25; private String qlty; private String so2; } }③Basic.java
public class Basic { public String city; private String cnty; public String id; public String lat; public String lon; public Update update; public class Update { public String loc; public String utc; } }
④DialyForecast.java (由于dialy_forcast字段对应的是一个数组,故可以用List来表示)
public class DailyForecast { public Astro astro; public class Astro { public String sr; public String ss; } public Cond cond; public class Cond { public String code_d; public String code_n; public String txt_d; public String txt_n; } public String date; public String hum; public String pcpn; public String pop; public String pres; public Tmp tmp; public class Tmp { public String max; public String min; } public String vis; public Wind wind; public class Wind { public String deg; public String dir; public String sc; public String spd; } }
⑤HourlyForcast.java
public class HourlyForecast { public String date; public String hum; public String pop; public String pres; public String tmp; public Wind wind; public class Wind { public String deg; public String dir; public String sc; public String spd; } }
⑥Now.java
public class Now { public Cond cond; public class Cond { public String code; public String txt; } public String fl; public String hum; public String pcpn; public String pres; public String tmp; public String vis; public Wind wind; public class Wind { public String deg; public String dir; public String sc; public String spd; } }
⑦Suggestion.java
public class Suggestion { public Air air; public class Air { public String brf; public String txt; } public Comf comf; public class Comf { public String brf; public String txt; } public Cw cw; public class Cw { public String brf; public String txt; } public Drsg drsg; public class Drsg { public String brf; public String txt; } public Flu flu; public class Flu { public String brf; public String txt; } public Sport sport; public class Sport { public String brf; public String txt; } public Trav trav; public class Trav { public String brf; public String txt; } public Uv uv; public class Uv { public String brf; public String txt; } }
5.用Gson解析数据,得到一个Weather对象(其中参数response为返回的json数据)
JSONObject jsonObject=new JSONObject(response); JSONArray jsonArray=jsonObject.getJSONArray("HeWeather data service 3.0");
String weatherContent=jsonArray.getJSONObject(0).toString();
Gson gson=new Gson();
Weather weather=gson.fromJson(weatherContent,Weather.class);
6.简单获取数据
城市名: weather.basic.city
更新时间: weather.basic.update.loc
天气: weather.now.cond.txt
今天温度: weather.now.tmp
最低温度: weather.daily_forecast[0].tmp.min
最高温度: weather.daily_forecast[0].tmp.min
舒适度: weather.suggestion.comf.txt;
洗车指数: weather.suggestion.cw.txt;
运动建议: weather.suggestion.sport.txt;
就写到这里了,调用天气接口其实很简单,按照固定的格式就可以调用并解析,多用几遍就可以记住了,加油!(凡星逝水2017)