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

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

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

    Java學(xué)習(xí)

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

     

    本站 JAVA 天氣預(yù)報(bào)實(shí)例

    大致步驟是
    1 利用soap向webservice endpoint進(jìn)行請(qǐng)求,取回請(qǐng)求結(jié)果
    2 把結(jié)果顯示在web界面上,web界面采用Java+Jsp
    一 WeatherReport類
    方法 1 構(gòu)造soap請(qǐng)求(請(qǐng)求格式請(qǐng)見上面的鏈接),用用戶輸入的城市名稱鑲在此請(qǐng)求里面

    java 代碼

    /**
    * 獲取SOAP的請(qǐng)求頭,并替換其中的標(biāo)志符號(hào)為用戶輸入的城市
    *
    * 編寫者:王景輝
    *
    * @param city
    * 用戶輸入的城市名稱
    * @return 客戶將要發(fā)送給服務(wù)器的SOAP請(qǐng)求
    */
    private static String getSoapRequest(String city) {
    StringBuilder sb = new StringBuilder();
    sb
    .append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
    + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
    + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
    + "<soap:Body> <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
    + "<theCityName>" + city
    + "</theCityName> </getWeatherbyCityName>"
    + "</soap:Body></soap:Envelope>");
    return sb.toString();
    }


    方法 2 向endpoint發(fā)送上述SOAP請(qǐng)求,并設(shè)置一些請(qǐng)求屬性,返回一個(gè)服務(wù)器端的InputStream(XML文檔流)

    java 代碼

    /**
    * 用戶把SOAP請(qǐng)求發(fā)送給服務(wù)器端,并返回服務(wù)器點(diǎn)返回的輸入流
    *
    * 編寫者:王景輝
    *
    * @param city
    * 用戶輸入的城市名稱
    * @return 服務(wù)器端返回的輸入流,供客戶端讀取
    * @throws Exception
    */
    private static InputStream getSoapInputStream(String city) throws Exception {
    try {
    String soap = getSoapRequest(city);
    if (soap == null) {
    return null;
    }
    URL url = new URL(
    "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
    URLConnection conn = url.openConnection();
    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);

    conn.setRequestProperty("Content-Length", Integer.toString(soap
    .length()));
    conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    conn.setRequestProperty("SOAPAction",
    "http://WebXml.com.cn/getWeatherbyCityName");

    OutputStream os = conn.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
    osw.write(soap);
    osw.flush();
    osw.close();

    InputStream is = conn.getInputStream();
    return is;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }


    方法 3 解析方法2返回的XML文檔流,并用特定的符號(hào)分隔,以便我們?cè)贘sp頁(yè)面進(jìn)行結(jié)果分析

    java 代碼

    /**
    * 對(duì)服務(wù)器端返回的XML進(jìn)行解析
    *
    * 編寫者:王景輝
    *
    * @param city
    * 用戶輸入的城市名稱
    * @return 字符串 用,分割
    */
    public static String getWeather(String city) {
    try {
    Document doc;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputStream is = getSoapInputStream(city);
    doc = db.parse(is);
    NodeList nl = doc.getElementsByTagName("string");
    StringBuffer sb = new StringBuffer();
    for (int count = 0; count < nl.getLength(); count++) {
    Node n = nl.item(count);
    if(n.getFirstChild().getNodeValue().equals("查詢結(jié)果為空!")) {
    sb = new StringBuffer("#") ;
    break ;
    }
    sb.append(n.getFirstChild().getNodeValue() + "#\n");
    }
    is.close();
    return sb.toString();
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }

    二 weatherInfo.jsp頁(yè)面

    核心功能是解析 方法3 所返回的字符串,向endpoint進(jìn)行請(qǐng)求時(shí),一個(gè)XML文檔片段是

    xml 代碼


    <?xml version="1.0" encoding="utf-8" ?>
    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">
    <string>湖南</string>
    <string>長(zhǎng)沙</string>
    <string>57687</string>
    <string>57687.jpg</string>
    <string>2007-12-26 14:35:26</string>
    <string>7℃ / 6℃</string>
    <string>12月26日 小雨</string>
    <string>西北風(fēng)<=3級(jí)</string>
    <string>7.gif</string>
    <string>7.gif</string>
    <string>今日天氣實(shí)況:多云;7.4℃;風(fēng)向/風(fēng)力:西北風(fēng)2級(jí);空氣質(zhì)量:較差;紫外線強(qiáng)度:最弱</string>
    <string>穿衣指數(shù):感冒指數(shù):溫度較低,…… 請(qǐng)您在旅游時(shí)注意增加衣物。</string>
    <string>8℃ / 5℃</string>
    <string>12月27日 小雨</string>
    <string>西北風(fēng)<=3級(jí)</string>
    <string>7.gif</string>
    <string>7.gif</string>
    <string>10℃ / 4℃</string>
    <string>12月28日 小雨</string>
    <string>西北風(fēng)<=3級(jí)</string>
    <string>7.gif</string>
    <string>7.gif</string>
    <string>長(zhǎng)沙市位于湖南省東部偏北, …… 市區(qū)年均降水量1361.6毫米。……<string>
    </ArrayOfString>


    在Jsp中解析的代碼如下,基本上是對(duì)字符串的操作,截取及截取長(zhǎng)度的控制

    java 代碼

    //穿衣指數(shù)
    s1 = str.substring(str.indexOf("穿衣指數(shù):"),str.indexOf("穿衣指數(shù):")+4) ;
    s1Content = str.substring(str.indexOf("穿衣指數(shù):")+5,str.indexOf("感冒指數(shù):")) ;
    //感冒指數(shù)
    s2 = str.substring(str.indexOf("感冒指數(shù):"),str.indexOf("感冒指數(shù):")+4) ;
    s2Content = str.substring(str.indexOf("感冒指數(shù):")+5,str.indexOf("晨練指數(shù):")) ;

    //晨練指數(shù)
    s3 = str.substring(str.indexOf("晨練指數(shù):"),str.indexOf("晨練指數(shù):")+4) ;
    s3Content = str.substring(str.indexOf("晨練指數(shù):")+5,str.indexOf("交通指數(shù):")) ;
    //交通指數(shù)
    s7 = str.substring(str.indexOf("交通指數(shù):"),str.indexOf("交通指數(shù):")+4) ;
    s7Content = str.substring(str.indexOf("交通指數(shù):")+5,str.indexOf("中暑指數(shù):")) ;
    //中暑指數(shù)
    s4 = str.substring(str.indexOf("中暑指數(shù):"),str.indexOf("中暑指數(shù):")+4) ;
    s4Content = str.substring(str.indexOf("中暑指數(shù):")+5,str.indexOf("防曬指數(shù):")) ;
    //防曬指數(shù)
    s5 = str.substring(str.indexOf("防曬指數(shù):"),str.indexOf("防曬指數(shù):")+4) ;
    s5Content = str.substring(str.indexOf("防曬指數(shù):")+5,str.indexOf("旅行指數(shù):")) ;
    //旅行指數(shù)
    s6 = str.substring(str.indexOf("旅行指數(shù):"),str.indexOf("旅行指數(shù):")+4) ;
    s6Content = str.substring(str.indexOf("旅行指數(shù):")+5) ;


    運(yùn)行附件:http://localhost:8080/yourProject/tianqi.jsp

    好了,基本上核心代碼就是上邊那些了!不僅如此,加入我們想要在自己的系統(tǒng)里加入飛機(jī)票,火車票,股票信息等等之類的功能,只要有相應(yīng)的webservice,我們都可以實(shí)現(xiàn)

    文檔來源 http://mhqawjh.javaeye.com/blog/150954

    源代碼 點(diǎn)擊下面現(xiàn)在下載按鈕

    這里感謝 技工窩 mhqawjh 博客地址 http://mhqawjh.javaeye.com/

    posted on 2009-04-08 14:25 找個(gè)美女做老婆 閱讀(1883) 評(píng)論(2)  編輯  收藏

    評(píng)論

    # re: 本站 JAVA 天氣預(yù)報(bào)實(shí)例 2012-12-25 11:20 dgqjava

    真心很垃圾~  回復(fù)  更多評(píng)論   

    # re: 本站 JAVA 天氣預(yù)報(bào)實(shí)例 2013-12-17 11:59 梁建民

    很好很實(shí)用  回復(fù)  更多評(píng)論   


    只有注冊(cè)用戶登錄后才能發(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)論排行榜

    主站蜘蛛池模板: 成人女人A级毛片免费软件| 久9久9精品免费观看| 毛片a级毛片免费观看免下载| 亚洲欧洲一区二区| 亚洲视频在线观看免费| 亚洲AV综合色区无码一区爱AV | 亚洲欧洲无码AV不卡在线| 中文字幕av无码无卡免费| 亚洲国产精品成人综合色在线婷婷| 99在线视频免费| 亚洲欧洲尹人香蕉综合| 中文字幕无码播放免费| 亚洲色欲色欲www在线播放 | 亚洲精品黄色视频在线观看免费资源| 免费在线观看视频a| 国产精品免费大片一区二区| 亚洲精品无码av天堂| 中文字幕高清免费不卡视频| 亚洲AV无码一区东京热久久 | 免费观看成人久久网免费观看| 亚洲国产第一站精品蜜芽| 久久免费视频99| 亚洲午夜电影一区二区三区| 免费观看一级毛片| 深夜特黄a级毛片免费播放| 国产专区一va亚洲v天堂| 久草福利资源网站免费| 亚洲精品第一综合99久久| 成人爱做日本视频免费| 在线观看免费视频网站色| 亚洲精品午夜久久久伊人| 午夜色a大片在线观看免费| 一个人看的www视频免费在线观看| 亚洲VA中文字幕无码一二三区| 啦啦啦完整版免费视频在线观看 | 亚洲国产日韩一区高清在线| 国产精品成人免费福利| 国产成人精品亚洲| 在线电影你懂的亚洲| 免费人成无码大片在线观看| 日本一卡精品视频免费 |