您的位置:首页 > 其它

Web服务入门之二:通过axis2调用远程Web Service,实现天气预报功能

2010-04-10 15:47 911 查看
今天我们来实现一个通过AXIS2来调用中国气象局的一个实现天气预报功能的Web Service。

准备工作:

1.下载axis2-1.4.1的bin包,地址:http://apache.etoak.com/ws/axis2/1_5/axis2-1.5-bin.zip

2.解压,将lib目录下的所有的jar包拷贝出来,在这里建议大家在自己的电脑上专门建立一个文件夹放jar包,比如:jdbc的jar包,Struts的jar包,spring的jar包等等,这里,我们将axis2的jar包专门拷贝出来放在一个目录下。

接下来的步骤是:

1、new java project

2、引入刚才axis2的所有jar包

3、new class

参考代码如下:

package com.weather.webservice;

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.SOAP11Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Test
{
private static EndpointReference targetEPR = new EndpointReference(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");

public void getResult() throws Exception
{
ServiceClient sender = new ServiceClient();
sender.setOptions(buildOptions());
OMElement result = sender.sendReceive(buildParam());
System.out.println(result);
}

private static OMElement buildParam()
{
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
OMElement data = fac.createOMElement("getWeatherbyCityName", omNs);
OMElement inner = fac.createOMElement("theCityName", omNs);
inner.setText("北京");
data.addChild(inner);
return data;
}

private static Options buildOptions()
{
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
options.setTo(targetEPR);
//options.setProperty 如果不是通过代理上网,此句可省
//options.setProperty(HTTPConstants.PROXY, buildProxy());
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
return options;
}

/**
* 本机采用代理服务器上网时,需要设置代理
* @return
*/
public static ProxyProperties buildProxy()
{
ProxyProperties proxyProperties = new ProxyProperties();
proxyProperties.setProxyName("代理名称");
proxyProperties.setProxyPort(8080);
return proxyProperties;
}

public static void main(String[] args) throws Exception
{
Test s = new Test();
s.getResult();
}

}


Result:

- Deploying module: metadataExchange-1.5 - file:/D:/jars/axis2/mex-1.5.jar
- Required credentials not available for BASIC <any realm>@www.webxml.com.cn:80
- Preemptive authentication requested but no default credentials available
<getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/"><getWeatherbyCityNameResult><string>直辖市</string><string>北京</string><string>54511</string><string>54511.jpg</string><string>2010-4-10 15:45:38</string><string>4℃/15℃</string><string>4月10日 多云转阴</string><string>无持续风向微风</string><string>1.gif</string><string>2.gif</string><string>今日天气实况:气温:15℃;风向/风力:西南风 小于3级;湿度:14%;气压:1011.6hPa;空气质量:中;紫外线强度:最弱</string><string>穿衣指数:建议着夹衣加薄羊毛衫等春秋服装。体弱者宜着夹衣加羊毛衫。但昼夜温差较大,注意增减衣服。
感冒指数:天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。
晨练指数:天气晴朗,空气清新,是您晨练的大好时机,建议不同年龄段的人们积极参加户外健身活动。
洗车指数:不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。
晾晒指数:天气晴朗,午后温暖的阳光仍能满足你驱潮消霉杀菌的晾晒需求。
旅游指数:多云,有时云会遮挡住阳光,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,很适宜旅游。
路况指数:晴天,其它条件适宜,路面比较干燥,路况较好。
舒适度指数:白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。</string><string>4℃/9℃</string><string>4月11日 小到中雨转小雨</string><string>无持续风向微风转北风3-4级</string><string>21.gif</string><string>7.gif</string><string>3℃/11℃</string><string>4月12日 晴</string><string>北风4-5级</string><string>0.gif</string><string>0.gif</string><string>北京位于华北平原西北边缘,市中心位于北纬39度,东经116度,四周被河北省围着,东南和天津市相接。全市面积一万六千多平方公里,辖12区6县,人口1100余万。北京为暖温带半湿润大陆性季风气候,夏季炎热多雨,冬季寒冷干燥,春、秋短促,年平均气温10-12摄氏度。北京是世界历史文化名城和古都之一。早在七十万年前,北京周口店地区就出现了原始人群部落“北京人”。而北京建城也已有两千多年的历史,最初见于记载的名字为“蓟”。公元前1045年北京成为蓟、燕等诸侯国的都城;公元前221年秦始皇统一中国以来,北京一直是中国北方重镇和地方中心;自公元938年以来,北京又先后成为辽陪都、金上都、元大都、明清国都。1949年10月1日正式定为中华人民共和国首都。北京具有丰富的旅游资源,对外开放的旅游景点达200多处,有世界上最大的皇宫紫禁城、祭天神庙天坛、皇家花园北海、皇家园林颐和园,还有八达岭、慕田峪、司马台长城以及世界上最大的四合院恭王府等各胜古迹。全市共有文物古迹7309项,其中国家文物保护单位42个,市级文物保护单位222个。北京的市树为国槐和侧柏,市花为月季和菊花。另外,北京出产的象牙雕刻、玉器雕刻、景泰蓝、地毯等传统手工艺品驰誉世界。</string></getWeatherbyCityNameResult></getWeatherbyCityNameResponse>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: