中国天气网Android XmlPULL解析:通过城市名获取weatherCode

来源:互联网 发布:手机遥控汽车软件 编辑:程序博客网 时间:2024/04/29 17:52

之前项目中使用和比较过多个天气API:百度天气,新浪天气,中国天气网等,返回值均为JSON数据格式(请注意编码格式utf-8)。现在将中国天气网城市-天气对应表的解析过程贴上来,方便后来的小伙伴开发。

本文旨在使用中国天气网,通过三级省市名来获取查询天气需要的weatherCode。
一、 小结:
1. 百度天气
  与百度地图一样,需要申请自己的AK。有集成百度定位的正好可以使用。API地址为:
(cityName例子:上海市)

//完整的代码有点多,函数名也很容易懂是什么作用。URL=“http://api.map.baidu.com/telematics/v3/weather?location=”  +URLEncoder.encode(cityName.substring(0, cityName.length() - 1), "UTF-8")  +"&output=json&ak=" + appinfo.metaData.get("com.baidu.lbsapi.API_KEY")       +"&mcode=" + getCertificateSHA1Fingerprint(context)        +";"+packagename;
  1. 新浪天气
      更新有延迟,数据可能会不完整。
URL = "http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city="  + cityName.replace("市", "") + "&dfc=3";

  
3. 中国天气网
  其API地址为 URL = http://www.weather.com.cn/data/cityinfo/101010100.html
  101010100即为北京所对应的天气代码

  接下来进入正题,首先要获取对应城市的天气代码,需要有对应的查询表,网上有提供的。我是复制了一份表,保存在了xml文件中,通过xml pull解析最终得出数据。
  贴上xml数据:完整中国天气网城市-天气代码对应表
  

<?xml version="1.0" encoding="UTF-8"?><China>    <province name="北京" id="01">        <city name="北京" id="0101">            <county name="北京" id="010101" weatherCode="101010100" />            <county name="海淀" id="010102" weatherCode="101010200" />            <county name="朝阳" id="010103" weatherCode="101010300" />            <county name="顺义" id="010104" weatherCode="101010400" />            <county name="怀柔" id="010105" weatherCode="101010500" />            <county name="通州" id="010106" weatherCode="101010600" />            <county name="昌平" id="010107" weatherCode="101010700" />            <county name="延庆" id="010108" weatherCode="101010800" />            <county name="丰台" id="010109" weatherCode="101010900" />            <county name="石景山" id="010110" weatherCode="101011000" />            <county name="大兴" id="010111" weatherCode="101011100" />            <county name="房山" id="010112" weatherCode="101011200" />            <county name="密云" id="010113" weatherCode="101011300" />            <county name="门头沟" id="010114" weatherCode="101011400" />            <county name="平谷" id="010115" weatherCode="101011500" />        </city>    </province>    <province name="上海" id="02">        <city name="上海" id="0201">            <county name="上海" id="020101" weatherCode="101020100" />            <county name="闵行" id="020102" weatherCode="101020200" />            <county name="宝山" id="020103" weatherCode="101020300" />            <county name="嘉定" id="020104" weatherCode="101020500" />            <county name="浦东南汇" id="020105" weatherCode="101020600" />            <county name="金山" id="020106" weatherCode="101020700" />            <county name="青浦" id="020107" weatherCode="101020800" />            <county name="松江" id="020108" weatherCode="101020900" />            <county name="奉贤" id="020109" weatherCode="101021000" />            <county name="崇明" id="020110" weatherCode="101021100" />            <county name="徐家汇" id="020111" weatherCode="101021200" />            <county name="浦东" id="020112" weatherCode="101021300" />        </city>    </province>    ....

  接下来,解析代码:
  

public String getCityCodeFromCityName(String provinceName, String cityName, String countyName) throws XmlPullParserException, IOException {        String cityCode = null;        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();        //获取XmlPullParser实例        XmlPullParser pullParser = factory.newPullParser();        InputStream in = context.getAssets().open("city_code_data.xml");        pullParser.setInput(in, "UTF-8");        //开始        int eventCode = pullParser.getEventType();        boolean ifExit = false;        boolean ifProvinceCatched = false;        boolean ifCityCatched = false;        boolean ifCountyCatched = false;        while (eventCode != XmlPullParser.END_DOCUMENT) {            String nodeName = pullParser.getName();            switch (eventCode) {                case XmlPullParser.START_DOCUMENT:                    break;                case XmlPullParser.START_TAG:                    if ("province".equals(nodeName)) {                        int i = 0;                        while (i < pullParser.getAttributeCount()) {                            if(ifProvinceCatched)                                break;                            String name = pullParser.getAttributeName(i);                            String value = pullParser.getAttributeValue(i);                            if (name.equalsIgnoreCase("name"))                                if (value.equalsIgnoreCase(provinceName)) {                                    this.provinceName = provinceName;                                    ifProvinceCatched = true;                                    break;                                }                            i++;                        }                    } else if ("city".equals(nodeName)) {                        int i = 0;                        while (i < pullParser.getAttributeCount()) {                            if(!ifProvinceCatched || ifCityCatched)                                break;                            String name = pullParser.getAttributeName(i);                            String value = pullParser.getAttributeValue(i);                            if (name.equalsIgnoreCase("name"))                                if (value.equalsIgnoreCase(cityName)) {                                    this.cityName = cityName;                                    ifCityCatched = true;                                    break;                                }                            i++;                        }                    } else if ("county".equals(nodeName)) {                        int i = 0;                        while (i < pullParser.getAttributeCount()) {                            if(!ifCityCatched || !ifProvinceCatched)                                break;                            String name = pullParser.getAttributeName(i);                            String value = pullParser.getAttributeValue(i);                            if (name.equalsIgnoreCase("name"))                                if (value.equalsIgnoreCase(countyName)) {                                    this.countyName = countyName;                                    ifCountyCatched = true;                                }                            if (ifCountyCatched && name.equalsIgnoreCase("weatherCode")) {                                cityCode = pullParser.getAttributeValue(i);                                ifExit = true;                                break;                            }                            i++;                        }                    }                    break;                case XmlPullParser.END_TAG:                    break;                default:                    break;            }            if (ifExit)                break;            eventCode = pullParser.next();        }        return cityCode;    }

  最后,通过调用cityCode = getCityCodeFromCityName(provinceName, cityName, countyName);获取cityCode。然后将“http://www.weather.com.cn/data/cityinfo/”进行补全即可得到上面的URL。请求部分就简单了,通过AsyncTask中HttpURLConnection访问这个URL就行了,当然又要对所得JSON数据进行GSON解析,最终得到天气等数据。

1 0