android天氣查詢(二)之網路json資料的獲取
阿新 • • 發佈:2019-02-02
前面一篇文章介紹瞭如何使用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,自己看好自己的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 block Log.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最重要的一件事情