Android擷取天氣預報的兩種方式(國家氣象局和中央氣象台天氣預報API)

來源:互聯網
上載者:User

在項目中有一個天氣預報的需求,在網上找了找大部分人給出的建議是使用webservice方式來擷取資料。於是就嘗試著做了一下,真是不做不知道,做了就收穫了很多。

首先,來說一下通過webservice來擷取國家氣象局提供的天氣服務

在Android SDK中並沒有提供調用WebService的庫,因此,需要使用第三方的SDK來調用WebService。適合手機的WebService用戶端的SDK有一些,比較常用的是KSOAP2。(強烈建議大家去官網上下載http://code.google.com/p/ksoap2-android/。博主之前隨便找了一個,結果是引用的sdk存在問題。坑死人啊。)注意,一定要下載正確的jar包。
http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.4/ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar
點 View raw file 正確下載對應檔案。



好了,下面直接上代碼(這裡只做簡單的介紹)


public class MyWeather extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);//展現天氣預報的具體資料showWeather("北京");}//展現天氣預報的具體資料private void showWeather(String city){String weatherToday = null;int iconToday[] = new int[2];// 擷取遠程Web Service返回的對象SoapObject detail = WebServiceUtil.getWeatherByCity(city);// 根據城市擷取城市具體天氣情況// 擷取天氣實況weatherCurrent = detail.getProperty(4).toString();// 解析今天的天氣情況String date = detail.getProperty(7).toString();weatherToday = "今天:" + date.split(" ")[0];weatherToday = weatherToday + "\n天氣:" + date.split(" ")[1];weatherToday = weatherToday + "\n氣溫:"+ detail.getProperty(8).toString();weatherToday = weatherToday + "\n風力:"+ detail.getProperty(9).toString() + "\n";iconToday[0] = parseIcon(detail.getProperty(10).toString());iconToday[1] = parseIcon(detail.getProperty(11).toString());}// 工具方法,該方法負責把返回的天氣表徵圖字串,轉換為程式的圖片資源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;if ("4.gif".equals(strIcon))return R.drawable.a_4;if ("5.gif".equals(strIcon))return R.drawable.a_5;if ("6.gif".equals(strIcon))return R.drawable.a_6;if ("7.gif".equals(strIcon))return R.drawable.a_7;if ("8.gif".equals(strIcon))return R.drawable.a_8;if ("9.gif".equals(strIcon))return R.drawable.a_9;if ("10.gif".equals(strIcon))return R.drawable.a_10;if ("11.gif".equals(strIcon))return R.drawable.a_11;if ("12.gif".equals(strIcon))return R.drawable.a_12;if ("13.gif".equals(strIcon))return R.drawable.a_13;if ("14.gif".equals(strIcon))return R.drawable.a_14;if ("15.gif".equals(strIcon))return R.drawable.a_15;if ("16.gif".equals(strIcon))return R.drawable.a_16;if ("17.gif".equals(strIcon))return R.drawable.a_17;if ("18.gif".equals(strIcon))return R.drawable.a_18;if ("19.gif".equals(strIcon))return R.drawable.a_19;if ("20.gif".equals(strIcon))return R.drawable.a_20;if ("21.gif".equals(strIcon))return R.drawable.a_21;if ("22.gif".equals(strIcon))return R.drawable.a_22;if ("23.gif".equals(strIcon))return R.drawable.a_23;if ("24.gif".equals(strIcon))return R.drawable.a_24;if ("25.gif".equals(strIcon))return R.drawable.a_25;if ("26.gif".equals(strIcon))return R.drawable.a_26;if ("27.gif".equals(strIcon))return R.drawable.a_27;if ("28.gif".equals(strIcon))return R.drawable.a_28;if ("29.gif".equals(strIcon))return R.drawable.a_29;if ("30.gif".equals(strIcon))return R.drawable.a_30;if ("31.gif".equals(strIcon))return R.drawable.a_31;return 0;}}

但是,多次運行項目之後,擷取不到資料了。經過尋找原來中國氣象局提供的服務分為付費和免費的。而免費的每天有一定的訪問次數限制。將應用安裝到其他手機上也無濟於事。並且免費的有時也不是很穩定,所以只能用其他服務了。

下面就來說一下訪問中央氣象台的天氣預報API得到天氣資料


使用這個服務唯一的缺點就是擷取對應的城市碼,這個有點麻煩(不過可以在這裡下載http://download.csdn.net/tag/%E4%B8%AD%E5%9B%BD%E5%A4%A9%E6%B0%94%E7%BD%91%E5%9F%8E%E5%B8%82%E4%BB%A3%E7%A0%81%E6%95%B4%E7%90%86%E7%89%88.txt)其它的如穩定性與廣闊性也是很一流的,它可以精確到縣和區.下面就直奔主題:


這個服務的天氣預報的請求地址是:http://m.weather.com.cn/data/101091106.html,這個文本就是城市天氣URL,101091106代表的為對應地區的編碼,執行URL,得到一個返迴文本,是JSON格式的,如下(經過格式化):


{"weatherinfo":{"city":"北戴河","city_en":"beidaihe","date_y":"2013年7月31日","date":"","week":"星期三","fchh":"18","cityid":"101091106","temp1":"23℃~28℃","temp2":"23℃~27℃","temp3":"25℃~28℃","temp4":"25℃~30℃","temp5":"25℃~29℃","temp6":"25℃~28℃","tempF1":"73.4℉~82.4℉","tempF2":"73.4℉~80.6℉","tempF3":"77℉~82.4℉","tempF4":"77℉~86℉","tempF5":"77℉~84.2℉","tempF6":"77℉~82.4℉","weather1":"晴轉雷陣雨","weather2":"小雨","weather3":"中雨轉小雨","weather4":"小雨","weather5":"多雲轉晴","weather6":"晴轉陰","img1":"0","img2":"4","img3":"7","img4":"99","img5":"8","img6":"7","img7":"7","img8":"99","img9":"1","img10":"0","img11":"0","img12":"2","img_single":"4","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":"西南風3-4級","wind4":"微風","wind5":"微風","wind6":"東南風轉西南風3-4級","fx1":"微風","fx2":"微風","fl1":"小於3級","fl2":"小於3級","fl3":"3-4級","fl4":"小於3級","fl5":"小於3級","fl6":"3-4級","index":"熱","index_d":"天氣熱,建議著短裙、短褲、短薄外套、T恤等夏季服裝。","index48":"舒適","index48_d":"建議著長袖T恤、襯衫加單褲等服裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"一般","index_co":"較不舒適","st1":"26","st2":"23","st3":"25","st4":"21","st5":"25","st6":"21","index_cl":"較不宜","index_ls":"不宜","index_ag":"極不易發"}}

部分參數說明:temp1~6表示六天的最低溫和最高溫、tempF1~tempF6:表示華氏溫度
Img1~img2表示今天白天、晚上的天氣表徵圖、img3~img4表示明天白天、晚上的天氣表徵圖。。。

然後就是擷取這些字串了,這裡就不介紹了。


主要向大家介紹一下jsonObject類(http://henzil.easymorse.com/?p=242)


JSONObject jsonObject = new JSONObject(jsonString)..getJSONObject("weatherinfo");其中的jsonString就是擷取的字串

例如得到天氣圖片
String imgurl = jsonObject.getString("img1");然後通過這個拼串就可以得到圖片了
http://m.weather.com.cn/img/b+ imgurl +".gif"

上面就是兩種方式擷取天氣情況了,歡迎大家給出意見或建議

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.