亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

java 實(shí)現(xiàn)天氣預(yù)報(bào)功能

系統(tǒng) 2399 0

題外話:
本程序利用網(wǎng)絡(luò)上發(fā)布的公共webservice endpoint

www.webxml.com.cn/WebServices/WeatherWebService.asmx


大致步驟是
1? 利用soap向webservice endpoint進(jìn)行請(qǐng)求,取回請(qǐng)求結(jié)果
2? 把結(jié)果顯示在web界面上,web界面采用 Java +Jsp(呵呵,有點(diǎn)丑陋,篇幅所迫)
好,廢話少說,直接進(jìn)入核心程序講解。

一? WeatherReport類??
??? 方法 1? 構(gòu)造soap請(qǐng)求(請(qǐng)求格式請(qǐng)見上面的鏈接),用用戶輸入的城市名稱鑲在此請(qǐng)求里面

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

?

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

java 代碼
  1. /** ?
  2. ?????*?用戶把SOAP請(qǐng)求發(fā)送給服務(wù)器端,并返回服務(wù)器點(diǎn)返回的輸入流 ?
  3. ?????*? ?
  4. ?????*?編寫者:王景輝 ?
  5. ?????*? ?
  6. ?????*?@param?city ?
  7. ?????*????????????用戶輸入的城市名稱 ?
  8. ?????*?@return?服務(wù)器端返回的輸入流,供客戶端讀取 ?
  9. ?????*?@throws?Exception ?
  10. ?????*/ ??
  11. ???? private ? static ?InputStream?getSoapInputStream(String?city)? throws ?Exception?{ ??
  12. ???????? try ?{ ??
  13. ????????????String?soap?=?getSoapRequest(city); ??
  14. ???????????? if ?(soap?==? null )?{ ??
  15. ???????????????? return ? null ; ??
  16. ????????????} ??
  17. ????????????URL?url?=? new ?URL( ??
  18. ???????????????????? "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" ); ??
  19. ????????????URLConnection?conn?=?url.openConnection(); ??
  20. ????????????conn.setUseCaches( false ); ??
  21. ????????????conn.setDoInput( true ); ??
  22. ????????????conn.setDoOutput( true ); ??
  23. ??
  24. ????????????conn.setRequestProperty( "Content-Length" ,?Integer.toString(soap ??
  25. ????????????????????.length())); ??
  26. ????????????conn.setRequestProperty( "Content-Type" ,? "text/xml;?charset=utf-8" ); ??
  27. ????????????conn.setRequestProperty( "SOAPAction" , ??
  28. ???????????????????? "http://WebXml.com.cn/getWeatherbyCityName" ); ??
  29. ??
  30. ????????????OutputStream?os?=?conn.getOutputStream(); ??
  31. ????????????OutputStreamWriter?osw?=? new ?OutputStreamWriter(os,? "utf-8" ); ??
  32. ????????????osw.write(soap); ??
  33. ????????????osw.flush(); ??
  34. ????????????osw.close(); ??
  35. ??
  36. ????????????InputStream?is?=?conn.getInputStream(); ??
  37. ???????????? return ?is; ??
  38. ????????}? catch ?(Exception?e)?{ ??
  39. ????????????e.printStackTrace(); ??
  40. ???????????? return ? null ; ??
  41. ????????} ??
  42. ????}??

?

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

java 代碼
  1. /** ?
  2. ?????*?對(duì)服務(wù)器端返回的XML進(jìn)行解析 ?
  3. ?????*? ?
  4. ?????*?編寫者:王景輝 ?
  5. ?????*? ?
  6. ?????*?@param?city ?
  7. ?????*????????????用戶輸入的城市名稱 ?
  8. ?????*?@return?字符串?用,分割 ?
  9. ?????*/ ??
  10. ???? public ? static ?String?getWeather(String?city)?{ ??
  11. ???????? try ?{ ??
  12. ????????????Document?doc; ??
  13. ????????????DocumentBuilderFactory?dbf?=?DocumentBuilderFactory.newInstance(); ??
  14. ????????????dbf.setNamespaceAware( true ); ??
  15. ????????????DocumentBuilder?db?=?dbf.newDocumentBuilder(); ??
  16. ????????????InputStream?is?=?getSoapInputStream(city); ??
  17. ????????????doc?=?db.parse(is); ??
  18. ????????????NodeList?nl?=?doc.getElementsByTagName( "string" ); ??
  19. ????????????StringBuffer?sb?=? new ?StringBuffer(); ??
  20. ???????????? for ?( int ?count?=? 0 ;?count?<?nl.getLength();?count++)?{ ??
  21. ????????????????Node?n?=?nl.item(count); ??
  22. ???????????????? if (n.getFirstChild().getNodeValue().equals( "查詢結(jié)果為空!" ))?{ ??
  23. ????????????????????sb?=? new ?StringBuffer( "#" )?; ??
  24. ???????????????????? break ?; ??
  25. ????????????????} ??
  26. ????????????????sb.append(n.getFirstChild().getNodeValue()?+? "#\n" ); ??
  27. ????????????} ??
  28. ????????????is.close(); ??
  29. ???????????? return ?sb.toString(); ??
  30. ????????}? catch ?(Exception?e)?{ ??
  31. ????????????e.printStackTrace(); ??
  32. ???????????? return ? null ; ??
  33. ????????} ??
  34. ????}??

