---
title: "使用axis2的方式访问天气预报的web service"
author: "bcworld"
date: "2012-01-14T21:45:24Z"
tags: []
categories: ["互联网"]
slug: "axis2-web-service"
draft: false
lastmod: "2012-01-16T12:48:41Z"
id: 1346497
url: "https://bcworld.iteye.com/blog/1346497"
---

实际开发中可能会调用各种web service,常用的web service可以在这个网站上查找到：[http://www.webxml.com.cn/zh\_cn/index.aspx](http://www.webxml.com.cn/zh_cn/index.aspx) ，下面通过axis2的方式编写访问天气预报服务的代码：

![](http://dl.iteye.com/upload/attachment/0062/1371/ca247ca1-0ff1-32f6-bdaf-6929833f636f.png)

编写客户端代码时需要参看几个量，如地址，请求结构等，这些信息可以参看服务的WSDL和示例：

![](http://dl.iteye.com/upload/attachment/0062/1373/3f056c55-b3fd-348f-bf68-b7eb6967dc60.png)

有了这些信息就可以编写代码调用天气预报服务了，注意需要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.  
&lt;getWeatherResponse xmlns="http://WebXml.com.cn/"&gt;&lt;getWeatherResult&gt;&lt;string&gt;**江苏 南京** &lt;/string&gt;&lt;string&gt;**南京** &lt;/string&gt;&lt;string&gt;1944&lt;/string&gt;&lt;string&gt;**2012/01/14 21:21:12** &lt;/string&gt;&lt;string&gt;今日天气实况：气温：3℃；风向/风力：东风 2级；湿度：90%&lt;/string&gt;&lt;string&gt;空气质量：良；紫外线强度：最弱&lt;/string&gt;&lt;string&gt;穿衣指数：天气冷，建议着棉衣、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣或冬大衣。  
感冒指数：天气较凉，较易发生感冒，请适当增加衣服。体质较弱的朋友尤其应该注意防护。  
运动指数：有降水，风力较强，较适宜在户内开展各种健身和休闲运动，若坚持户外运动，请注意保暖。  
洗车指数：不宜洗车，未来24小时内有雨，如果在此期间洗车，雨水和路上的泥水可能会再次弄脏您的爱车。  
晾晒指数：有降水，不适宜晾晒。若需要晾晒，请在室内准备出充足的空间。  
旅游指数：天气稍凉，风稍大会加大些凉意，且预报有小雨，旅游指数一般，外出旅游请注意防风保暖并携带雨具。  
路况指数：有降水，路面潮湿，车辆易打滑，请小心驾驶。  
舒适度指数：有降水，人们会感到有些凉意，不过大部分人仍会有比较舒适的感觉。  
空气污染指数：气象条件有利于空气污染物稀释、扩散和清除，可在室外正常活动。  
紫外线指数：属弱紫外线辐射天气，无需特别防护。若长期在户外，建议涂擦SPF在8-12之间的防晒护肤品。&lt;/string&gt;&lt;string&gt;1月15日 小雨转阴&lt;/string&gt;&lt;string&gt;2℃/6℃&lt;/string&gt;&lt;string&gt;东风3-4级&lt;/string&gt;&lt;string&gt;7.gif&lt;/string&gt;&lt;string&gt;2.gif&lt;/string&gt;&lt;string&gt;1月16日 多云&lt;/string&gt;&lt;string&gt;1℃/8℃&lt;/string&gt;&lt;string&gt;东南风3-4级&lt;/string&gt;&lt;string&gt;1.gif&lt;/string&gt;&lt;string&gt;1.gif&lt;/string&gt;&lt;string&gt;1月17日 多云转小雨&lt;/string&gt;&lt;string&gt;3℃/8℃&lt;/string&gt;&lt;string&gt;东南风3-4级&lt;/string&gt;&lt;string&gt;1.gif&lt;/string&gt;&lt;string&gt;7.gif&lt;/string&gt;&lt;string&gt;1月18日 小雨&lt;/string&gt;&lt;string&gt;3℃/6℃&lt;/string&gt;&lt;string&gt;东风4-5级&lt;/string&gt;&lt;string&gt;7.gif&lt;/string&gt;&lt;string&gt;7.gif&lt;/string&gt;&lt;string&gt;1月19日 小雨&lt;/string&gt;&lt;string&gt;3℃/7℃&lt;/string&gt;&lt;string&gt;东风4-5级转东北风4-5级&lt;/string&gt;&lt;string&gt;7.gif&lt;/string&gt;&lt;string&gt;7.gif&lt;/string&gt;&lt;/getWeatherResult&gt;&lt;/getWeatherResponse&gt;

 

最后**提醒** 下大家，不要在一天内多次访问该服务，它是有访问次数限制的，而且有的服务并不是free of charge！