Java學(xué)習(xí)

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

           

          JSP從WebService取天氣預(yù)報(bào)數(shù)據(jù),很精簡(jiǎn)的代碼[效果圖]。

          JSP從WebService取天氣預(yù)報(bào)數(shù)據(jù)。

          網(wǎng)上也有相關(guān)的代碼,不過(guò)比較繁瑣,我改進(jìn)了一下。很簡(jiǎn)單地實(shí)現(xiàn)了。

          package com.xxx.web;

          import java.io.*;
          import java.net.*;

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

          import org.w3c.dom.Document;
          import org.w3c.dom.Node;
          import org.w3c.dom.NodeList;

          public class Weather {
           private static String _url = "

           private Weather() {
           }

           /**
            * @param cityName
            * @return
            */
           private static InputStream getSoapInputStream(String cityName) {
            try {
             /*
              * URL url = new URL(_url + cityName); HttpURLConnection hc =
              * (HttpURLConnection) url.openConnection(); hc.connect();
              * InputStream urlStream = hc.getInputStream(); return urlStream;
              */
             return new URL(_url + cityName).openStream();
            } catch (Exception ex) {
             return null;
            }

           }
           
           /**
            * 用W3C DOM對(duì)返回的XML進(jìn)行解釋
            * @param cityName
            * @return
            */
           public static String getWeatherByCityName(String cityName) {
            try {
             Document doc;
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setNamespaceAware(true);
             DocumentBuilder db = dbf.newDocumentBuilder();
             InputStream is = getSoapInputStream(cityName);
             if (is == null){
              return null;
             }
             doc = db.parse(is);
             NodeList nl = doc.getElementsByTagName("string");
             Node n = nl.item(0);
             String weather = n.getFirstChild().getNodeValue();
             is.close();
             return weather;
            } catch (Exception e) {
             e.printStackTrace();
             return null;
            }
           }

           /**
            * @param args
            */
           public static void main(String[] args) {
            System.out.print(getWeatherByCityName("北京"));
           }
          }

          得到一個(gè)String對(duì)象,剩下的就很好處理了。

          最終實(shí)現(xiàn)效果:
          最終實(shí)現(xiàn)效果

          posted on 2009-04-03 16:43 找個(gè)美女做老婆 閱讀(1666) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 时尚| 兴山县| 三门峡市| 鲁甸县| 平湖市| 西宁市| 阳城县| 京山县| 江都市| 江达县| 安阳市| 子洲县| 修水县| 郁南县| 苗栗市| 房产| 静安区| 奉化市| 镇赉县| 呼玛县| 虎林市| 神池县| 时尚| 甘肃省| 汕头市| 德清县| 米林县| 尼玛县| 泉州市| 延吉市| 丹阳市| 淳化县| 政和县| 镇安县| 三门县| 建阳市| 中方县| 高州市| 西藏| 金湖县| 清丰县|