?

二?? weatherInfo.jsp頁面

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

xml 代碼
  1. <? xml ? version = "1.0" ? encoding = "utf-8" ? ?> ? ??
  2. < ArrayOfString ? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ? xmlns:xsd = "http://www.w3.org/2001/XMLSchema" ? xmlns = "http://WebXml.com.cn/" > ??
  3. < string > 湖南 </ string > ? ??
  4. < string > 長沙 </ string > ? ??
  5. < string > 57687 </ string > ? ??
  6. < string > 57687.jpg </ string > ? ??
  7. < string > 2007-12-26?14:35:26 </ string > ? ??
  8. < string > 7℃?/?6℃ </ string > ? ??
  9. < string > 12月26日?小雨 </ string > ? ??
  10. < string > 西北風(fēng) < =3級(jí) </ string > ? ??
  11. < string > 7.gif </ string > ? ??
  12. < string > 7.gif </ string > ? ??
  13. < string > 今日天氣實(shí)況:多云;7.4℃;風(fēng)向/風(fēng)力:西北風(fēng)2級(jí);空氣質(zhì)量:較差;紫外線強(qiáng)度:最弱 </ string > ? ??
  14. < string > 穿衣指數(shù):感冒指數(shù):溫度較低,較易發(fā)生感冒,請(qǐng)適當(dāng)增加衣服。體質(zhì)較弱的朋友尤其應(yīng)該注意防護(hù)。晨練指數(shù):早晨天氣陰沉,氣溫極低,請(qǐng)盡量避免戶外晨練,若堅(jiān)持戶外晨練請(qǐng)注意保暖防凍。交通指數(shù):中暑指數(shù):溫度不高,其他各項(xiàng)氣象條件適宜,中暑機(jī)率極低。公園指數(shù):天氣不好,不適宜放風(fēng)箏。防曬指數(shù):屬弱紫外輻射天氣,長期在戶外,建議涂擦SPF在8-12之間的防曬護(hù)膚品。旅行指數(shù):陰天,缺少陽光的陪伴,加上過低的溫度會(huì)給出行帶來些不便,旅游指數(shù)一般,請(qǐng)您在旅游時(shí)注意增加衣物。 </ string > ? ??
  15. < string > 8℃?/?5℃ </ string > ? ??
  16. < string > 12月27日?小雨 </ string > ? ??
  17. < string > 西北風(fēng) < =3級(jí) </ string > ? ??
  18. < string > 7.gif </ string > ? ??
  19. < string > 7.gif </ string > ? ??
  20. < string > 10℃?/?4℃ </ string > ? ??
  21. < string > 12月28日?小雨 </ string > ? ??
  22. < string > 西北風(fēng) < =3級(jí) </ string > ? ??
  23. < string > 7.gif </ string > ? ??
  24. < string > 7.gif </ string > ? ??
  25. < string > 長沙市位于湖南省東部偏北,湘江下游和長瀏盆地西緣。其地域范圍為東經(jīng)111°53′-114°15′,北緯27°51′-28°41′。東鄰江西省宜春地區(qū)和萍鄉(xiāng)市,南接株洲、湘潭兩市,西連婁底、益陽兩市,北抵岳陽、益陽兩市。東西長約230公里,南北寬約88公里。全市土地面積11819.5平方公里,其中城區(qū)面積556平方公里。長沙是一座有2000余年悠久文化歷史的古城,早在春秋時(shí)期,就是楚國雄踞南方的戰(zhàn)略要地之一。漢朝的劉邦立國之后,于公元前206年改臨江為長沙,并設(shè)立漢朝的屬國----長沙國,自此之后,長沙開始筑建城墻,并逐漸成為兵家必爭之地。長沙屬亞熱帶季風(fēng)性濕潤氣候。氣候特征是:氣候溫和,降水充沛,雨熱同期,四季分明。長沙市區(qū)年平均氣溫17.2℃,各縣16.8℃-17.3℃,年積溫為5457℃,市區(qū)年均降水量1361.6毫米。景觀:岳麓山、桔子洲、天心閣、烈士公園、月亮島等。 </ string > ? ??
  26. </ ArrayOfString > ??

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

