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多萬。上海豐富的人文資源、迷人的城市風貌、繁華的商業街市和歡樂的節慶活動形成了獨特的都市景觀。遊覽上海,不僅能體驗到大都市中西合壁、商儒交融、八方來風的氛圍,而且能感受到這個城市人流熙攘、車水馬龍、燈火璀璨的活力。上海在中國現代史上佔有着十分重要的地位,她是中國共產黨的誕生地。許多震動中外的歷史事件在這裏發生,留下了衆多的革命遺蹟,處處爲您講述着一個個使人永不忘懷的可歌可泣的故事,成爲包含民俗的人文景觀和紀念地。在上海,每到秋祭,紛至沓來的人們在這裏祭祀先烈、緬懷革命歷史,已成爲了一種風俗。大上海在中國近代歷史中,曾是風起雲涌可歌可泣的地方。在這裏薈萃多少風雲人物,散落在上海各處的不同住宅建築,由於其主人的非同尋常,蘊含了耐人尋味的歷史意義。這裏曾留下許多革命先烈的足跡。瞻仰孫中山、宋慶齡、魯迅等故居,會使您產生撫今追昔的深沉遐思,這裏還有無數個達官貴人的住宅,探訪一下李鴻章、蔣介石等人的公館,可以聯想起主人那段顯赫的發跡史。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章