该程序在上一个程序的基础上 改进 添加了获取天气状态
该程序调用中央气象局API 有多种数据:
简单的实时天气json 数据
包含详细的天气预报信息
后者 如下例子:
{"weatherinfo":{
"city":"武汉",
"city_en":"wuhan",
"date_y":"2012年4月20日",
"date":"",
"week":"星期五","fchh":"11",
"cityid":"101200101",
"temp1":"21℃~12℃",
"temp2":"27℃~14℃",
"temp3":"28℃~17℃",
"temp4":"30℃~19℃",
"temp5":"32℃~22℃",
"temp6":"29℃~13℃",
"tempF1":"69.8℉~53.6℉",
"tempF2":"80.6℉~57.2℉",
"tempF3":"82.4℉~62.6℉",
"tempF4":"86℉~66.2℉",
"tempF5":"89.6℉~71.6℉",
"tempF6":"84.2℉~55.4℉",
"weather1":"阵雨转多云",
"weather2":"多云",
"weather3":"多云",
"weather4":"阴转阵雨",
"weather5":"小雨转阴",
"weather6":"阴",
"img1":"3",
"img2":"1",
"img3":"1",
"img4":"99",
"img5":"1",
"img6":"99",
"img7":"2",
"img8":"3",
"img9":"7",
"img10":"2",
"img11":"2",
"img12":"99",
"img_single":"3",
"img_title1":"阵雨",
"img_title2":"多云",
"img_title3":"多云",
"img_title4":"多云",
"img_title5":"多云",
"img_title6":"多云",
"img_title7":"阴",
"img_title8":"阵雨",
"img_title9":"小雨",
"img_title10":"阴",
"img_title11":"阴",
"img_title12":"阴",
"img_title_single":"阵雨",
"wind1":"微风",
"wind2":"微风",
"wind3":"微风",
"wind4":"微风",
"wind5":"微风",
"wind6":"微风",
"fx1":"微风",
"fx2":"微风",
"fl1":"小于3级",
"fl2":"小于3级",
"fl3":"小于3级",
"fl4":"小于3级",
"fl5":"小于3级",
"fl6":"小于3级",
"index":"舒适",//穿衣指数
"index_d":"建议着薄型套装或牛仔衫裤等春秋过渡装。年老体弱者宜着套装、夹克衫等。",
"index48":"舒适",
"index48_d":"建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减服。",
"index_uv":"弱",//紫外线指数
"index48_uv":"弱",//紫外线指数
"index_xc":"不宜", //洗车指数
"index_tr":"适宜", //旅游指数
"index_co":"舒适", //舒适指数
"st1":"17",
"st2":"10",
"st3":"28",
"st4":"15",
"st5":"29",
"st6":"17",
"index_cl":"较不宜", //晨练指数
"index_ls":"不太适宜", //晾晒指数
"index_ag":"极易发" //息斯敏过敏气象指数
}
}
img1~ img12数字表示 见下表:
编号 名称
0 晴
1 多云
2 阴
3 阵雨
4 雷阵雨
5 雷阵雨伴有冰雹
6 雨夹雪
7 小雨
8 中雨
9 大雨
10 暴雨
11 大暴雨
12 特大暴雨
13 阵雪
14 小雪
15 中雪
16 大雪
17 暴雪
18 雾
19 冻雨
20 沙尘暴
21 小雨-中雨
22 中雨-大雨
23 大雨-暴雨
24 暴雨-大暴雨
25 大暴雨-特大暴雨
26 小雪-中雪
27 中雪-大雪
28 大雪-暴雪
29 浮尘
30 扬沙
31 强沙尘暴
99 不变
前者例子:
{"weatherinfo"{"city":"北京",
"cityid":"101010100",//城市编号
"temp":"14",//实时气温
"WD":"东风",//风向
"WS":"级", //单位
"SD":"71%",//湿度
"WSE":"2", //风级
"time":"13:00",
"isRadar":"1",
"Radar":"JC_RADAR_AZ9010_JB"
}
}
只是数据不同 实现完全相同 简单起见 使用后者进行解析
1配置文件 在前篇博客的基础上加入互联网权限
<uses-permission Android:name="android.permission.INTERNET"/>
2 WeatherInfo.java WeatherModel.java
package com.cityselection;
public class WeatherModel {
private WeatherInfo weatherinfo =new WeatherInfo();
public WeatherInfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(WeatherInfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
package com.cityselection;