Android使用"中国天气网"API数据通过城市名称获取天气情况

项目要求在应用首页面展示本地当日天气的概况,首先想到的是google和雅虎,前者很久之前接触过,听说后来用不了了,后者由于邮箱事件的缘故个人不喜欢(虽然貌似苹果也用雅虎的天气预报),之后又想到了前段时间写的调用webservice的例子,可是频繁的在手机调用毕竟不太放心,加之免费版的各种限制,还是放弃了!搜了很多资料,也问了群里的一些人士,最后决定使用"中国天气网"来实现http://www.weather.com.cn/,在国内这个算是比较权威了吧,下面印着"中国气象局公共气象服务中心"的版权呢!通过接口返回的数据信息量很大,也比较稳定,绝对可以满足一般应用对天气的需求!就是有一点比较麻烦,需要知道对应的城市码,稍后说明我的处理办法!

        没有官方的API,都是网络上大家研究贡献的成果,普遍流传的有这么三个接口(以深圳的城市代码101280601为例):

        ①http://m.weather.com.cn/data/101280601.html 返回的信息最全的接口

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS; font-size:18px">{  
  2.     "weatherinfo": {  
  3.         "city": "深圳",  
  4.         "city_en": "shenzhen",  
  5.         "date_y": "2013年9月11日",  
  6.         "date": "",  
  7.         "week": "星期三",  
  8.         "fchh": "11",  
  9.         "cityid": "101280601",  
  10.         "temp1": "33℃~27℃",  
  11.         "temp2": "32℃~26℃",  
  12.         "temp3": "28℃~25℃",  
  13.         "temp4": "29℃~25℃",  
  14.         "temp5": "30℃~25℃",  
  15.         "temp6": "31℃~26℃",  
  16.         "tempF1": "91.4℉~80.6℉",  
  17.         "tempF2": "89.6℉~78.8℉",  
  18.         "tempF3": "82.4℉~77℉",  
  19.         "tempF4": "84.2℉~77℉",  
  20.         "tempF5": "86℉~77℉",  
  21.         "tempF6": "87.8℉~78.8℉",  
  22.         "weather1": "多云",  
  23.         "weather2": "阵雨",  
  24.         "weather3": "阵雨",  
  25.         "weather4": "阵雨",  
  26.         "weather5": "阵雨",  
  27.         "weather6": "阵雨",  
  28.         "img1": "1",  
  29.         "img2": "99",  
  30.         "img3": "3",  
  31.         "img4": "99",  
  32.         "img5": "3",  
  33.         "img6": "99",  
  34.         "img7": "3",  
  35.         "img8": "99",  
  36.         "img9": "3",  
  37.         "img10": "99",  
  38.         "img11": "3",  
  39.         "img12": "99",  
  40.         "img_single": "1",  
  41.         "img_title1": "多云",  
  42.         "img_title2": "多云",  
  43.         "img_title3": "阵雨",  
  44.         "img_title4": "阵雨",  
  45.         "img_title5": "阵雨",  
  46.         "img_title6": "阵雨",  
  47.         "img_title7": "阵雨",  
  48.         "img_title8": "阵雨",  
  49.         "img_title9": "阵雨",  
  50.         "img_title10": "阵雨",  
  51.         "img_title11": "阵雨",  
  52.         "img_title12": "阵雨",  
  53.         "img_title_single": "多云",  
  54.         "wind1": "微风",  
  55.         "wind2": "微风",  
  56.         "wind3": "微风",  
  57.         "wind4": "微风",  
  58.         "wind5": "微风",  
  59.         "wind6": "微风",  
  60.         "fx1": "微风",  
  61.         "fx2": "微风",  
  62.         "fl1": "小于3级",  
  63.         "fl2": "小于3级",  
  64.         "fl3": "小于3级",  
  65.         "fl4": "小于3级",  
  66.         "fl5": "小于3级",  
  67.         "fl6": "小于3级",  
  68.         "index": "炎热",  
  69.         "index_d": "天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。",  
  70.         "index48": "炎热",  
  71.         "index48_d": "天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。",  
  72.         "index_uv": "中等",  
  73.         "index48_uv": "中等",  
  74.         "index_xc": "不宜",  
  75.         "index_tr": "较适宜",  
  76.         "index_co": "较不舒适",  
  77.         "st1": "33",  
  78.         "st2": "26",  
  79.         "st3": "30",  
  80.         "st4": "24",  
  81.         "st5": "25",  
  82.         "st6": "23",  
  83.         "index_cl": "较适宜",  
  84.         "index_ls": "适宜",  
  85.         "index_ag": "不易发"  
  86.     }  
  87. }</span>  


         ②http://www.weather.com.cn/data/sk/101280601.html 返回的信息比较简洁

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS; font-size:18px">{  
  2.     "weatherinfo": {  
  3.         "city": "深圳",  
  4.         "cityid": "101280601",  
  5.         "temp": "31",  
  6.         "WD": "东南风",  
  7.         "WS": "3级",  
  8.         "SD": "58%",  
  9.         "WSE": "3",  
  10.         "time": "17:10",  
  11.         "isRadar": "1",  
  12.         "Radar": "JC_RADAR_AZ9755_JB"  
  13.     }  
  14. }</span>  

       

        ③http://www.weather.com.cn/data/cityinfo/101010100.html  另一个返回信息简洁的接口

[html]  view plain copy
  1. <span style="font-family:Comic Sans MS; font-size:18px">{  
  2.     "weatherinfo": {  
  3.         "city": "深圳",  
  4.         "cityid": "101280601",  
  5.         "temp1": "26℃",  
  6.         "temp2": "32℃",  
  7.         "weather": "多云",  
  8.         "img1": "n1.gif",  
  9.         "img2": "d1.gif",  
  10.         "ptime": "18:00"  
  11.     }  
  12. }</span>  


        返回的数据是json格式的,处理起来并不麻烦,可是城市代码怎么办?一种方法是打开中国天气网的官网,在查询框中输入你所要查询的城市,浏览器跳转到的页面会显示一个地址,比如:http://www.weather.com.cn/weather/101280601.shtml,那么红色的部分就是你所查询的城市对应的城市代码!可是这也太有局限性了吧!

还有的文章介绍根据IP地址获取城市代码,但应该不适用在Android移动端吧,我没有研究,所有采用了下面的办法:第二种是将最新的城市代码放入本地数据库(数据库文件我放在末尾处,需要的可以下载,在中国范围内应该足够用了),通过在代码中查询城市名称从数据库中取出与之对应的城市代码,通过字符串的拼接发送请求,来获取想要的天气信息!这个是我自己的思路,或许比较笨拙,希望有更好想法的朋友可以指出!


下面的这段代码是根据城市名称从本地数据库中查询出与之对应的城市代码,通过封装的HttpGet方法来获取返回的数据,并使用json解析出自己所需要的内容:

[java]  view plain copy
  1. <span style="font-family:Comic Sans MS; font-size:18px">public void initWaetherData() {  
  2.         new Thread(new Runnable() {  
  3.             @Override  
  4.             public void run() {  
  5.                 // TODO Auto-generated method stub  
  6.                 DBHelper helper = new DBHelper(getApplicationContext());  
  7.                 String cityName = "深圳";  
  8.                 String cityCode = null;  
  9.                 String sql = "select * from city_table where CITY =" + "'"  
  10.                         + cityName + "'" + ";";  
  11.                 Log.i("TAG""sql:" + sql);  
  12.                 Cursor cursor = helper.getReadableDatabase()  
  13.                         .rawQuery(sql, null);  
  14.                 if (cursor != null) {  
  15.                     cursor.moveToFirst();  
  16.                     cityCode = cursor.getString(cursor  
  17.                             .getColumnIndex("WEATHER_ID"));  
  18.                     Log.i("TAG""cityCode:" + cityCode);  
  19.                 }  
  20.                 cursor.close();  
  21.                 helper.close();  
  22.                 String weatherUrl = "http://www.weather.com.cn/data/cityinfo/" + cityCode  
  23.                         + ".html";  
  24.                 String weatherJson = queryStringForGet(weatherUrl);  
  25.                 Log.i("TAG", weatherJson);  
  26.   
  27.                 try {  
  28.                     JSONObject jsonObject = new JSONObject(weatherJson);  
  29.   
  30.                     JSONObject weatherObject = jsonObject  
  31.                             .getJSONObject("weatherinfo");  
  32.   
  33.                     Log.i("TAG""city:" + weatherObject.getString("city"));  
  34.                     Log.i("TAG""temp:" + weatherObject.getString("temp1"));  
  35.                     Log.i("TAG""temp:" + weatherObject.getString("temp2"));  
  36.                     Log.i("TAG","weather:" + weatherObject.getString("weather"));  
  37.                     Log.i("TAG""temp:" + weatherObject.getString("img1"));  
  38.                     Log.i("TAG""temp:" + weatherObject.getString("img2"));  
  39.                     Message message = new Message();  
  40.                     message.obj = weatherObject;  
  41.                     handler.sendMessage(message);  
  42.   
  43.                 } catch (JSONException e) {  
  44.                     // TODO Auto-generated catch block  
  45.                     e.printStackTrace();  
  46.                 }  
  47.             }  
  48.         }).start();  
  49.     }</span>  


      

在handler中根据需要将返回的内容解析并显示到界面:

[java]  view plain copy
  1. <span style="font-family:Comic Sans MS; font-size:18px">private Handler handler = new Handler() {  
  2.         @Override  
  3.         public void handleMessage(Message msg) {  
  4.             // TODO Auto-generated method stub  
  5.             super.handleMessage(msg);  
  6.             JSONObject object = (JSONObject) msg.obj;  
  7.             try {  
  8.                 txt_weather_city.setText(object.getString("city"));  
  9.                 txt_weather_temp.setText(object.getString("temp2")+"/"+object.getString("temp1"));  
  10.                 txt_weather_detail.setText(object.getString("weather"));  
  11.             } catch (Exception e) {  
  12.                 // TODO Auto-generated catch block  
  13.                 e.printStackTrace();  
  14.             }  
  15.         }  
  16.     };</span>  


当然还有一个问题就是城市名字我是写死的,建议可以使用百度地图的定位功能来获取当前城市名称,然后再使用!当然还会存在很多字符串的问题,比如深圳,深圳市等等,具体问题具体分析,按照需求处理就好了!


数据库文件我放在了资源文件夹raw中了,通过读取复制到当前程序的数据库目录下,这个工具方法挺好用的(之前一个同事的工具代码),代码如下:

[java]  view plain copy
  1. <span style="font-family:Comic Sans MS; font-size:18px">/** 将资源文件中的数据库文件复制到当前程序数据库目录下 */  
  2.     public void copyDatabase() {  
  3.   
  4.         File file = new File(DB_PATH);  
  5.         if (!file.isDirectory())  
  6.             file.mkdir();  
  7.         String dbfile = DB_PATH + "/" + DBNAME;//自己应用数据库的名字  
  8.   
  9.         try {  
  10.             if (new File(dbfile).length() == 0) {  
  11.   
  12.                 FileOutputStream fos = new FileOutputStream(dbfile);  
  13.                 byte[] buffer = new byte[BUFFER_SIZE];  
  14.   
  15.                 readDB(fos, buffer,<span style="color:#33ff33"> </span><span style="color:#3366ff">R.raw.citychina</span>);//数据库文件的名称  
  16.   
  17.                 fos.close();  
  18.             }  
  19.         } catch (FileNotFoundException e) {  
  20.             // TODO Auto-generated catch block  
  21.             e.printStackTrace();  
  22.         } catch (IOException e) {  
  23.             // TODO Auto-generated catch block  
  24.             e.printStackTrace();  
  25.         }  
  26.   
  27.     }</span>  

[java]  view plain copy
  1. <span style="font-family:Comic Sans MS; font-size:18px">private void readDB(FileOutputStream fos, byte[] buffer, int db_id)  
  2.             throws IOException {  
  3.         int count;  
  4.         InputStream is;  
  5.         is = this.context.getResources().openRawResource(db_id);  
  6.         while ((count = is.read(buffer)) > 0) {  
  7.             fos.write(buffer, 0, count);  
  8.         }  
  9.         is.close();  
  10.     }</span>  


至此使用中国天气网,用城市名称获取天气状况的思路大致是这样了,只是个人的拙见,本文同样是留个备份案底,以便日后真正用起来不会抓狂!Demo和城市数据库文件的下载地址如下,数据库文件在demo的raw下面如果觉得有需要的话可以自行下载看看@_@

Demo下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值