獲取天氣預報的例子,參考安卓學習手冊,裏面有源代碼。android學習手冊包含9個章節和108個示例。隨便看看源碼文檔,例子都是有交互性和可操作性的。源代碼采用android studio的目錄結構,代碼高亮顯示。文檔都是通過文檔結構圖顯示的,可以快速定位。從360手機助手下載,圖標上有殼。
中國國家氣象局的天氣預報界面總是提供三種:
/data/sk/101010100 . html
/data/city info/101010100 . html
/data/101010100 . html
註:以上界面2014.3.4不再更新。換成這個:/Atad/101230201 . html看34樓。感謝tdwll和黃曉佳·科比什。
最詳細的信息來自第三個界面。在上面的url中,101010100是城市代碼,這裏是北京的城市代碼。只需更改城市代碼,您就可以獲得您所在城市的天氣信息。作者在福州,所以選擇的城市代碼是福州101230101。
在瀏覽器中輸入URL:/data/101230101 . html獲取信息。天氣信息為json數據格式,數據如下:
{"weatherinfo":{"city ":"福州"," city _ en ":"福州"," date _ y ":"2012 May 14 "," date ":"星期":"星期壹。"city id ":" 101230101 "," temp1":"29℃~23 "," temp2":"26℃~20 "," temp3":"24℃~20 "," temp4":"25℃~20 "," temp5":"24℃~21 "," temp 6 ":" 25℃~ 25 "img1":"3 "," img2":"8 "," img3":"8 "," img4":"7 "," img5":"7 "," img6":"99 "," img7":"7 "," img8":"99 "," img9":"7 "," img10":"3 "," img11":"3 "," img 12": "7 "," 7 "" img_title7 ":"小雨"," img_title8 ":"小雨"," img_title9 ":"小雨"," img_title10 ":"淋浴"," img_title11 ":"淋浴。"img_title_single ":"陣雨","風1 ":"微風","風2 ":"微風","風3 ":"微風","風4 ":"微風","風5 ":"微風","風6 ":"fl1 ":"小於3級"," fl2 ":"小於3級"," fl3 ":"小於3級"," fl4 ":"小於3級"," fl5 ":"小於3級"," fl6 ":"小於3級","索引。年老體弱者應穿長袖襯衫和長褲。"、" index48 ":"保暖"、" index48_d ":"天氣較涼,春秋過渡建議穿長袖襯衫和長褲。年老體弱者應穿針織長袖襯衫、背心和長褲。"、" index_uv ":"弱"、" index48_uv ":"最弱"、" index_xc ":"不適合"、" index_tr ":"適合"、" index_co ":"不太舒服"、" ST 1 ":" st5 ":" 22 "、" st6": "18 "、" index _ cl ":"不適合"、" index_ls ":"不適合"、" index_ag ":"不好發" }}
我們可以解析json數據來獲得我們想要使用的天氣信息。
天氣信息的解釋:
[html]查看純文本打印?
{?
" weatherinfo":{?
& lt!- ?基本信息?-& gt;?
“城市”:“福州”
“city_en”:“福州”,?
" date_y":"2012 May 14 "?
"日期":"",?
“星期”:“星期壹”
" fchh":"08 ",
“city id”:“101230101”,?
& lt!- ?今天到初六每天的天氣怎麽樣?這裏的溫度是多少?-& gt;?
" temp1":"29℃~23℃"," temp2":"26℃~20℃," temp3":"24℃~20℃," temp4":"25℃~20℃," temp5":"24℃~21℃," temp6":"25℃~22℃",?
& lt!- ?今天到初六每天的天氣怎麽樣?這裏的溫度是多少?-& gt;?
" tempF1":"84.2華氏度~73.4華氏度"," tempF2":"78.8華氏度~68華氏度"," tempF3":"75.2華氏度~68華氏度"," tempF4":"77華氏度~68華氏度"," tempF5":"75.2華氏度~69.8華氏度"," tempF6":"77華氏度
& lt!- ?天氣描述?-& gt;?
" weather1 ":"陣雨轉中雨"," weather2 ":"中雨轉小雨"," weather3 ":"小雨"," weather4 ":"小雨"," weather5 ":"小雨轉陣雨"," weather6 ":"小雨轉小雨",
& lt!- ?天氣描述圖片號?-& gt;?
" img1":"3 "," img2":"8 "," img3":"8 "," img4":"7 "," img5":"7 "," img6":"99 "," img7":"7 "," img8":"99 "," img9":"7 "," img10":"3 "," img11":"3 "," img12":"7 "," 7 "
& lt!- ?圖片名稱?-& gt;?
" img_title1 ":"陣雨"," img_title2 ":"中雨"," img_title3 ":"中雨"," img_title4 ":"小雨"," img_title5 ":"小雨"," img_title6 ":。" img_title9 ":"小雨"," img_title10 ":"淋浴"," img_title11 ":"淋浴"," img_title12 ":"小雨"," img_title "。
& lt!- ?風速描述?-& gt;?
" wind1 ":"微風"," wind2 ":"微風"," wind3 ":"微風"," wind4 ":"微風"," wind5 ":"微風"," wind6 ":"微風"," fx1 ":。
& lt!- ?風速等級描述?-& gt;?
" fl1 ":"小於3級"," fl2 ":"小於3級"," fl3 ":"小於3級"," fl4 ":"小於3級"," fl5 ":"小於3級"," fl6 ":"小於3級",
& lt!- ?今天穿衣指數?-& gt;?
“指數”:“熱點”
" index_d ":"天氣炎熱,建議短裙、短褲、短西裝、t恤等夏裝。年老體弱者應穿長袖襯衫和長褲。”,?
& lt!- ?48小時。穿衣指數?-& gt;?
" index48 ":"保暖"," index48_d ":"天氣較涼,春秋過渡建議穿長袖襯衫和長褲。年老體弱者應穿針織長袖襯衫、背心和長褲。”,?
& lt!- ?紫外線和48小時紫外線?-& gt;?
" index_uv ":"弱"," index48_uv ":"最弱"?
& lt!- ?洗車?-& gt;?
" index_xc ":"不適合"?
& lt!- ?旅行?-& gt;?
" index_tr ":"適當",
& lt!- ?舒適指數?-& gt;?
“index_co”:“不太舒服”?
" st1":"27 "," st2":"21 "," st3":"24 "," st4":"18 "," st5":"22 "," st6":"18 ",
& lt!- ?晨練?-& gt;?
" index_cl ":"不適合"?
& lt!- ?風幹?-& gt;?
" index_ls ":"不適合"?
& lt!- ?過敏?-& gt;?
" index_ag ":"不好發"?
}?
}?
{
"天氣信息":{
& lt!-基本信息-& gt;
“城市”:“福州”,
“city_en”:“福州”,
" date_y":"2012 May 14 ",
"日期":"",
「星期」:「星期壹」,
" fchh":"08 ",
“city id”:“101230101”,
& lt!-今天到第六天的每日天氣,這裏的溫度是攝氏度-& gt;
" temp1":"29℃~23℃"," temp2":"26℃~20℃," temp3":"24℃~20℃," temp4":"25℃~20℃," temp5":"24℃~21℃," temp6":"25℃~22℃",
& lt!-今天到第六天的每日天氣,這裏的溫度是華氏-& gt;
" tempF1":"84.2華氏度~73.4華氏度"," tempF2":"78.8華氏度~68華氏度"," tempF3":"75.2華氏度~68華氏度"," tempF4":"77華氏度~68華氏度"," tempF5":"75.2華氏度~69.8華氏度"," tempF6":"77華氏度
& lt!-天氣描述->;
" weather1 ":"陣雨轉中雨"," weather2 ":"中雨轉小雨"," weather3 ":"小雨"," weather4 ":"小雨"," weather5 ":"小雨轉陣雨"," weather6 ":"小雨轉小雨",
& lt!-天氣描述圖片的序列號-& gt;
" img1":"3 "," img2":"8 "," img3":"8 "," img4":"7 "," img5":"7 "," img6":"99 "," img7":"7 "," img8":"99 "," img9":"7 "," img10":"3 "," img11":"3 "," img12":"7 "," 7 "
& lt!-圖片名稱-& gt;
" img_title1 ":"陣雨"," img_title2 ":"中雨"," img_title3 ":"中雨"," img_title4 ":"小雨"," img_title5 ":"小雨"," img_title6 ":。" img_title9 ":"小雨"," img_title10 ":"淋浴"," img_title11 ":"淋浴"," img_title12 ":"小雨"," img_title "。
& lt!-風速描述-& gt;
" wind1 ":"微風"," wind2 ":"微風"," wind3 ":"微風"," wind4 ":"微風"," wind5 ":"微風"," wind6 ":"微風"," fx1 ":。
& lt!-風速等級描述-& gt;
" fl1 ":"小於3級"," fl2 ":"小於3級"," fl3 ":"小於3級"," fl4 ":"小於3級"," fl5 ":"小於3級"," fl6 ":"小於3級",
& lt!-今日穿衣指數-& gt;
「指數」:「熱門」,
" index_d ":"天氣炎熱,建議短裙、短褲、短西裝、t恤等夏裝。年老體弱者應穿長袖襯衫和長褲。”,
& lt!-48小時穿衣指數-& gt;
" index48 ":"保暖"," index48_d ":"天氣較涼,春秋過渡建議穿長袖襯衫和長褲。年老體弱者應穿針織長袖襯衫、背心和長褲。”,
& lt!-紫外線和48小時紫外線-& gt;
" index_uv ":"弱"," index48_uv ":"最弱",
& lt!-洗車-& gt;
" index_xc ":"不適合",
& lt!-旅遊->;
" index_tr ":"適當",
& lt!-舒適指數->;
" index_co ":"不太舒服",
" st1":"27 "," st2":"21 "," st3":"24 "," st4":"18 "," st5":"22 "," st6":"18 ",
& lt!-早操-& gt;
" index_cl ":"不適合",
& lt!-正在播放->;
" index_ls ":"不適合",
& lt!-過敏->;
" index_ag ":"不好發"
}
}