平时我们在开发的过程中有时会要用到天气预报的信息,国家气象局为我们提供了天气预报的接口,只需要我们去解析就行了。很方便很好用
那么下面,我们开始吧!
国家气象局提供了三种数据的形式
网址在:
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
http://m.weather.com.cn/data/101010100.html
最后一种是解析最全面的。
这里再附上中国天气网城市代码大全(百度文库里面的),把后面的城市代码换下就行了:
http://wenku.baidu.com/view/ea286102bb68a98271fefad8.html
数据解析格式:
第一个网址提供的json数据为:
第二个网址提供的json数据为:
第三个网址提供的json数据较为全面:
{
"weatherinfo":
{
"city":"北京",
"city_en":"beijing",
"date_y":"2013年1月17日",
"date":"",
"week":"星期四",
"fchh":"11",
"cityid":"101010100",
"temp1":"3℃~-8℃",
"temp2":"3℃~-5℃",
"temp3":"3℃~-3℃",
"temp4":"1℃~-5℃",
"temp5":"3℃~-6℃",
"temp6":"2℃~-5℃",
"tempF1":"37.4℉~17.6℉",
"tempF2":"37.4℉~23℉",
"tempF3":"37.4℉~26.6℉",
"tempF4":"33.8℉~23℉",
"tempF5":"37.4℉~21.2℉",
"tempF6":"35.6℉~23℉",
"weather1":"晴",
"weather2":"晴",
"weather3":"多云转阴",
"weather4":"阴转多云",
"weather5":"多云转晴",
"weather6":"晴转多云",
"img1":"0",
"img2":"99",
"img3":"0",
"img4":"99",
"img5":"1",
"img6":"2",
"img7":"2",
"img8":"1",
"img9":"1",
"img10":"0",
"img11":"0",
"img12":"1",
"img_single":"0",
"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":"2",
"st2":"-6",
"st3":"2",
"st4":"-3",
"st5":"3",
"st6":"-4",
"index_cl":"适宜",
"index_ls":"基本适宜",
"index_ag":"极不易发"
}
}
详细解析格式如下:
以json格式提供,格式如下 {"weatherinfo":{
//基本信息;
"city":"北京","city_en":"beijing",
"date_y":"2013年1月17日","date":"辛卯年","week":"星期三","fchh":"18","cityid":"101010100",
//摄氏温度
"temp1":"24℃~33℃","temp2":"24℃~32℃","temp3":"25℃~31℃","temp4":"23℃~30℃","temp5":"22℃~30℃","temp6":"21℃~32℃",
//华氏温度;
"tempF1":"75.2℉~91.4℉","tempF2":"75.2℉~89.6℉","tempF3":"77℉~87.8℉","tempF4":"73.4℉~86℉","tempF5":"71.6℉~86℉","tempF6":"69.8℉~89.6℉",
//天气描述;
"weather1":"多云","weather2":"晴转多云","weather3":"阴","weather4":"晴转阵雨","weather5":"阵雨","weather6":"多云",
//天气描述图片序号
"img1":"1","img2":"99","img3":"0","img4":"1","img5":"2","img6":"99","img7":"0","img8":"3","img9":"3","img10":"99","img11":"1","img12":"99",
"img_single":"1",
//图片名称;
"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":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。",
//48小时穿衣指数
"index48":"炎热","index48_d":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。"
//紫外线及48小时紫外线
,"index_uv":"中等","index48_uv":"弱",
//洗车
"index_xc":"适宜",
//旅游
"index_tr":"较适宜",
//舒适指数
"index_co":"较不舒适",
//
"st1":"33","st2":"24","st3":"32","st4":"25","st5":"32","st6":"24",
//晨练
"index_cl":"适宜",
//晾晒
"index_ls":"适宜",
//过敏
"index_ag":"极易发"}}
字段言简意赅,城市名,英文名,日期,农历日期,星期,预报时间,城市代码,6个预报温度(华氏温度,摄氏温度),6个预报天气和风力,剩下的就是一些气象指数。
之前有朋友提到过,希望把各个城市的城市代码也整理一下,这样的话,可以跟方便的使用,经过查找,在百度文库里面找到一篇详细的城市代码。大家可以去查看,地址如下,祝大家使用愉快:
http://wenku.baidu.com/view/ea286102bb68a98271fefad8.html
本文实例所述为Android天气预报之解析天气数据的代码,可实现获取HttpGet对象读取天气网站天气数据,并从数据中解析出天气数据,比如温度、温度、风力、风向、未来几天天气趋势、当天天气状况、空气污染指数等信息,还包括了调用对应的图片或天气动画文件,对于开发android天气预报程序的可以参考本文实例。
具体功能代码如下:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
import
java.io.IOException;
import
java.text.SimpleDateFormat;
import
java.util.ArrayList;
import
java.util.List;
import
java.util.Locale;
import
net.tsz.afinal.FinalHttp;
import
net.tsz.afinal.http.AjaxCallBack;
import
org.apache.http.HttpResponse;
import
org.apache.http.HttpStatus;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.util.EntityUtils;
import
org.json.JSONException;
import
org.json.JSONObject;
import
org.lmw.weather.MyApp;
import
org.lmw.weather.entity.WeatherEntity;
import
android.app.Activity;
import
android.os.Handler;
import
android.os.Message;
/**
* 解析天气数据
* @author Dave
*/
public
class
WeatherData {
private
Activity activity;
private
FinalHttp fh;
public
static
String def_weather_key=
"def_weather"
;
public
WeatherData(Activity activity) {
this
.activity = activity;
fh =
new
FinalHttp();
fh.configTimeout(
1000
*
3
);
}
public
void
getData(
final
String cityId,
final
Handler hd) {
StringBuffer sb_url =
new
StringBuffer();
sb_url.append(
"http://0.qnweather.duapp.com/weather.php?uri="
);
sb_url.append(
"http://m.weather.com.cn/data/"
);
sb_url.append(cityId);
sb_url.append(
".html"
);
final
Message msg=
new
Message();
fh.get(sb_url.toString(),
new
AjaxCallBack() {
@Override
public
void
onSuccess(Object t) {
super
.onSuccess(t);
MySharedPreferences.writeMessage(activity,
"def_weather"
,t.toString());
msg.what=
0
;
msg.obj=parseJson(t.toString());
hd.sendMessage(msg);
}
@Override
public
void
onFailure(Throwable t,
int
errorNo, String strMsg) {
super
.onFailure(t, errorNo, strMsg);
System.out.println(
"-------errorNo---------"
+errorNo);
msg.what=-
1
;
msg.arg1=errorNo;
msg.obj=MySharedPreferences.readMessage(activity, def_weather_key,
""
);
hd.sendMessage(msg);
}
});
}
private
String connServerForResult(String strUrl) {
// 获取HttpGet对象
HttpGet httpRequest =
new
HttpGet(strUrl);
String strResult =
""
;
try
{
// HttpClient对象
HttpClient httpClient =
new
DefaultHttpClient();
// 获得HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpRequest);
if
(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(Exception e) {
e.printStackTrace();
}
System.out.println(
"rresult"
+ strResult);
return
strResult;
// 返回结果
}
// 数据解析
private
WeatherEntity parseJson(String strResult) {
WeatherEntity weather =
null
;
try
{
JSONObject jsonObj =
new
JSONObject(strResult.replace(
"℃"
,
"°"
))
.getJSONObject(
"weatherinfo"
);
weather =
new
WeatherEntity();
int
ftime = jsonObj.getInt(
"fchh"
);
// 更新时间(整点)【更新时间确定temp属于哪天】
int
temp =
0
;
// 偏移
if
(ftime >=
18
|| ftime <
8
) {
weather.setNight(
true
);
temp =
1
;
}
MyApp.week = jsonObj.getString(
"week"
);
// 今天星期几
weather.setCity(jsonObj.getString(
"city"
));
// 城市
weather.setComfortable(jsonObj.getString(
"index"
));
// 舒适度
weather.setRefreshDate(getDate());
// 更新日期
weather.setRefreshTime(getTime());
// 更新时间
weather.setRefreshWeek(getWeek());
// 更新星期
weather.setPicIndex(jsonObj.getInt(
"img1"
));
// 当天天气图片编号
List
new
ArrayList
// 最高温时的图片编号
if
(temp ==
1
) {
topPic.add(getSavePic(activity));
}
else
{
topPic.add(getJsonPic(jsonObj,
"img"
,
1
+ temp));
savePic(activity, topPic.get(
0
));
}
topPic.add(getJsonPic(jsonObj,
"img"
,
3
- temp));
topPic.add(getJsonPic(jsonObj,
"img"
,
5
- temp));
topPic.add(getJsonPic(jsonObj,
"img"
,
7
- temp));
weather.setTopPic(topPic);
List
new
ArrayList
// 最低温时的图片编号
lowPic.add(getJsonPic(jsonObj,
"img"
,
2
- temp));
lowPic.add(getJsonPic(jsonObj,
"img"
,
4
- temp));
lowPic.add(getJsonPic(jsonObj,
"img"
,
6
- temp));
lowPic.add(getJsonPic(jsonObj,
"img"
,
8
- temp));
weather.setLowPic(lowPic);
// ---------------------------以上为获取图片编号,暂且不管----------------------------------------------------------------------
List
new
ArrayList
// 未来五天温度(第一个是今天)
tempList.add(jsonObj.getString(
"temp1"
));
tempList.add(jsonObj.getString(
"temp2"
));
tempList.add(jsonObj.getString(
"temp3"
));
tempList.add(jsonObj.getString(
"temp4"
));
tempList.add(jsonObj.getString(
"temp5"
));
tempList.add(jsonObj.getString(
"temp6"
));
MyApp.tempList.clear();
MyApp.tempList = tempList;
List
new
ArrayList
// 未来五天天气(第一个是今天)
weatherList.add(jsonObj.getString(
"weather1"
));
weatherList.add(jsonObj.getString(
"weather2"
));
weatherList.add(jsonObj.getString(
"weather3"
));
weatherList.add(jsonObj.getString(
"weather4"
));
weatherList.add(jsonObj.getString(
"weather5"
));
weatherList.add(jsonObj.getString(
"weather6"
));
MyApp.weatherList.clear();
MyApp.weatherList = weatherList;
List
new
ArrayList
// 未来五天最高温度集合(有°符号)
if
(temp ==
1
) {
tempListMax.add(getSaveTemperature(activity));
}
else
{
tempListMax
.add(getTemperatureMaxAndMin(tempList.get(
0
))[
0
+ temp]);
saveTemperature(activity, tempListMax.get(
0
));
}
tempListMax
.add(getTemperatureMaxAndMin(tempList.get(
1
- temp))[
0
+ temp]);
tempListMax
.add(getTemperatureMaxAndMin(tempList.get(
2
- temp))[
0
+ temp]);
tempListMax
.add(getTemperatureMaxAndMin(tempList.get(
3
- temp))[
0
+ temp]);
weather.setTemperatureMax(tempListMax);
weather.setTodayTemperature(getTemperatureMaxAndMin(tempList.get(
0
))[
0
]);
// 当天温度(实时)
weather.setTodayWeather(jsonObj.getString(
"img_title1"
));
// 当天天气描述(实时)
List
new
ArrayList
// 未来四天最低温度集合(有°符号)
tempListMin.add(getTemperatureMaxAndMin(tempList.get(
0
))[
1
- temp]);
tempListMin.add(getTemperatureMaxAndMin(tempList.get(
1
))[
1
- temp]);
tempListMin.add(getTemperatureMaxAndMin(tempList.get(
2
))[
1
- temp]);
tempListMin.add(getTemperatureMaxAndMin(tempList.get(
3
))[
1
- temp]);
weather.setTemperatureMin(tempListMin);
weather.setTomorrowTemperature(tempList.get(
1
));
// 明天温度(包括最高温和最低温)
if
(temp ==
1
) {
weatherList.add(getSaveWeather(activity));
}
else
{
weatherList.add(jsonObj.getString(
"weather"
+
1
));
saveWeather(activity, weatherList.get(
0
));
}
weatherList.add(jsonObj.getString(
"weather"
+ (
2
- temp)));
weatherList.add(jsonObj.getString(
"weather"
+ (
3
- temp)));
weatherList.add(jsonObj.getString(
"weather"
+ (
4
- temp)));
weather.setWeather(weatherList);
weather.setTomorrowWeather(weatherList.get(
1
));
List
new
ArrayList
// 未来四天风力
windList.add(jsonObj.getString(
"wind1"
));
windList.add(jsonObj.getString(
"wind2"
));
windList.add(jsonObj.getString(
"wind3"
));
windList.add(jsonObj.getString(
"wind4"
));
weather.setWind(windList);
weather.setMaxlist(transplate(tempListMax));
// 未来四天最高温度集合(无°符号)
weather.setMinlist(transplate(tempListMin));
// 未来四天最低温度集合(无°符号)
}
catch
(JSONException e) {
e.printStackTrace();
}
return
weather;
}
// 获取更新日期 并转换为(X月X日 周X)
private
String getDate() {
SimpleDateFormat sdf =
new
SimpleDateFormat(
"MM月dd日 EEE"
, Locale.CHINA);
String date = sdf.format(
new
java.util.Date());
System.out.println(date);
return
date;
}
// 获取更新时间 并转换为 (小时:分钟 更新)
private
String getTime() {
SimpleDateFormat sdf =
new
SimpleDateFormat(
"HH:mm"
, Locale.CHINA);
String time = sdf.format(
new
java.util.Date()) +
" "
+
"更新"
;
System.out.println(time);
return
time;
}
private
String getWeek() {
return
null
;
}
// 获取最高温度和最低温度,有°符号
private
String[] getTemperatureMaxAndMin(String str) {
return
str.split(
"~"
);
}
// 去除最高温度和最低温度里的°符号
private
List
List
new
ArrayList
for
(String temp : strList) {
intList.add(Integer.valueOf(temp.split(
"°"
)[
0
]));
}
return
intList;
}
// 获取图片编号 例如"img" + "1"
private
int
getJsonPic(JSONObject jsonObj, String str,
int
index)
throws
JSONException {
int
result = jsonObj.getInt(str + index);
if
(result ==
99
&& index >
1
) {
index--;
result = jsonObj.getInt(str + index);
}
return
result;
}
private
void
saveTemperature(Activity activity, String value) {
// MySharedPreferences mp = new MySharedPreferences(activity);
// mp.writeMessage("temperature", value);
}
// 保存的温度
private
String getSaveTemperature(Activity activity) {
return
MySharedPreferences.readMessage(activity,
"temperature"
,
"100"
);
}
private
void
saveWeather(Activity activity, String value) {
// MySharedPreferences mp = new MySharedPreferences(activity);
// mp.writeMessage("weather", value);
}
// 保存的天气
private
String getSaveWeather(Activity activity) {
return
MySharedPreferences.readMessage(activity,
"weather"
,
""
);
}
private
void
savePic(Activity activity,
int
value) {
// MySharedPreferences mp = new MySharedPreferences(activity);
// mp.writeMessage("pic", value);
}
// 保存的天气图片编号
private
int
getSavePic(Activity activity) {
return
MySharedPreferences.readMessage(activity,
"pic"
,
99
);
}
}
|