您的位置:首页 > 其它

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。

会生成如下客户端代码:



四:测试代码

/**
*
*/
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);
}
}
}


五:得到结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: