最近在弄一个安卓天气应用.前几天下了不少天气应用,有商业公司的,也有个人的,用起来都不太好,不是无用的功能太多就是不符合我的审美观,我只是想看下天气而已,我想看的时候看下.不想让天气应用老是来烦我,其实现在上网查天气也是很方便的,不过既然在学安卓开发,就做个自己想要的天气应用.
天气应用不算复杂,前提是你不想把它做得太复杂,要是想把它做得复杂,加上各种各样的功能也是可以让天气应用变得很复杂,我只想要 一个简单的能查天气和天气预报的应用,所以逻辑比较简单.从网上获取天气,然后将天气显示出来就可以了,这貌似是废话,现在的大部分应用不是这样吗?从网上获取数据,然后把数据显示出来,再根据应用的特点,添加和数据互动的功能.
获取天气数据源
要做天气应用,第一个要解决的问题是天气数据的来源.在国内最权威的来源当然是中国天气网,中国所有的天气数据都在那里,在各种网上看到的各种天气信息基本上都是从那里来的.而且中国天气网现在的气象数据接口免费开放,以前是要收钱的,现在免费开发,只要注册账号,成为开发者,获取API ,就能获取几乎所有的天气数据了,不过这个注册有点坑,我注册了,审核还没有通过,不知道什么时候能通过中国天气网气象数据开放平台地址,点进去就能注册了.前几天比较坑,不知道是什么情况,我一点注册就无法连接到服务器,刚刚写博客的时候想起了这件事,点进去看了下,竟然点进去了,所以说我是刚刚才注册的,注册的时候要填身份证,还要上传证件照片,还要填使用API的服务器IP,还是必填项,没有服务器怎么办?我也不知道,我前几天买了三个月的个阿里云ECS,就把那个IP填上去了,不知道审核能不能通过.当然除了中国天气网的数据,还有其他的第三方数据中心,新浪百度都有开放API,关于其他第三方的API,给两个知乎的链接,有较详细的回答网上的天气API哪一个更可靠,有哪些免费开放且收录城市较完整的天气 API 接口这两个回答都有比较详细的介绍,这里不再多做介绍.我现在用的是和风天气提供的API,这个数据比较全,有免费的,不过有次数限制,不想注册的,点这里,百度API Store,这个也不错.具体选哪个数据来源,就看个人喜好了.中国气象网开放数据平台的数据当然是最权威的,不过我太好获取,等我审核通过了,我再试下它的数据。
使用和风天气的数据
先看下和风天气返回的json数据,和风天气的数据很全,但是pm2.5这类数据只有一些大城市有,很多城市没有,除了这点外,其他数据都有。调用方法官网上有详细说明,这里只看下它的数据内容
{
"HeWeather data service 3.0":[
{
"aqi": 空气质量指数
{"city": 城市数据
{"aqi":"110", 空气质量指数
"co":"1", 一氧化碳
"no2":"60", 二氧化碳
"o3":"28", 臭氧
"pm10":"103", pm10
"pm25":"82", pm2.5
"qlty":"轻度污染", 空气质量类别
"so2":"14" 二氧化硫
}
},
basic":{ 城市基本信息
"city":"广州", 城市名称
"cnty":"中国", 国家名称
"id":"CN101280101", 城市id
"lat":"23.108000", 城市经度
"lon":"113.265000", 城市纬度
"update":{ 数据更新时间
"loc":"2015-12-21 15:08", 数据更新的当地时间
"utc":"2015-12-21 07:08" 数据更新的标准时间
}
},
"daily_forecast":[ 天气预报
{
"astro":{ 天文数值
"sr":"07:03", 日出时间
"ss":"17:46" 日落时间
},
"cond":{ 天气状况
"code_d":"305", 白天天气代码
"code_n":"104", 晚上天气代码
"txt_d":"小雨", 白天天气描述
"txt_n":"阴" 晚上天气描述
},
"date":"2015-12-21", 当地日期
"hum":"67", 湿度
"pcpn":"0.0", 降雨量
"pop":"38", 降水概率
"pres":"1021", 气压
"tmp":{ 温度
"max":"19", 最高
"min":"16" 最低
},
"vis":"10", 能见度
"wind":{ 风力状况
"deg":"65", 风向(角度)
"dir":"无持续风向", 风向(方向)
"sc":"微风", 风力等级
"spd":"0" 风速
}
},
{
"astro":{
"sr":"07:03",
"ss":"17:46"
},
"cond":{
"code_d":"104",
"code_n":"104",
"txt_d":"阴",
"txt_n":"阴"
},
"date":"2015-12-22",
"hum":"86",
"pcpn":"1.3",
"pop":"87",
"pres":"1021",
"tmp":{
"max":"21",
"min":"18"
},
"vis":"9",
"wind":{
"deg":"49",
"dir":"无持续风向",
"sc":"微风",
"spd":"9"
}
},
{
"astro":{
"sr":"07:04",
"ss":"17:47"
},
"cond":{
"code_d":"101",
"code_n":"101",
"txt_d":"多云",
"txt_n":"多云"
},
"date":"2015-12-23",
"hum":"73",
"pcpn":"0.2",
"pop":"0",
"pres":"1016",
"tmp":{
"max":"23",
"min":"18"
},
"vis":"10",
"wind":{
"deg":"183",
"dir":"无持续风向",
"sc":"微风",
"spd":"6"
}
},
{
"astro":{
"sr":"07:04",
"ss":"17:47"
},
"cond":{
"code_d":"101",
"code_n":"305",
"txt_d":"多云",
"txt_n":"小雨"
},
"date":"2015-12-24",
"hum":"64",
"pcpn":"0.3",
"pop":"55",
"pres":"1016",
"tmp":{
"max":"17",
"min":"12"
},
"vis":"10",
"wind":{
"deg":"187",
"dir":"北风",
"sc":"3-4",
"spd":"12"
}
},
{
"astro":{
"sr":"07:05",
"ss":"17:48"
},
"cond":{
"code_d":"305",
"code_n":"101",
"txt_d":"小雨",
"txt_n":"多云"
},
"date":"2015-12-25",
"hum":"82",
"pcpn":"7.7",
"pop":"63",
"pres":"1021",
"tmp":{"
max":"16",
"min":"11"
},
"vis":"9",
"wind":{
"deg":"358",
"dir":"北风",
"sc":"3-4",
"spd":"10"
}
},
{
"astro":{
"sr":"07:05",
"ss":"17:48"
},
"cond":{
"code_d":"101",
"code_n":"101",
"txt_d":"多云",
"txt_n":"多云"
},
"date":"2015-12-26",
"hum":"82",
"pcpn":"1.8",
"pop":"55",
"pres":"1022",
"tmp":{
"max":"18",
"min":"12"
},
"vis":"2",
"wind":{
"deg":"11",
"dir":"无持续风向",
"sc":"微风",
"spd":"4"
}
},
{
"astro":{
"sr":"07:06",
"ss":"17:49"
},
"cond":{
"code_d":"101",
"code_n":"305",
"txt_d":"多云",
"txt_n":"小雨"
},
"date":"2015-12-27",
"hum":"76",
"pcpn":"0.7",
"pop":"43",
"pres":"1022",
"tmp":{
"max":"18",
"min":"12"
},
"vis":"10",
"wind":{"
deg":"112",
"dir":"无持续风向",
"sc":"微风",
"spd":"1"
}
}
],
"hourly_forecast":[ 每小时天气预报
{
"date":"2015-12-21 16:00", 当地日期和时间
"hum":"70", 湿度
"pop":"3", 降水概率
"pres":"1021", 压力
"tmp":"23", 温度
"wind":{ 风力状况
"deg":"93", 风向(角度)
"dir":"东风", 风向(方向)
"sc":"微风", 风力等级
"spd":"6" 风速
}
},
{
"date":"2015-12-21 19:00",
"hum":"80",
"pop":"1",
"pres":"1021",
"tmp":"20",
"wind":{
"deg":"36",
"dir":"东北风",
"sc":"微风",
"spd":"3"
}
},
{
"date":"2015-12-21 22:00",
"hum":"85",
"pop":"1",
"pres":"1022",
"tmp":"18",
"wind":{
"deg":"55",
"dir":"东北风",
"sc":"微风",
"spd":"2"
}
}
],
"now":{ 现在天气
"cond":{ 天气状况
"code":"300", 天气代码
"txt":"阵雨" 天气描述
},
"fl":"18", 体感温度
"hum":"74", 湿度
"pcpn":"0", 降雨量
"pres":"1020", 压力
"tmp":"17", 温度
"vis":"7", 能见度
"wind":{ 风力状况
\ "deg":"70", 风向(角度)
"dir":"西风", 风向(方向)
"sc":"3-4", 风力等级
"spd":"11" 风速
}
},
"status":"ok", 状态
"suggestion":{" 建议
comf":{ 舒适度
"brf":"舒适", 简介
"txt":"白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。" 详情
},
"cw":{ 洗车指数
"brf":"不宜",
"txt":"不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"
},
"drsg":{ 穿衣指数 "brf":"较舒适",
"txt":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"
},
"flu":{ 感冒指数
"brf":"较易发",
"txt":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"
},
"sport":{ 运动指数
"brf":"较不宜",
"txt":"有降水,推荐您在室内进行各种健身休闲运动,若坚持户外运动,须注意保暖并携带雨具。"
},
"trav":{ 旅行指数
"brf":"适宜",
"txt":"温度适宜,又有较弱降水和微风作伴,会给您的旅行带来意想不到的景象,适宜旅游,可不要错过机会呦!"
},
"uv":{ 紫外线指数
"brf":"最弱",
"txt":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"
}
}
}
]
}
内容不少,不算太复杂,最外面是一个jsonObject,里面一个jsonArray的值,jsonArray里又有几个jsonObject,jsonArray。返回的信息分为返回结果(status),城市基本信息(basic),未来几日天气预报(daily_forecast),未来几小时天气预报(hourly_forecast),当前天气状态(now),建议(suggestion)。
使用jsonObejct解析数据
本次获取的json数据比较复杂,用jsonObject解析代码量会比较大,不过很直观,下面代码演示用jsonObject解析天气信息,一般来说,不会用jsonObject解析复杂的json数据,容易出错,代码量也大,代码如下。
package com.ykkj.goodweather.util;
import android.util.Log;
import com.ykkj.goodweather.model.WeatherInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 使用jsonObject解析天气数据
*/
public class JsonTool {
public static void parseJsonWithJsonObejct(String jsonData) throws JSONException {
JSONObject jsonObjWeatherInfo = getHead(jsonData);//获取jsonArray里的jsonObject
String status = jsonObjWeatherInfo.getString("status");//获取返回结果
if (status.equals("ok")){//返回结果为"ok"表示成功获取天气数据
//获取基本信息,基本信息为jsonObject
JSONObject jsonObjBasic = jsonObjWeatherInfo.getJSONObject(WeatherInfo.BASIC);
//获取未来七日天气预报,该数据为jsonArray
JSONArray jsonArrDailyForecast = jsonObjWeatherInfo.getJSONArray(WeatherInfo.DAILY_FORECAST);
//获取未来3小时天气预报,该数据为jsonArray
JSONArray jsonArrHourlyForecast = jsonObjWeatherInfo.getJSONArray(WeatherInfo.HOURLY_FORECAT);
//获取当前天气状况
JSONObject jsonObjNow = jsonObjWeatherInfo.getJSONObject(WeatherInfo.NOW);
//获取出行建议
JSONObject jsonObjSuggestion = jsonObjWeatherInfo.getJSONObject(WeatherInfo.SUGGESTION);
//解析基本信息
parseBasic(jsonObjBasic);
//解析未来7日天气预报
parseDailyForecast(jsonArrDailyForecast);
//解析未来3小时天气预报
parseHourlyForecast(jsonArrHourlyForecast);
//解析当前天气状况
parseNow(jsonObjNow);
//解析出行建议
parseSuggestion(jsonObjSuggestion);
//获取空气质量指数
JSONObject jsonObjAqi = jsonObjWeatherInfo.getJSONObject(WeatherInfo.AQI);
}
}
//解析基本信息
private static void parseBasic(JSONObject jsonObjBasic) throws JSONException {
String city = jsonObjBasic.getString(WeatherInfo.CITY);
String cnty = jsonObjBasic.getString(WeatherInfo.CNTY);
String id = jsonObjBasic.getString(WeatherInfo.ID);
String lat = jsonObjBasic.getString(WeatherInfo.LAT);
String lon = jsonObjBasic.getString(WeatherInfo.LON);
JSONObject jsonObjUpdate = jsonObjBasic.getJSONObject(WeatherInfo.UPDATE);
String loc = jsonObjUpdate.getString(WeatherInfo.LOC);
String utc = jsonObjUpdate.getString(WeatherInfo.UTC);
Log.d("json","城市基本信息 城市名字" + city + "\n 所属国家 " + cnty +
"\n 编号 " + id + "\n 经度 " + lat + "\n 纬度 " + lon +
"\n 更新时间 " + loc);
}
//解析未来7日天气预报
private static void parseDailyForecast(JSONArray jsonArrDailyForecast) throws JSONException {
for (int i = 0 ; i < jsonArrDailyForecast.length();i++){
JSONObject object = jsonArrDailyForecast.getJSONObject(i);
JSONObject jsonObjAstro = object.getJSONObject(WeatherInfo.ASTRO);
JSONObject jsonObjCnd = object.getJSONObject(WeatherInfo.COND);
JSONObject jsonObjTmp = object.getJSONObject(WeatherInfo.TMP);
JSONObject jsonObjWIND = object.getJSONObject(WeatherInfo.WIND);
String sunRaise = jsonObjAstro.getString(WeatherInfo.SR);
String sunShine = jsonObjAstro.getString(WeatherInfo.SS);
String codeDay = jsonObjCnd.getString(WeatherInfo.CODE_D);
String codeNight = jsonObjCnd.getString(WeatherInfo.CODE_N);
String textDay = jsonObjCnd.getString(WeatherInfo.TEXT_D);
String textNight = jsonObjCnd.getString(WeatherInfo.TEXT_N);
String date = object.getString(WeatherInfo.DATE);
String hum = object.getString(WeatherInfo.HUM);
String pcpn = object.getString(WeatherInfo.PCPN);
String pop = object.getString(WeatherInfo.POP);
String pres = object.getString(WeatherInfo.PRES);
String vis = object.getString(WeatherInfo.VIS);
String max = jsonObjTmp.getString(WeatherInfo.MAX );
String min = jsonObjTmp.getString(WeatherInfo.MIN);
String deg = jsonObjWIND.getString(WeatherInfo.DEG);
String dir = jsonObjWIND.getString(WeatherInfo.DIR);
String sc = jsonObjWIND.getString(WeatherInfo.SC);
String spd = jsonObjWIND.getString(WeatherInfo.SPD);
Log.d("json" ,date + " 天气信息 \n" + " 最高温度 "+ max + " 最低温度 " + min + "\n" +
" 湿度 " + hum + " 降水概率 " + pop + " 降雨量 " + pcpn + " 气压 " + pres + "\n" +
" 能见度 " + vis + "\n 日出时间 " + sunRaise + " 日落时间 " + sunShine + "\n"+
" \n 白天天气状况 " + textDay + " 晚上天气状况 " + textNight + "\n" +
" 风向 " + dir + " " + sc + " " + spd);
}
}
//解析未来3小时天气预报
private static void parseHourlyForecast(JSONArray jsonArrHourlyForecast) throws JSONException {
for (int i = 0 ;i < jsonArrHourlyForecast.length() ;i++){
JSONObject object = jsonArrHourlyForecast.getJSONObject(i);
JSONObject jsonObjWIND = object.getJSONObject(WeatherInfo.WIND);
String date = object.getString(WeatherInfo.DATE);
String hum = object.getString(WeatherInfo.HUM);
String pop = object.getString(WeatherInfo.POP);
String pres = object.getString(WeatherInfo.PRES);
String tmp = object.getString(WeatherInfo.TMP);
String deg = jsonObjWIND.getString(WeatherInfo.DEG);
String dir = jsonObjWIND.getString(WeatherInfo.DIR);
String sc = jsonObjWIND.getString(WeatherInfo.SC);
String spd = jsonObjWIND.getString(WeatherInfo.SPD);
Log.d("json" ,date + " 天气信息 \n" +
" 湿度 " + hum + " 降水概率 " + pop + " 气压 " + pres + "\n" +
" 风向 " + dir + " " + sc + " " + spd);
}
}
//解析当前天气状况
private static void parseNow(JSONObject jsonObjNow) throws JSONException {
JSONObject jsonObjCnd = jsonObjNow.getJSONObject(WeatherInfo.COND);
JSONObject jsonObjWIND = jsonObjNow.getJSONObject(WeatherInfo.WIND);
String text = jsonObjCnd.getString(WeatherInfo.TXT);
String code = jsonObjCnd.getString(WeatherInfo.CODE);
String feel = jsonObjNow.getString(WeatherInfo.FEEL);
String hum = jsonObjNow.getString(WeatherInfo.HUM);
String pcpn = jsonObjNow.getString(WeatherInfo.PCPN);
String pres = jsonObjNow.getString(WeatherInfo.PRES);
String vis = jsonObjNow.getString(WeatherInfo.VIS);
String tmp = jsonObjNow.getString(WeatherInfo.TMP);
String deg = jsonObjWIND.getString(WeatherInfo.DEG);
String dir = jsonObjWIND.getString(WeatherInfo.DIR);
String sc = jsonObjWIND.getString(WeatherInfo.SC);
String spd = jsonObjWIND.getString(WeatherInfo.SPD);
Log.d("json" ," 当前天气状态 \n" +
" 湿度 " + hum + " 降水量 " + pcpn + " 气压 " + pres + "\n" +
" 能见度 " + vis + " 温度 " + tmp +
"\n 天气状态 " + text + " 天气代码 " + code + " 体感温度 " + feel+
"\n 风向 " + dir + " " + sc + " " + spd);
}
//解析出行建议
private static void parseSuggestion(JSONObject jsonObjSuggestion) throws JSONException {
JSONObject jsonObjComf = jsonObjSuggestion.getJSONObject(WeatherInfo.COMF);
JSONObject jsonObjCw = jsonObjSuggestion.getJSONObject(WeatherInfo.CW);
JSONObject jsonObjDrag = jsonObjSuggestion.getJSONObject(WeatherInfo.DRSG);
JSONObject jsonObjFlu = jsonObjSuggestion.getJSONObject(WeatherInfo.FLU);
JSONObject jsonObjSport = jsonObjSuggestion.getJSONObject(WeatherInfo.SPORT);
JSONObject jsonObjTrav = jsonObjSuggestion.getJSONObject(WeatherInfo.TRAV);
JSONObject jsonObjUv = jsonObjSuggestion.getJSONObject(WeatherInfo.UV);
String comfBref = jsonObjComf.getString(WeatherInfo.BRF);
String comfTxt = jsonObjComf.getString(WeatherInfo.TXT);
String cwBref = jsonObjCw.getString(WeatherInfo.BRF);
String cwTxt = jsonObjCw.getString(WeatherInfo.TXT);
String dragBref = jsonObjDrag.getString(WeatherInfo.BRF);
String dragTxt = jsonObjDrag.getString(WeatherInfo.TXT);
String fluBref = jsonObjFlu.getString(WeatherInfo.BRF);
String fluTxt = jsonObjFlu.getString(WeatherInfo.TXT);
String sporBref = jsonObjSport.getString(WeatherInfo.BRF);
String sportTxt = jsonObjSport.getString(WeatherInfo.TXT);
String travBref = jsonObjTrav.getString(WeatherInfo.BRF);
String travTxt = jsonObjTrav.getString(WeatherInfo.TXT);
String uvBref = jsonObjUv.getString(WeatherInfo.BRF);
String uvTxt = jsonObjUv.getString(WeatherInfo.TXT);
Log.d("json","出行建议\n" + "舒适度 " + comfBref + " " + comfTxt + "\n" +
" 洗车指数 " + cwBref + " " + cwTxt + "\n" +
" 穿衣指数 " + dragBref + " " + dragTxt + "\n" +
" 感冒指数 " + fluBref + " " + fluTxt +" \n" +
" 运动指数 " + sporBref + " " + sportTxt + "\n" +
" 旅行指数 " + travBref + " " + travTxt + "\n" +
" 紫外线指数 " + uvBref + " " + uvTxt);
}
//获取jsonArray中的第一个jsonObject
private static JSONObject getHead(String jsonData) {
JSONObject result = null;
try {
JSONObject object = new JSONObject(jsonData);
JSONArray jsonArrWeatherInfo = object.getJSONArray(WeatherInfo.SERVICE_NAME);
result = jsonArrWeatherInfo.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
Log.e("err","err");
}
return result;
}
}
代码中将使用的字符串放到了一个类中,代码如下
package com.ykkj.goodweather.model;
/**
* Created by buniaowanfeng on 15-12-22.
*/
public class WeatherInfo {
public static final String SERVICE_NAME = "HeWeather data service 3.0";
public static final String AQI = "aqi";
public static final String BASIC = "basic";
public static final String DAILY_FORECAST = "daily_forecast";
public static final String HOURLY_FORECAT = "hourly_forecast";
public static final String NOW = "now";
public static final String SUGGESTION = "suggestion";
public static final String CITY = "city";
public static final String CNTY = "cnty";
public static final String ID = "id";
public static final String LAT = "lat";
public static final String LON = "lon";
public static final String UPDATE = "update";
public static final String LOC = "loc";
public static final String UTC = "utc";
public static final String ASTRO = "astro";
public static final String COND = "cond";
public static final String TMP = "tmp";
public static final String WIND = "wind";
public static final String SR = "sr";
public static final String SS = "ss";
public static final String CODE_D = "code_d";
public static final String CODE_N = "code_n";
public static final String TEXT_D = "txt_d";
public static final String TEXT_N = "txt_n";
public static final String DATE = "date";
public static final String HUM = "hum";
public static final String PCPN = "pcpn";
public static final String POP = "pop";
public static final String PRES = "pres";
public static final String MAX = "max";
public static final String MIN = "min";
public static final String VIS = "vis";
public static final String DEG = "deg";
public static final String DIR = "dir";
public static final String SC = "sc";
public static final String SPD = "spd";
public static final String TXT = "txt";
public static final String CODE = "code";
public static final String FEEL = "fl";
public static final String COMF = "comf";
public static final String CW = "cw";
public static final String DRSG ="drsg";
public static final String FLU = "flu";
public static final String SPORT = "sport";
public static final String TRAV = "trav";
public static final String UV = "uv";
public static final String BRF = "brf";
}