`
bcworld
  • 浏览: 37926 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

使用axis2的方式访问天气预报的web service

阅读更多

实际开发中可能会调用各种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
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics