<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java學習

    java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

     

    JAVA Webservices 實現天氣預報

    最近工作有用到web services,而這兩天也比較有空,就弄了一個獲取天氣預報的Util。以前以為這有多難,因為數據來源是個困難?,F在用web services的技術,這個就不用管了。說共享也好,說偷也好,總之是達到目的了。網上找了幾個提供web services的網站。發現下面這個是相當不錯的。
    http://www.webxml.com.cn/Webservices/WeatherWebService.asmx

    下面就用java把具體的代碼寫寫吧!
    這里我采用比較簡單的get請求調用,畢竟這也沒什么秘密可言,就用最簡單的就可以了。
    還有,這里很多捕獲異常的東西給我去掉了,自己加吧!

    public final class WeatherUtil
    {
    private static String SERVICES_HOST = "www.webxml.com.cn";
    private static String WEATHER_SERVICES_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/";
    private static String SUPPORT_CITY_URL = WEATHER_SERVICES_URL
    + "getSupportCity?byProvinceName=ALL";
    private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
    + "getWeatherbyCityName?theCityName=";
    private WeatherUtil(){}
    public static InputStream getSoapInputStream(String url)
    {
    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()
    {
    ArrayList cityList = null;
    Document doc;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputStream is = getSoapInputStream(SUPPORT_CITY_URL);
    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)
    {
    ArrayList weatherList = null;
    Document doc;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    //這里他的編碼格式就是這樣,我試了幾個也沒辦法。。只好這樣混過去了
    InputStream is = getSoapInputStream(WEATHER_QUERY_URL
    + new String(city.getBytes("UTF-8"), "GBK"));
    doc = db.parse(is);
    NodeList nl = doc.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 = WeatherUtil.getWeather("59287");
    // ArrayList<String> weatherList = WeatherUtil.getSupportCity();
    for (String weather : weatherList)
    {
    System.out.println(weather);
    }
    }
    }



    文章來源 mccxj
    鼠中無大將的博客
    , 文章地址 http://mccxj.javaeye.com/blog/144161

    posted on 2009-04-08 14:27 找個美女做老婆 閱讀(790) 評論(1)  編輯  收藏

    評論

    # re: JAVA Webservices 實現天氣預報 2010-08-03 11:13 lb_rain

    ArrayList<String> 處編譯錯誤

    Multiple markers at this line
    - The type ArrayList is not generic; it cannot be parameterized with
    arguments <String>
    - Syntax error, parameterized types are only available if source level
    is 5.0

    我用的jdk1.5.0

    請問你用的哪個版本?  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    公告

    本blog已經搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: baoyu122.永久免费视频| 亚洲美女精品视频| 羞羞视频免费网站日本| 日本免费一二区在线电影 | 亚洲国产精品VA在线观看麻豆| 免费国产在线精品一区| 亚洲女同成人AⅤ人片在线观看| 看全免费的一级毛片| 免费h成人黄漫画嘿咻破解版| 美女黄频免费网站| 亚洲黄片毛片在线观看| 精品国产免费一区二区三区| 国产亚洲?V无码?V男人的天堂 | 亚洲va久久久噜噜噜久久天堂| 免费播放在线日本感人片| 亚洲AV无码乱码在线观看裸奔| 真实国产乱子伦精品免费| 亚洲人成在线精品| 美女黄网站人色视频免费国产| 美女的胸又黄又www网站免费| 亚洲伊人色欲综合网| 91成人免费观看| 亚洲日韩精品无码专区加勒比| 免费日本黄色网址| 最近免费中文字幕MV在线视频3 | 亚洲性线免费观看视频成熟| 91在线亚洲综合在线| 一级毛片直播亚洲| 日本免费一区二区久久人人澡| 亚洲人成777在线播放| 免费人成在线观看播放国产| 两个人日本免费完整版在线观看1 两个人的视频www免费 | 国产AV无码专区亚洲AV蜜芽| 伊人久久亚洲综合| 99爱在线精品免费观看| 婷婷亚洲综合五月天小说在线| 亚洲va中文字幕无码久久| 在线免费视频一区| 日本中文字幕免费高清视频| 亚洲乱亚洲乱妇24p| 亚洲av永久无码精品古装片|