題外話:
本程序利用網絡上發布的公共webservice endpoint
www.webxml.com.cn/WebServices/WeatherWebService.asmx
大致步驟是
1? 利用soap向webservice endpoint進行請求,取回請求結果
2? 把結果顯示在web界面上,web界面采用
Java
+Jsp(呵呵,有點丑陋,篇幅所迫)
好,廢話少說,直接進入核心程序講解。
一? WeatherReport類??
??? 方法 1? 構造soap請求(請求格式請見上面的鏈接),用用戶輸入的城市名稱鑲在此請求里面
- /** ?
- ?????*?獲取SOAP的請求頭,并替換其中的標志符號為用戶輸入的城市 ?
- ?????*? ?
- ?????*?編寫者:王景輝 ?
- ?????*? ?
- ?????*?@param?city ?
- ?????*????????????用戶輸入的城市名稱 ?
- ?????*?@return?客戶將要發送給服務器的SOAP請求 ?
- ?????*/ ??
- ???? 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發送上述SOAP請求,并設置一些請求屬性,返回一個服務器端的InputStream(XML文檔流)
- /** ?
- ?????*?用戶把SOAP請求發送給服務器端,并返回服務器點返回的輸入流 ?
- ?????*? ?
- ?????*?編寫者:王景輝 ?
- ?????*? ?
- ?????*?@param?city ?
- ?????*????????????用戶輸入的城市名稱 ?
- ?????*?@return?服務器端返回的輸入流,供客戶端讀取 ?
- ?????*?@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文檔流,并用特定的符號分隔,以便我們在Jsp頁面進行結果分析
- /** ?
- ?????*?對服務器端返回的XML進行解析 ?
- ?????*? ?
- ?????*?編寫者:王景輝 ?
- ?????*? ?
- ?????*?@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( "查詢結果為空!" ))?{ ??
- ????????????????????sb?=? new ?StringBuffer( "#" )?; ??
- ???????????????????? break ?; ??
- ????????????????} ??
- ????????????????sb.append(n.getFirstChild().getNodeValue()?+? "#\n" ); ??
- ????????????} ??
- ????????????is.close(); ??
- ???????????? return ?sb.toString(); ??
- ????????}? catch ?(Exception?e)?{ ??
- ????????????e.printStackTrace(); ??
- ???????????? return ? null ; ??
- ????????} ??
- ????}??
?
二?? weatherInfo.jsp頁面
?????? 核心功能是解析 方法3 所返回的字符串,向endpoint進行請求時,一個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 > 長沙 </ 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 > 西北風 < =3級 </ string > ? ??
- < string > 7.gif </ string > ? ??
- < string > 7.gif </ string > ? ??
- < string > 今日天氣實況:多云;7.4℃;風向/風力:西北風2級;空氣質量:較差;紫外線強度:最弱 </ string > ? ??
- < string > 穿衣指數:感冒指數:溫度較低,較易發生感冒,請適當增加衣服。體質較弱的朋友尤其應該注意防護。晨練指數:早晨天氣陰沉,氣溫極低,請盡量避免戶外晨練,若堅持戶外晨練請注意保暖防凍。交通指數:中暑指數:溫度不高,其他各項氣象條件適宜,中暑機率極低。公園指數:天氣不好,不適宜放風箏。防曬指數:屬弱紫外輻射天氣,長期在戶外,建議涂擦SPF在8-12之間的防曬護膚品。旅行指數:陰天,缺少陽光的陪伴,加上過低的溫度會給出行帶來些不便,旅游指數一般,請您在旅游時注意增加衣物。 </ string > ? ??
- < string > 8℃?/?5℃ </ string > ? ??
- < string > 12月27日?小雨 </ string > ? ??
- < string > 西北風 < =3級 </ string > ? ??
- < string > 7.gif </ string > ? ??
- < string > 7.gif </ string > ? ??
- < string > 10℃?/?4℃ </ string > ? ??
- < string > 12月28日?小雨 </ string > ? ??
- < string > 西北風 < =3級 </ string > ? ??
- < string > 7.gif </ string > ? ??
- < string > 7.gif </ string > ? ??
- < string > 長沙市位于湖南省東部偏北,湘江下游和長瀏盆地西緣。其地域范圍為東經111°53′-114°15′,北緯27°51′-28°41′。東鄰江西省宜春地區和萍鄉市,南接株洲、湘潭兩市,西連婁底、益陽兩市,北抵岳陽、益陽兩市。東西長約230公里,南北寬約88公里。全市土地面積11819.5平方公里,其中城區面積556平方公里。長沙是一座有2000余年悠久文化歷史的古城,早在春秋時期,就是楚國雄踞南方的戰略要地之一。漢朝的劉邦立國之后,于公元前206年改臨江為長沙,并設立漢朝的屬國----長沙國,自此之后,長沙開始筑建城墻,并逐漸成為兵家必爭之地。長沙屬亞熱帶季風性濕潤氣候。氣候特征是:氣候溫和,降水充沛,雨熱同期,四季分明。長沙市區年平均氣溫17.2℃,各縣16.8℃-17.3℃,年積溫為5457℃,市區年均降水量1361.6毫米。景觀:岳麓山、桔子洲、天心閣、烈士公園、月亮島等。 </ string > ? ??
- </ ArrayOfString > ??
在Jsp中解析的代碼如下,基本上是對字符串的操作,截取及截取長度的控制
- //穿衣指數 ??
- ??????????s1?=?str.substring(str.indexOf( "穿衣指數:" ),str.indexOf( "穿衣指數:" )+ 4 )?; ??
- ??????????s1Content?=?str.substring(str.indexOf( "穿衣指數:" )+ 5 ,str.indexOf( "感冒指數:" ))?; ??
- ?????????? //感冒指數 ??
- ??????????s2?=?str.substring(str.indexOf( "感冒指數:" ),str.indexOf( "感冒指數:" )+ 4 )?; ??
- ??????????s2Content?=?str.substring(str.indexOf( "感冒指數:" )+ 5 ,str.indexOf( "晨練指數:" ))?; ??
- ?????????? ??
- ?????????? //晨練指數 ??
- ??????????s3?=?str.substring(str.indexOf( "晨練指數:" ),str.indexOf( "晨練指數:" )+ 4 )?; ??
- ??????????s3Content?=?str.substring(str.indexOf( "晨練指數:" )+ 5 ,str.indexOf( "交通指數:" ))?; ??
- ?????????? //交通指數 ??
- ??????????s7?=?str.substring(str.indexOf( "交通指數:" ),str.indexOf( "交通指數:" )+ 4 )?; ??
- ??????????s7Content?=?str.substring(str.indexOf( "交通指數:" )+ 5 ,str.indexOf( "中暑指數:" ))?; ??
- ?????????? //中暑指數 ??
- ??????????s4?=?str.substring(str.indexOf( "中暑指數:" ),str.indexOf( "中暑指數:" )+ 4 )?; ??
- ??????????s4Content?=?str.substring(str.indexOf( "中暑指數:" )+ 5 ,str.indexOf( "防曬指數:" ))?; ??
- ?????????? //防曬指數 ??
- ??????????s5?=?str.substring(str.indexOf( "防曬指數:" ),str.indexOf( "防曬指數:" )+ 4 )?; ??
- ??????????s5Content?=?str.substring(str.indexOf( "防曬指數:" )+ 5 ,str.indexOf( "旅行指數:" ))?; ??
- ?????????? //旅行指數 ??
- ??????????s6?=?str.substring(str.indexOf( "旅行指數:" ),str.indexOf( "旅行指數:" )+ 4 )?; ??
- ??????????s6Content?=?str.substring(str.indexOf( "旅行指數:" )+ 5 )?;??
程序運行效果見附件上的截圖!!!運行附件: http://localhost:8080/yourProject/tianqi.jsp
好了,基本上核心代碼就是上邊那些了!不僅如此,加入我們想要在自己的系統里加入飛機票,火車票,股票信息等等之類的功能,只要有相應的webservice,我們都可以實現(呵呵,好像免費的少哦),各位有什么疑問,留言吧!!!
- 天氣預報源程序.rar (265.2 KB)
- 描述: 源程序
- 下載次數: 719
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
