? ? ?前面一篇文章介紹了如何使用ksoap獲取天氣信息,但是使用的網絡資源受到了限制,所以我們這里會采用第二種方法,可以無限制的獲取。http://m.weather.com.cn/data/101010100.html?但是對應的101010100(北京)我們怎么獲取呢,還有就是圖片資源怎么來的呢?http://m.weather.com.cn/img/b1.gif這個是圖片資源,但是每次從網上去還是比較費流量的,我仔細對比了Ksoap中給的gif圖片資源,和中國氣象局的這個圖片都是一一對應的,所以這里我會做成本地圖片。
?
{"weatherinfo":{<!-- 基本信息 -->"city":"北京","city_en":"北京","date_y":"2013年5月14日","date":"","week":"星期一","fchh":"08","cityid":"101010100",<!-- 從今天開始到第六天的每天的天氣情況,這里的溫度是攝氏溫度 -->"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",<!-- 從今天開始到第六天的每天的天氣情況,這里的溫度是華氏溫度 -->"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",<!-- 天氣描述 -->"weather1":"陣雨轉中雨","weather2":"中雨轉小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨轉陣雨","weather6":"陣雨轉小雨",<!-- 天氣描述圖片序號 -->"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","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":"天氣較熱,建議著短裙、短褲、短套裝、T恤等夏季服裝。年老體弱者宜著長袖襯衫和單褲。",<!-- 48小時穿衣指數 -->"index48":"暖","index48_d":"較涼爽,建議著長袖襯衫加單褲等春秋過渡裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",<!-- 紫外線及48小時紫外線 -->"index_uv":"弱","index48_uv":"最弱",<!-- 洗車 -->"index_xc":"不宜",<!-- 旅游 -->"index_tr":"適宜",、<!-- 舒適指數 -->"index_co":"較不舒適","st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",<!-- 晨練 -->"index_cl":"較不宜",<!-- 晾曬 -->"index_ls":"不太適宜",<!-- 過敏 -->"index_ag":"不易發"}
}
下面我主要講下程序: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
1.1城市代碼獲取 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? 這里我把下載下來的城市代碼的空行給去掉了,把文件保存為txt格式(UTF-8另存為可以看見)。下載地址:http://download.csdn.net/detail/feiyangxiaomi/6261685程序中的讀取方法為:
?
/**************************************************************************************** 注意在讀入txt的時候是UTF-8,自己看好自己的txt文本格式,在另存為就可以看出來。*/
private Map<String,String> cityCodes; //根據城市信息索引自己的code
private List<String> citys; //給城市做數據源
private void getAssetsContent(){try {String buf;citys = new ArrayList<String>();cityCodes = new HashMap<String, String>();InputStream input = this.getAssets().open("cityCode.txt");BufferedReader br = new BufferedReader(new InputStreamReader(input,"UTF-8"));while((buf = br.readLine())!=null){String[] codeCity = buf.split("=");citys.add(codeCity[1]);cityCodes.put(codeCity[1], codeCity[0]);}} catch (IOException e) {// TODO Auto-generated catch blockLog.i(TAG, e.toString());e.printStackTrace();}
}
在使用的時候直接索引對應的城市即可。文件夾放在assets目錄下,為不受編譯才部分。
?
1.2網絡數據的使用 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?
private void refreshUI(JSONObject jsonobject){JSONObject jsonData = jsonobject;try{TextView today_text = (TextView) findViewById(R.id.today);today_text.setText(jsonData.getString("date_y"));TextView city_text = (TextView) findViewById(R.id.city_text);city_text.setText(jsonData.getString("city"));TextView today_weather = (TextView) findViewById(R.id.today_weather);today_weather.setText(jsonData.getString("weather1"));// 取得<string>15℃/21℃</string>中的數據TextView qiweng_text = (TextView) findViewById(R.id.qiweng);qiweng_text.setText(jsonData.getString("temp1"));// 取得<string>今日天氣風速情況TextView shidu_text = (TextView) findViewById(R.id.shidu);shidu_text.setText(jsonData.getString("wind1"));// 取得<string>東北風3-4級</string>中的數據TextView fengli_text = (TextView) findViewById(R.id.fengli);fengli_text.setText(jsonData.getString("fl1"));// 取得<string>舒適指數和紫外線強度TextView kongqi_text = (TextView) findViewById(R.id.kongqi);kongqi_text.setText(jsonData.getString("index_co"));TextView zhiwai_text = (TextView) findViewById(R.id.zhiwai);zhiwai_text.setText(jsonData.getString("index_uv"));// 設置小貼士數據TextView xiaotieshi_text = (TextView) findViewById(R.id.xiaotieshi);xiaotieshi_text.setText("今日小貼士:"+jsonData.getString("index_d"));// 設置當日圖片ImageView image = (ImageView) findViewById(R.id.imageView1);int icon = parseIcon(jsonData.getString("img1")+".gif");image.setImageResource(icon);// 取得第二天的天氣情況TextView tomorrow_date = (TextView) findViewById(R.id.tomorrow_date);tomorrow_date.setText(jsonData.getString("weather2"));TextView tomorrow_qiweng = (TextView) findViewById(R.id.tomorrow_qiweng);tomorrow_qiweng.setText(jsonData.getString("temp2"));TextView tomorrow_tianqi = (TextView) findViewById(R.id.tomorrow_tianqi);tomorrow_tianqi.setText(jsonData.getString("wind2"));ImageView tomorrow_image = (ImageView) findViewById(R.id.tomorrow_image);int icon1 = parseIcon(jsonData.getString("img3")+".gif");tomorrow_image.setImageResource(icon1);// 取得第三天的天氣情況TextView afterday_date = (TextView) findViewById(R.id.afterday_date);afterday_date.setText(jsonData.getString("weather3"));TextView afterday_qiweng = (TextView) findViewById(R.id.afterday_qiweng);afterday_qiweng.setText(jsonData.getString("temp3"));TextView afterday_tianqi = (TextView) findViewById(R.id.afterday_tianqi);afterday_tianqi.setText(jsonData.getString("wind3"));ImageView afterday_image = (ImageView) findViewById(R.id.afterday_image);int icon2 = parseIcon(jsonData.getString("img5")+".gif");afterday_image.setImageResource(icon2);// 取得第四天的天氣情況TextView nextday_date = (TextView) findViewById(R.id.nextday_date);nextday_date.setText(jsonData.getString("weather4"));TextView nextday_qiweng = (TextView) findViewById(R.id.nextday_qiweng);nextday_qiweng.setText(jsonData.getString("temp4"));TextView nextday_tianqi = (TextView) findViewById(R.id.nextday_tianqi);nextday_tianqi.setText(jsonData.getString("wind4"));ImageView nextday_image = (ImageView) findViewById(R.id.nextday_image);int icon3 = parseIcon(jsonData.getString("img7")+".gif");nextday_image.setImageResource(icon3);}catch(Exception e){e.printStackTrace();}}
這里我們直接獲取網絡上的JSON數據,把數據放入對應的位置即可,圖片資源的使用方法不變,還是放在本地drawalbe文件下。
?
1.3圖片資源的使用 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?
// 工具方法,該方法負責把返回的天氣圖標字符串,轉換為程序的圖片資源ID。
private int parseIcon(String strIcon)
{if (strIcon == null)return -1;if ("0.gif".equals(strIcon))return R.drawable.a_0;if ("1.gif".equals(strIcon))return R.drawable.a_1;if ("2.gif".equals(strIcon))return R.drawable.a_2;if ("3.gif".equals(strIcon))return R.drawable.a_3;
……
?
這里就不全部貼上了。
1.4最重要的一件事情 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
(1)源碼http://download.csdn.net/detail/feiyangxiaomi/6261805(2)資源(源碼里面有)
?