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

来源:互联网 发布:网络传播理论 编辑:程序博客网 时间:2024/05/21 10:06

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


0 0
原创粉丝点击