博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7.Web Service 调用天气代码
阅读量:7291 次
发布时间:2019-06-30

本文共 2425 字,大约阅读时间需要 8 分钟。

1.

1 2500多个城市天气预报 WEB服务公用事业 2 Endpoint:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx 3 Disco:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?disco 4 WSDL:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 5  6 代码如下: 7  8 import java.rmi.RemoteException; 9 10 import javax.xml.namespace.QName;11 import javax.xml.rpc.ServiceException;12 13 import org.apache.axis.client.Call;14 import org.apache.axis.client.Service;15 16 17 18 19 public class Weather {20     public static void main(String[] args) throws ServiceException,21             RemoteException {22         String cityCode = "北京";23         String userId = "";24         // http://WebXml.com.cn/是wsdl中definitions根节点的targetNamespace属性25         // webservice路径26         // 这里后面加不加 "?wsdl" 效果都一样的27         String endpoint = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";28         String[] res = null;29 30         // 查询城市天气的接口方法名31         String operationName = "getWeather";32         // 定义service对象33         Service service = new Service();34         // 创建一个call对象35         Call call = (Call) service.createCall();36         // 设置目标地址,即webservice路径37         call.setTargetEndpointAddress(endpoint);38         // 设置操作名称,即方法名称39         call.setOperationName(new QName("http://WebXml.com.cn/", operationName));40         // 设置方法参数41         call.addParameter(new QName("http://WebXml.com.cn/", "theCityCode"),42                 org.apache.axis.encoding.XMLType.XSD_STRING,43                 javax.xml.rpc.ParameterMode.IN);44         call.addParameter(new QName("http://WebXml.com.cn/", "theUserID"),45                 org.apache.axis.encoding.XMLType.XSD_STRING,46                 javax.xml.rpc.ParameterMode.IN);47         // 设置返回值类型48         // 对于返回是字符串数组的返回类型只有这两种可行49 50         // call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_VECTOR);51         call.setReturnClass(java.lang.String[].class);52 53         call.setUseSOAPAction(true);54         call.setSOAPActionURI("http://WebXml.com.cn/" + "getWeather");55 56         res = (String[]) call.invoke(new Object[] { cityCode, userId });57 58         // 如果返回类型是org.apache.axis.encoding.XMLType.SOAP_VECTOR时用下面的转型接收59         // Vector v=(Vector) call.invoke(new Object[]{cityCode,userId});60         for (String str : res) {61             System.out.println(str);62         }63     }64 }

 

转载于:https://www.cnblogs.com/sharpest/p/7856046.html

你可能感兴趣的文章
HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面。
查看>>
ELK+Filebeat+Nginx集中式日志解决方案(三)—— 添加kafka+zookeeper集群
查看>>
动态加载动态库(C#)反射
查看>>
iOS UI-利用可视化(StoryBoard)在Scroll View的内容中添加一个TableView和一个ColletionView...
查看>>
为了学好linux 特此写下决心书!
查看>>
使用SpringMVC拦截器做一个简单的权限控制
查看>>
Android学习——Animation动画效果
查看>>
BI实时图表实现数据可视化的原理
查看>>
OpenGG.Clean.Player{喜歡看視頻用戶強推}
查看>>
不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。...
查看>>
现有IOS设备唯一标示符的方案比较
查看>>
解决Unable to load R3
查看>>
跟 UMU 一起玩 OpenWRT(入门篇7):安装 LUCI
查看>>
qml扇形角度动态渲染控件封装
查看>>
基于Android平台简易即时通讯的研究与设计
查看>>
OAuth 2.0 认证的原理与实践
查看>>
asp.net后台调用前台js代码
查看>>
KDE一周工作纪要(2011/9/11~2011/9/17)
查看>>
thinkphp 3.2 auth 源代码说明
查看>>
tomcat 安全记述
查看>>