项目要求在应用首页面展示本地当日天气的概况,首先想到的是google和雅虎,前者很久之前接触过,听说后来用不了了,后者由于邮箱事件的缘故个人不喜欢(虽然貌似苹果也用雅虎的天气预报),之后又想到了前段时间写的调用webservice的例子,可是频繁的在手机调用毕竟不太放心,加之免费版的各种限制,还是放弃了!搜了很多资料,也问了群里的一些人士,最后决定使用"中国天气网"来实现http://www.weather.com.cn/,在国内这个算是比较权威了吧,下面印着"中国气象局公共气象服务中心"的版权呢!通过接口返回的数据信息量很大,也比较稳定,绝对可以满足一般应用对天气的需求!就是有一点比较麻烦,需要知道对应的城市码,稍后说明我的处理办法!
没有官方的API,都是网络上大家研究贡献的成果,普遍流传的有这么三个接口(以深圳的城市代码101280601为例):
①http://m.weather.com.cn/data/101280601.html 返回的信息最全的接口
- <span style="font-family:Comic Sans MS; font-size:18px">{
- "weatherinfo": {
- "city": "深圳",
- "city_en": "shenzhen",
- "date_y": "2013年9月11日",
- "date": "",
- "week": "星期三",
- "fchh": "11",
- "cityid": "101280601",
- "temp1": "33℃~27℃",
- "temp2": "32℃~26℃",
- "temp3": "28℃~25℃",
- "temp4": "29℃~25℃",
- "temp5": "30℃~25℃",
- "temp6": "31℃~26℃",
- "tempF1": "91.4℉~80.6℉",
- "tempF2": "89.6℉~78.8℉",
- "tempF3": "82.4℉~77℉",
- "tempF4": "84.2℉~77℉",
- "tempF5": "86℉~77℉",
- "tempF6": "87.8℉~78.8℉",
- "weather1": "多云",
- "weather2": "阵雨",
- "weather3": "阵雨",
- "weather4": "阵雨",
- "weather5": "阵雨",
- "weather6": "阵雨",
- "img1": "1",
- "img2": "99",
- "img3": "3",
- "img4": "99",
- "img5": "3",
- "img6": "99",
- "img7": "3",
- "img8": "99",
- "img9": "3",
- "img10": "99",
- "img11": "3",
- "img12": "99",
- "img_single": "1",
- "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恤衫等清凉夏季服装。",
- "index48": "炎热",
- "index48_d": "天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。",
- "index_uv": "中等",
- "index48_uv": "中等",
- "index_xc": "不宜",
- "index_tr": "较适宜",
- "index_co": "较不舒适",
- "st1": "33",
- "st2": "26",
- "st3": "30",
- "st4": "24",
- "st5": "25",
- "st6": "23",
- "index_cl": "较适宜",
- "index_ls": "适宜",
- "index_ag": "不易发"
- }
- }</span>
②http://www.weather.com.cn/data/sk/101280601.html 返回的信息比较简洁
- <span style="font-family:Comic Sans MS; font-size:18px">{
- "weatherinfo": {
- "city": "深圳",
- "cityid": "101280601",
- "temp": "31",
- "WD": "东南风",
- "WS": "3级",
- "SD": "58%",
- "WSE": "3",
- "time": "17:10",
- "isRadar": "1",
- "Radar": "JC_RADAR_AZ9755_JB"
- }
- }</span>
③http://www.weather.com.cn/data/cityinfo/101010100.html 另一个返回信息简洁的接口
- <span style="font-family:Comic Sans MS; font-size:18px">{
- "weatherinfo": {
- "city": "深圳",
- "cityid": "101280601",
- "temp1": "26℃",
- "temp2": "32℃",
- "weather": "多云",
- "img1": "n1.gif",
- "img2": "d1.gif",
- "ptime": "18:00"
- }
- }</span>
返回的数据是json格式的,处理起来并不麻烦,可是城市代码怎么办?一种方法是打开中国天气网的官网,在查询框中输入你所要查询的城市,浏览器跳转到的页面会显示一个地址,比如:http://www.weather.com.cn/weather/101280601.shtml,那么红色的部分就是你所查询的城市对应的城市代码!可是这也太有局限性了吧!
还有的文章介绍根据IP地址获取城市代码,但应该不适用在Android移动端吧,我没有研究,所有采用了下面的办法:第二种是将最新的城市代码放入本地数据库(数据库文件我放在末尾处,需要的可以下载,在中国范围内应该足够用了),通过在代码中查询城市名称从数据库中取出与之对应的城市代码,通过字符串的拼接发送请求,来获取想要的天气信息!这个是我自己的思路,或许比较笨拙,希望有更好想法的朋友可以指出!
下面的这段代码是根据城市名称从本地数据库中查询出与之对应的城市代码,通过封装的HttpGet方法来获取返回的数据,并使用json解析出自己所需要的内容:
- <span style="font-family:Comic Sans MS; font-size:18px">public void initWaetherData() {
- new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- DBHelper helper = new DBHelper(getApplicationContext());
- String cityName = "深圳";
- String cityCode = null;
- String sql = "select * from city_table where CITY =" + "'"
- + cityName + "'" + ";";
- Log.i("TAG", "sql:" + sql);
- Cursor cursor = helper.getReadableDatabase()
- .rawQuery(sql, null);
- if (cursor != null) {
- cursor.moveToFirst();
- cityCode = cursor.getString(cursor
- .getColumnIndex("WEATHER_ID"));
- Log.i("TAG", "cityCode:" + cityCode);
- }
- cursor.close();
- helper.close();
- String weatherUrl = "http://www.weather.com.cn/data/cityinfo/" + cityCode
- + ".html";
- String weatherJson = queryStringForGet(weatherUrl);
- Log.i("TAG", weatherJson);
- try {
- JSONObject jsonObject = new JSONObject(weatherJson);
- JSONObject weatherObject = jsonObject
- .getJSONObject("weatherinfo");
- Log.i("TAG", "city:" + weatherObject.getString("city"));
- Log.i("TAG", "temp:" + weatherObject.getString("temp1"));
- Log.i("TAG", "temp:" + weatherObject.getString("temp2"));
- Log.i("TAG","weather:" + weatherObject.getString("weather"));
- Log.i("TAG", "temp:" + weatherObject.getString("img1"));
- Log.i("TAG", "temp:" + weatherObject.getString("img2"));
- Message message = new Message();
- message.obj = weatherObject;
- handler.sendMessage(message);
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }).start();
- }</span>
在handler中根据需要将返回的内容解析并显示到界面:
- <span style="font-family:Comic Sans MS; font-size:18px">private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // TODO Auto-generated method stub
- super.handleMessage(msg);
- JSONObject object = (JSONObject) msg.obj;
- try {
- txt_weather_city.setText(object.getString("city"));
- txt_weather_temp.setText(object.getString("temp2")+"/"+object.getString("temp1"));
- txt_weather_detail.setText(object.getString("weather"));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- };</span>
当然还有一个问题就是城市名字我是写死的,建议可以使用百度地图的定位功能来获取当前城市名称,然后再使用!当然还会存在很多字符串的问题,比如深圳,深圳市等等,具体问题具体分析,按照需求处理就好了!
数据库文件我放在了资源文件夹raw中了,通过读取复制到当前程序的数据库目录下,这个工具方法挺好用的(之前一个同事的工具代码),代码如下:
- <span style="font-family:Comic Sans MS; font-size:18px">/** 将资源文件中的数据库文件复制到当前程序数据库目录下 */
- public void copyDatabase() {
- File file = new File(DB_PATH);
- if (!file.isDirectory())
- file.mkdir();
- String dbfile = DB_PATH + "/" + DBNAME;//自己应用数据库的名字
- try {
- if (new File(dbfile).length() == 0) {
- FileOutputStream fos = new FileOutputStream(dbfile);
- byte[] buffer = new byte[BUFFER_SIZE];
- readDB(fos, buffer,<span style="color:#33ff33"> </span><span style="color:#3366ff">R.raw.citychina</span>);//数据库文件的名称
- fos.close();
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }</span>
- <span style="font-family:Comic Sans MS; font-size:18px">private void readDB(FileOutputStream fos, byte[] buffer, int db_id)
- throws IOException {
- int count;
- InputStream is;
- is = this.context.getResources().openRawResource(db_id);
- while ((count = is.read(buffer)) > 0) {
- fos.write(buffer, 0, count);
- }
- is.close();
- }</span>
至此使用中国天气网,用城市名称获取天气状况的思路大致是这样了,只是个人的拙见,本文同样是留个备份案底,以便日后真正用起来不会抓狂!Demo和城市数据库文件的下载地址如下,数据库文件在demo的raw下面如果觉得有需要的话可以自行下载看看@_@