webservice 天气预报的接口

下面是在学习webService时整理的一些琐碎:

package cn.com.snxun.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public final class WeatherUtils {
    private static String SERVICES_HOST = "www.webxml.com.cn";
    private static String WEATHER_SERVICES_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/";
    // 获得本天气预报Web Services支持的洲、国内外省份和城市信息
    private static String SUPPORT_CITY_URL = WEATHER_SERVICES_URL
            + "getSupportCity?byProvinceName=";
    // 根据城市或地区名称查询
    private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
            + "getWeatherbyCityName?theCityName=";

    private WeatherUtils() {
    }

    /**
     * 将文件转换为流
     * 
     * @param url
     * @return
     * @throws IOException
     */
    public static InputStream getSoapInputStream(String url) throws IOException {
        InputStream is = null;
        URL U = new URL(url);
        URLConnection conn = U.openConnection();
        conn.setRequestProperty("Host", SERVICES_HOST);
        conn.connect();
        is = conn.getInputStream();
        return is;
    }

    // 取得支持的城市列表
    public static ArrayList<String> getSupportCity(String provinceName)
            throws ParserConfigurationException, IOException, SAXException {
        ArrayList cityList = null;
        Document doc;
        // 首先得到:得到 DOM 解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // 如果生成的解析器将提供对 xml 名称空间的支持,则为 true;否则为 false
        dbf.setNamespaceAware(true);
        // 然后从 DOM 工厂获得 DOM 解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream is = getSoapInputStream(SUPPORT_CITY_URL + provinceName);
        doc = db.parse(is);
        NodeList nl = doc.getElementsByTagName("string");
        int len = nl.getLength();
        cityList = new ArrayList<String>(len);
        for (int i = 0; i < len; i++) {
            Node n = nl.item(i);
            String city = n.getFirstChild().getNodeValue();
            cityList.add(city);
        }
        is.close();
        return cityList;
    }

    // 取得城市的天气
    public static ArrayList<String> getWeather(String city)
            throws ParserConfigurationException, UnsupportedEncodingException,
            IOException, SAXException {
        ArrayList weatherList = null;

        // 首先得到:得到 DOM 解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // 如果生成的解析器将提供对 xml 名称空间的支持,则为 true;否则为 false
        dbf.setNamespaceAware(true);
        // 然后从 DOM 工厂获得 DOM 解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        // 将XML文件转成流
        InputStream is = getSoapInputStream(WEATHER_QUERY_URL + city);
        // 解析 XML 文档的输入流,得到一个 Document
        Document doc = db.parse(is);
        // 得到 XML 文档的根节点
        Element root = doc.getDocumentElement();
        // 得到节点的子节点
        NodeList nl = root.getElementsByTagName("string");
        int len = nl.getLength();
        weatherList = new ArrayList<String>(len);
        for (int i = 0; i < len; i++) {
            Node n = nl.item(i);
            String weather = n.getFirstChild().getNodeValue();
            weatherList.add(weather);
        }
        is.close();
        return weatherList;
    }

    public static void main(String[] args) throws Exception {
         ArrayList<String> weatherList = WeatherUtils.getWeather("上海");
        //ArrayList<String> weatherList = WeatherUtils.getSupportCity("江苏");
        for (String weather : weatherList) {
            System.out.println(weather);
        }
    }
}

显示结果:

直辖市
上海
58367
58367.jpg
2018/1/5 21:02:46
3℃/715日 阴转多云
北风转东北风小于32.gif
1.gif
今日天气实况:气温:3℃;风向/风力:静风 0级;湿度:70%;紫外线强度:最弱。空气质量:较差。
紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。
健臻·血糖指数:较易波动,血糖较易波动,注意监测。
感冒指数:较易发,天较凉,增加衣服,注意防护。
穿衣指数:冷,建议着棉衣加羊毛衫等冬季服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
空气污染指数:较差,气象条件较不利于空气污染物扩散。。

4℃/816日 阴转中雨
东风小于32.gif
8.gif
4℃/1117日 中雨转小雨
东南风小于3级转西北风4-58.gif
7.gif
上海简称:沪,位置:上海地处长江三角洲前缘,东濒东海,南临杭州湾,西接江苏,浙江两省,北界长江入海,正当我国南北岸线的中部,北纬31°14′,东经121°29′。面积:总面积7823.5平方公里。人口:人口1000多万。上海丰富的人文资源、迷人的城市风貌、繁华的商业街市和欢乐的节庆活动形成了独特的都市景观。游览上海,不仅能体验到大都市中西合壁、商儒交融、八方来风的氛围,而且能感受到这个城市人流熙攘、车水马龙、灯火璀璨的活力。上海在中国现代史上占有着十分重要的地位,她是中国共产党的诞生地。许多震动中外的历史事件在这里发生,留下了众多的革命遗迹,处处为您讲述着一个个使人永不忘怀的可歌可泣的故事,成为包含民俗的人文景观和纪念地。在上海,每到秋祭,纷至沓来的人们在这里祭祀先烈、缅怀革命历史,已成为了一种风俗。大上海在中国近代历史中,曾是风起云涌可歌可泣的地方。在这里荟萃多少风云人物,散落在上海各处的不同住宅建筑,由于其主人的非同寻常,蕴含了耐人寻味的历史意义。这里曾留下许多革命先烈的足迹。瞻仰孙中山、宋庆龄、鲁迅等故居,会使您产生抚今追昔的深沉遐思,这里还有无数个达官贵人的住宅,探访一下李鸿章、蒋介石等人的公馆,可以联想起主人那段显赫的发迹史。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值