最近在弄一个Android应用,其中一个功能是天气情况展示,准备使用google的天气API服务(http://www.google.com/ig/api?hl=zh-cn&weather=,,,, ),不想这个接口已经在几个月前关闭服务了。于是各种资料,最终决定使用 中国天气网 的服务。
中国天气网提供有两种数据格式的接口:
一、 XML接口
http://flash.weather.com.cn/wmaps/xml/china.xml
http://flash.weather.com.cn/wmaps/xml/changsha.xml
查了好久,发现这个xml接口返回的数据有点乱,果断弃之。
二、JSON接口
http://m.weather.com.cn/data/101250101.html
这个接口返回的是JSON数据,数据格式很好,缺点就是城市编码的问题了。
下面通过XML接口返回的数据来解析全国各地城市所对应的编码,以便使用。
代码:
Java代码
- package com.tangqiang.tools;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.List;
- import java.util.logging.Logger;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- public class CityCode
- {
- private Logger log = Logger.getLogger(CityCode.class.getName());
- public static void main(String[] args) {
- try {
- CityCode cc = new CityCode();
- Document doc = cc.getCityXml();
- System.out.println(doc.asXML());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 解析
- *
- * @return
- * @throws Exception
- */
- @SuppressWarnings("unchecked")
- private Document getCityXml() throws Exception {
- /** 新建一个doc 保存结果 */
- Document docResult = DocumentHelper.createDocument();
- Element addElementRoot = DocumentHelper.createElement("china");
- String sUrlChina = "http://flash.weather.com.cn/wmaps/xml/china.xml";
- String sChinaProvXml = getUrlString(sUrlChina);
- Document doc = DocumentHelper.parseText(sChinaProvXml);
- Element rootElt = doc.getRootElement(); // 获取根节点
- log.info("根节点:" + rootElt.getName()); // 拿到根节点的名称
- /** 取得所有省份 */
- List<Element> listProv = rootElt.elements("city"); // 获取根节点下的子节点
- for (int i = 0; i < listProv.size(); i++) {
- Element elementProv = listProv.get(i);
- /** 省份名字 */
- String sProvName = elementProv.attributeValue("pyName");
- String sProvNameCN = elementProv.attributeValue("quName");
- /** 添加节点 */
- Element addElementProv = DocumentHelper.createElement("prov");
- addElementProv.addAttribute("pyName", sProvName);
- addElementProv.addAttribute("quName", sProvNameCN);
- try {
- log.info("查询省份:" + sProvNameCN + " 下的城市!");
- String sUrlProv = "http://flash.weather.com.cn/wmaps/xml/" + sProvName + ".xml";
- String sProvXml = getUrlString(sUrlProv);
- Document docProv = DocumentHelper.parseText(sProvXml);
- Element rootEltProv = docProv.getRootElement(); // 获取省份下的跟节点
- List<Element> listCity1 = rootEltProv.elements("city");
- for (int j = 0; j < listCity1.size(); j++) {
- Element elementCity1 = listCity1.get(j);
- String sCityName1 = elementCity1.attributeValue("pyName");
- String sCityUrl1 = elementCity1.attributeValue("url");
- String sCityNameCN1 = elementCity1.attributeValue("cityname");
- System.out.println(sCityNameCN1 + ":" + sCityUrl1);
- Element addElementMainCity = DocumentHelper.createElement("city");
- addElementMainCity.addAttribute("pyName", sCityName1);
- addElementMainCity.addAttribute("quName", sCityNameCN1);
- addElementMainCity.addAttribute("url", sCityUrl1);
- try {
- String sUrlCity1 = "http://flash.weather.com.cn/wmaps/xml/" + sCityName1 + ".xml";
- log.info("查询主要城市:" + sCityNameCN1 + " 下的城市!");
- String sCityXml = getUrlString(sUrlCity1);
- Document docCity1 = DocumentHelper.parseText(sCityXml);
- Element rootCity1 = docCity1.getRootElement();
- List<Element> listCity2 = rootCity1.elements("city");
- for (int k = 0; k < listCity2.size(); k++) {
- Element elementCity2 = listCity2.get(k);
- String sCityName2 = elementCity2.attributeValue("pyName");
- String sCityNameCN2 = elementCity2.attributeValue("cityname");
- String sCityUrl2 = elementCity2.attributeValue("url");
- System.out.println(sCityNameCN2 + ":" + sCityUrl2);
- Element addElementCity2 = DocumentHelper.createElement("city2");
- addElementCity2.addAttribute("pyName", sCityName2);
- addElementCity2.addAttribute("quName", sCityNameCN2);
- addElementCity2.addAttribute("url", sCityUrl2);
- addElementMainCity.add(addElementCity2);
- }
- } catch (Exception e) {
- log.info("查询主要城市:" + sCityNameCN1 + " 下的城市失败!");
- }
- addElementProv.add(addElementMainCity);
- }
- } catch (Exception e) {
- log.info("查询省份:" + sProvNameCN + "下的城市失败!");
- }
- addElementRoot.add(addElementProv);
- }
- docResult.setRootElement(addElementRoot);
- return docResult;
- }
- /**
- * 从url获取xml字符串
- *
- * @param sUrl
- * @return
- * @throws Exception
- */
- private String getUrlString(String sUrl) throws Exception {
- String sResult = "";
- StringBuffer sbResult = null;
- log.info("开始连接Url:" + sUrl);
- sbResult = new StringBuffer();
- URL url = new URL(sUrl);
- URLConnection con = url.openConnection();
- BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
- String sLine = null;
- while ((sLine = br.readLine()) != null) {
- sbResult.append(sLine);
- }
- sResult = sbResult.toString();
- log.info("获取到信息:" + sbResult.toString().substring(0, 500));
- if (sResult.contains("html")) {
- String sInt = "null";
- Integer.parseInt(sInt);
- }
- return sResult;
- }
- }
中国天气网 JSON接口的城市编码解析及结果
时间: 2024-10-26 15:47:40