实际开发中可能会调用各种web service,常用的web service可以在这个网站上查找到:http://www.webxml.com.cn/zh_cn/index.aspx
,下面通过axis2的方式编写访问天气预报服务的代码:
编写客户端代码时需要参看几个量,如地址,请求结构等,这些信息可以参看服务的WSDL和示例:
有了这些信息就可以编写代码调用天气预报服务了,注意需要axis2的jar包支持:
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
* 调用天气预报
*/
public class Weather {
public static void main(String[] args) {
Weather client = new Weather();
client.testDocument();
}
public void testDocument() {
try {
ServiceClient sc = new ServiceClient();
Options opts = new Options();
// 确定目标服务地址
// location
opts.setTo(new EndpointReference(
"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx"));
// 确定调用方法
// 对应soapAction
opts.setAction("http://WebXml.com.cn/getWeather");
opts.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
sc.setOptions(opts);
// 发送请求并并得到返回结果,注意参数生成方法的分析
OMElement res = sc.sendReceive(createPayLoad());
// 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。
res.getFirstElement().getText();
System.out.println(res);
} catch (AxisFault e) {
e.printStackTrace();
}
}
public static OMElement createPayLoad() {
OMFactory fac = OMAbstractFactory.getOMFactory();
// 指定命名空间
OMNamespace omNs = fac
.createOMNamespace("http://WebXml.com.cn/", "nsl");
// 指定方法
OMElement method = fac.createOMElement("getWeather", omNs);
OMElement p1 = fac.createOMElement("theCityCode", omNs);
OMElement p2 = fac.createOMElement("userID", omNs);
method.addChild(p1);
method.addChild(p2);
p1.setText("南京");
// 返回方法(实际上就是OMElement封装的xml字符串)
return method;
}
}
程序运行结果如下:
log4j:WARN No appenders could be found for logger (org.apache.axis2.context.AbstractContext).
log4j:WARN Please initialize the log4j system properly.
<getWeatherResponse xmlns="http://WebXml.com.cn/"><getWeatherResult><string>江苏 南京
</string><string>南京
</string><string>1944</string><string>2012/01/14 21:21:12
</string><string>今日天气实况:气温:3℃;风向/风力:东风 2级;湿度:90%</string><string>空气质量:良;紫外线强度:最弱</string><string>穿衣指数:天气冷,建议着棉衣、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣或冬大衣。
感冒指数:天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。
运动指数:有降水,风力较强,较适宜在户内开展各种健身和休闲运动,若坚持户外运动,请注意保暖。
洗车指数:不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。
晾晒指数:有降水,不适宜晾晒。若需要晾晒,请在室内准备出充足的空间。
旅游指数:天气稍凉,风稍大会加大些凉意,且预报有小雨,旅游指数一般,外出旅游请注意防风保暖并携带雨具。
路况指数:有降水,路面潮湿,车辆易打滑,请小心驾驶。
舒适度指数:有降水,人们会感到有些凉意,不过大部分人仍会有比较舒适的感觉。
空气污染指数:气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。
紫外线指数:属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。</string><string>1月15日 小雨转阴</string><string>2℃/6℃</string><string>东风3-4级</string><string>7.gif</string><string>2.gif</string><string>1月16日 多云</string><string>1℃/8℃</string><string>东南风3-4级</string><string>1.gif</string><string>1.gif</string><string>1月17日 多云转小雨</string><string>3℃/8℃</string><string>东南风3-4级</string><string>1.gif</string><string>7.gif</string><string>1月18日 小雨</string><string>3℃/6℃</string><string>东风4-5级</string><string>7.gif</string><string>7.gif</string><string>1月19日 小雨</string><string>3℃/7℃</string><string>东风4-5级转东北风4-5级</string><string>7.gif</string><string>7.gif</string></getWeatherResult></getWeatherResponse>
最后提醒
下大家,不要在一天内多次访问该服务,它是有访问次数限制的,而且有的服务并不是free of charge!
- 大小: 11.5 KB
- 大小: 4.9 KB
分享到:
相关推荐
用axis2开发web service,过程比用axis1开发web service简单
java基于Axis2调用C#开发的web service获取xml结果
这是我在myeclipse中用axis2使用网上提供的天气预报服务的简单demo。你可以在普通的java类中,直接将该代码复制过去即可,当然不要忘了导入axis的jar包。axis的jar包和网上的web service地址,在我的资源和博客中都...
web service 使用 axis2 框架使用的插件 必不可少的
在MyEclipse环境下配置Axis2的详细步骤与web service简单程序应用 适合初学者使用!!
基于AXIS2实现Web Service开发
Axis开发Web Service实例,介绍AXIS的三种方法
.Web Service简介 .如何开发一个Web Service .Axis2简介 .Axis2安装与部署 .Web Service开发步骤
Axis2与Eclipse整合开发的Web Service的服务端
Axis2与Eclipse整合开发Web Service之一:简单的计算服务例子
Axis2创建web service快速入门
axisClient Maven Webapp是一个maven web工程,在这个工程里面,介绍了2种axis2访问服务端接口的方式。第三种方式是利用axis2插件,将wsdl文件自动生成客户端代码,此种方式在myeclipse安装axis2.txt文件中已经做了...
WebService大讲堂之Axis2,Axis2教程,axis2教程例子,Web_Service实例应用
1.1 Axis2(Eclipse)插件的安装。 1.2 Axis2.war的安装。 1.3 Eclipse Build Path。 二、 Web Service服务器端的发布。 2.1 创建java类。 2.2 创建services.xml。 2.3 发布服务。 2.4 在浏览器上测试服务。 三、 ...
本代码使用AXIS2客户端调用,用WSDL2JAVA 生成代码,解决DATASET类型不兼容问题.
Chapter 1, Apache Web Services and Axis2 - Gives you an introduction to web services and the web service stack at Apache. Chapter 2, Looking inside Axis2 - Learn about Axis2 architecture and the ...
axis1.4 spring3.0 集成 实现 web service 服务端, axis1.4 客户端认证,授权,访问日志记录,集成spring 解决 PHP 调用web service 无法认证,和解析soap 模板
Web Service Axis2-1.6.2框架使用 压缩包里有:文档说明,axis2-1.6.2-bin.zip、axis2-1.6.2-war.zip、axis2-eclipse-codegen-plugin-1.6.2.zip、axis2-eclipse-service-plugin-1.6.2.zip。文档里用的所有jar压缩包...
web service Axis2相关jar 里面包含了Axis2 java调用.net接口所需jar包