webservices系列(三)——呼叫線上webservice(天氣預報和號碼查詢)
阿新 • • 發佈:2019-01-07
天氣預報,這個介面是.net平臺的,直接用wsimport 生成程式碼會報錯,需要儲存本地修改
瀏覽器開啟http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl,另存為weather.wsdl,然後將檔案中所有 <s:element ref="s:schema" /><s:any /> 替換成 <s:any minOccurs="2" maxOccurs="2"/>即可。
再次使用wsimport命令生成客戶端程式碼,這次使用本地修改好的WSDL檔案來生成命令如下:wsimport -p com.lc.client3 -keep H:\workspace1.8\web_service_test2\src\com\lc\weather.wsdl
可以看到在當前目錄下生成了很多Java類,將這些類複製到eclipse專案
下面編寫一個測試類來呼叫生成的客戶端程式碼,獲取天氣預報資訊
執行結果 手機號碼詳情查詢public class WeatherClent { public static void main(String[] args) { WeatherClent clent = new WeatherClent(); clent.printWeathInfo("深圳"); } /** * 天氣查詢 * @param cname */ public void printWeathInfo(String cname) { WeatherWS wws = new WeatherWS(); WeatherWSSoap wwssoap = wws.getWeatherWSSoap(); ArrayOfString aString = wwssoap.getWeather(cname, null); List<String> ls = aString.getString(); for(String s : ls) { System.out.println(s); } } }
使用wsimport生成客戶端程式碼
wsimport -p com.lc.client3.mobile -keep http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
將生成的程式碼複製到eclipse對應的包下面
編寫測試類
執行結果public class WeatherClent { public static void main(String[] args) { WeatherClent clent = new WeatherClent(); clent.printWeathInfo("深圳"); } /** * 天氣查詢 * @param cname */ public void printWeathInfo(String cname) { WeatherWS wws = new WeatherWS(); WeatherWSSoap wwssoap = wws.getWeatherWSSoap(); ArrayOfString aString = wwssoap.getWeather(cname, null); List<String> ls = aString.getString(); for(String s : ls) { System.out.println(s); } } }
webservices系列參考資料
[1].webservice搭建和檔案上傳下載:http://wuhongyu.iteye.com/blog/807470
[2].天氣預報和手機號資訊查詢:https://my.oschina.net/liu13430/blog/373940?fromerr=WmdtQOoY
[3].axis2建立例項:http://clq9761.iteye.com/blog/976029/
[4].axis2整合web專案:http://wangronaldo.iteye.com/blog/1456441
[5].xml配置詳情:http://blog.csdn.net/guihaijinfen/article/details/8363839