webservice通信调用天气预报接口实例
2016-07-13 22:34
731 查看
一:环境搭建
1:新建一个java project工程weatherInf
2:引入相应的jar包
activation.jar
axis-ant.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar
下载axis 1.4 src压缩包,解压后到webapp/web-info/lib下取包,具体路径如下:
http://download.csdn.net/detail/yyg64/5351114
其中mail.jar 以及 activation.jar 可到如下路径下载:
http://download.csdn.net/detail/dbhunter/398258
3:将天气预报接口wsdl文件拷贝到src目录下
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
二:目录结构
三:根据wsdl文件生成客户端代码
wsdl文件——右键——web services——Generate Client,然后一路next到finish。
会生成如下客户端代码:
四:测试代码
五:得到结果
1:新建一个java project工程weatherInf
2:引入相应的jar包
activation.jar
axis-ant.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar
下载axis 1.4 src压缩包,解压后到webapp/web-info/lib下取包,具体路径如下:
http://download.csdn.net/detail/yyg64/5351114
其中mail.jar 以及 activation.jar 可到如下路径下载:
http://download.csdn.net/detail/dbhunter/398258
3:将天气预报接口wsdl文件拷贝到src目录下
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
二:目录结构
三:根据wsdl文件生成客户端代码
wsdl文件——右键——web services——Generate Client,然后一路next到finish。
会生成如下客户端代码:
四:测试代码
/** * */ package com.paic.services; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import cn.com.WebXml.WeatherWebServiceLocator; import cn.com.WebXml.WeatherWebServiceSoapStub; /** * @author Administrator * */ public class TestWeather { public static void main(String[] args) throws ServiceException, RemoteException { WeatherWebServiceLocator locator = new WeatherWebServiceLocator(); WeatherWebServiceSoapStub service = (WeatherWebServiceSoapStub) locator .getPort(WeatherWebServiceSoapStub.class); invokeGetSupportProvince(service); System.out.println("..................."); invokeGetSupportCity(service); invokeGetWeatherByOneCity(service); } // 调用获取支持的省份、州接口 public static void invokeGetSupportProvince( WeatherWebServiceSoapStub service) throws RemoteException { String[] provices = service.getSupportProvince(); System.out.println("总共" + provices.length + "个"); int count = 0; for (String str : provices) { if (0 != count && count % 5 == 0) { System.out.println(); } System.out.print(str + "\t"); count++; } } // 调用获取支持查询某个省份内的城市接口 public static void invokeGetSupportCity(WeatherWebServiceSoapStub service) throws RemoteException { String provinceName = "江苏"; String[] cities = service.getSupportCity(provinceName); System.out.println("总共" + cities.length + "个市"); for (int i = 0; i < cities.length; i++) { if (0 != i && i % 5 == 0) { System.out.println(); } System.out.print(cities[i] + "\t"); } } // 调用查询某个城市天气的接口 public static void invokeGetWeatherByOneCity( WeatherWebServiceSoapStub service) throws RemoteException { String cityName = "南京"; String[] weatherInfo = service.getWeatherbyCityName(cityName); for (String str : weatherInfo) { System.out.println(str); } } }
五:得到结果
相关文章推荐
- 修改注册表项的权限
- 不用加减乘除做加法中Python存在的bug
- 【poj3261】 Milk Patterns
- jQuery:unbind方法的使用详解
- 学习笔记----如何从零打造一个手游客户端
- c语言中关于n个元素的栈和c1赋不上值的问题
- VC++ 高效、快速给REG_DWORD赋值
- 使用RecyclerView
- IOS中UIScrollView的详细使用
- 使用uiautomator的python封装进行android的UI测试
- vi作者:Bill Joy
- Android RecyclerView Item动画(Checkbox全选、单选)
- html原样输出html代码
- atoi() 函数解析
- SQL中表之间的连接
- Handler Looper MessageQueue
- 快学Scala第20章----Actor
- opencv 图像减色
- MySQL5.7绿色版安装
- QueryCache那些事