­

webservices系列(三)——调用线上webservice(天气预报和号码查询)

  • 2019 年 10 月 30 日
  • 筆記

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/luo4105/article/details/69938780

天气预报,这个接口是.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.8web_service_test2srccomlcweather.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