webservice通信调用天气预报接口实例

时间:2021-08-16 11:38:27

转载:http://www.cnblogs.com/warrior4236/p/5668449.html

一:环境搭建

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

二:目录结构

webservice通信调用天气预报接口实例

三:根据wsdl文件生成客户端代码

wsdl文件——右键——web services——Generate Client,然后一路next到finish。

会生成如下客户端代码:

webservice通信调用天气预报接口实例

四:测试代码

webservice通信调用天气预报接口实例
 1 /**
2 *
3 */
4 package com.paic.services;
5
6 import java.rmi.RemoteException;
7
8 import javax.xml.rpc.ServiceException;
9
10 import cn.com.WebXml.WeatherWebServiceLocator;
11 import cn.com.WebXml.WeatherWebServiceSoapStub;
12
13 /**
14 * @author Administrator
15 *
16 */
17 public class TestWeather {
18 public static void main(String[] args) throws ServiceException,
19 RemoteException {
20 WeatherWebServiceLocator locator = new WeatherWebServiceLocator();
21 WeatherWebServiceSoapStub service = (WeatherWebServiceSoapStub) locator
22 .getPort(WeatherWebServiceSoapStub.class);
23 invokeGetSupportProvince(service);
24 System.out.println("...................");
25 invokeGetSupportCity(service);
26 invokeGetWeatherByOneCity(service);
27 }
28
29 // 调用获取支持的省份、州接口
30 public static void invokeGetSupportProvince(
31 WeatherWebServiceSoapStub service) throws RemoteException {
32 String[] provices = service.getSupportProvince();
33 System.out.println("总共" + provices.length + "个");
34 int count = 0;
35 for (String str : provices) {
36 if (0 != count && count % 5 == 0) {
37 System.out.println();
38 }
39 System.out.print(str + "\t");
40 count++;
41 }
42 }
43
44 // 调用获取支持查询某个省份内的城市接口
45 public static void invokeGetSupportCity(WeatherWebServiceSoapStub service)
46 throws RemoteException {
47 String provinceName = "江苏";
48 String[] cities = service.getSupportCity(provinceName);
49 System.out.println("总共" + cities.length + "个市");
50 for (int i = 0; i < cities.length; i++) {
51 if (0 != i && i % 5 == 0) {
52 System.out.println();
53 }
54 System.out.print(cities[i] + "\t");
55 }
56 }
57
58 // 调用查询某个城市天气的接口
59 public static void invokeGetWeatherByOneCity(
60 WeatherWebServiceSoapStub service) throws RemoteException {
61 String cityName = "南京";
62 String[] weatherInfo = service.getWeatherbyCityName(cityName);
63 for (String str : weatherInfo) {
64 System.out.println(str);
65 }
66 }
67 }
webservice通信调用天气预报接口实例

五:得到结果

webservice通信调用天气预报接口实例