java 代碼
  1. //穿衣指數(shù) ??
  2. ??????????s1?=?str.substring(str.indexOf( "穿衣指數(shù):" ),str.indexOf( "穿衣指數(shù):" )+ 4 )?; ??
  3. ??????????s1Content?=?str.substring(str.indexOf( "穿衣指數(shù):" )+ 5 ,str.indexOf( "感冒指數(shù):" ))?; ??
  4. ?????????? //感冒指數(shù) ??
  5. ??????????s2?=?str.substring(str.indexOf( "感冒指數(shù):" ),str.indexOf( "感冒指數(shù):" )+ 4 )?; ??
  6. ??????????s2Content?=?str.substring(str.indexOf( "感冒指數(shù):" )+ 5 ,str.indexOf( "晨練指數(shù):" ))?; ??
  7. ?????????? ??
  8. ?????????? //晨練指數(shù) ??
  9. ??????????s3?=?str.substring(str.indexOf( "晨練指數(shù):" ),str.indexOf( "晨練指數(shù):" )+ 4 )?; ??
  10. ??????????s3Content?=?str.substring(str.indexOf( "晨練指數(shù):" )+ 5 ,str.indexOf( "交通指數(shù):" ))?; ??
  11. ?????????? //交通指數(shù) ??
  12. ??????????s7?=?str.substring(str.indexOf( "交通指數(shù):" ),str.indexOf( "交通指數(shù):" )+ 4 )?; ??
  13. ??????????s7Content?=?str.substring(str.indexOf( "交通指數(shù):" )+ 5 ,str.indexOf( "中暑指數(shù):" ))?; ??
  14. ?????????? //中暑指數(shù) ??
  15. ??????????s4?=?str.substring(str.indexOf( "中暑指數(shù):" ),str.indexOf( "中暑指數(shù):" )+ 4 )?; ??
  16. ??????????s4Content?=?str.substring(str.indexOf( "中暑指數(shù):" )+ 5 ,str.indexOf( "防曬指數(shù):" ))?; ??
  17. ?????????? //防曬指數(shù) ??
  18. ??????????s5?=?str.substring(str.indexOf( "防曬指數(shù):" ),str.indexOf( "防曬指數(shù):" )+ 4 )?; ??
  19. ??????????s5Content?=?str.substring(str.indexOf( "防曬指數(shù):" )+ 5 ,str.indexOf( "旅行指數(shù):" ))?; ??
  20. ?????????? //旅行指數(shù) ??
  21. ??????????s6?=?str.substring(str.indexOf( "旅行指數(shù):" ),str.indexOf( "旅行指數(shù):" )+ 4 )?; ??
  22. ??????????s6Content?=?str.substring(str.indexOf( "旅行指數(shù):" )+ 5 )?;??

程序運(yùn)行效果見附件上的截圖!!!運(yùn)行附件: http://localhost:8080/yourProject/tianqi.jsp

好了,基本上核心代碼就是上邊那些了!不僅如此,加入我們想要在自己的系統(tǒng)里加入飛機(jī)票,火車票,股票信息等等之類的功能,只要有相應(yīng)的webservice,我們都可以實(shí)現(xiàn)(呵呵,好像免費(fèi)的少哦),各位有什么疑問,留言吧!!!

  • D9e075b4-57ec-47c4-be33-de8914f5e307-thumb
  • 描述: 天氣預(yù)報(bào)效果圖
  • 大小: 1.6 MB

java 實(shí)現(xiàn)天氣預(yù)報(bào)功能


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产成人综合欧美精品久久 | 国产精品麻豆久久久 | 亚洲美女视频网址 | 91青青青国产在观免费影视 | 欧美性xxxx偷拍 | 久久久国产99久久国产久 | 欧美一区二区三区久久久 | 91精品视频在线播放 | 京野结衣免费一区二区 | 日韩中文欧美 | 草久免费视频 | 在线观看亚洲成人 | 久热国产精品 | 国产成人亚洲精品老王 | 尤物精品国产福利网站 | 日本一线一区二区三区免费视频 | 欧美亚洲国产精品第一页 | 久久免费精品高清麻豆 | 亚洲视频 在线观看 | 色偷偷久久一区二区三区 | 国产高清国产专区国产精品 | 久久er热这里只有精品免费 | 国产a级高清版毛片 | 欧美日本一区亚洲欧美一区 | 人人揉揉香蕉大免费不卡 | 久久精品操| 天天干天天做天天操 | 99精品国产兔费观看久久99 | 国产精品成人h片在线 | 国产91在线九色 | 中文国产成人精品少久久 | 亚欧洲精品在线视频免费观看 | 成人爽a毛片在线视频 | 高清亚洲| 免费超爽大片黄网站 | 天天干天天拍天天操 | 国产色吧 | 欧美日本另类xxx乱大交 | 韩国精品欧美一区二区三区 | 国产一区二区精品久 | 成年女人18毛片毛片免费 |