`
xitong
  • 浏览: 6196986 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

分类:Web Service840人阅读评论(1)收藏举报

今天我们来实现一个通过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

参考代码如下:

  1. packagecom.weather.webservice;
  2. importorg.apache.axiom.om.OMAbstractFactory;
  3. importorg.apache.axiom.om.OMElement;
  4. importorg.apache.axiom.om.OMFactory;
  5. importorg.apache.axiom.om.OMNamespace;
  6. importorg.apache.axiom.soap.SOAP11Constants;
  7. importorg.apache.axis2.Constants;
  8. importorg.apache.axis2.addressing.EndpointReference;
  9. importorg.apache.axis2.client.Options;
  10. importorg.apache.axis2.client.ServiceClient;
  11. importorg.apache.axis2.transport.http.HTTPConstants;
  12. importorg.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;
  13. importorg.apache.commons.logging.Log;
  14. importorg.apache.commons.logging.LogFactory;
  15. publicclassTest
  16. {
  17. privatestaticEndpointReferencetargetEPR=newEndpointReference(
  18. "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
  19. publicvoidgetResult()throwsException
  20. {
  21. ServiceClientsender=newServiceClient();
  22. sender.setOptions(buildOptions());
  23. OMElementresult=sender.sendReceive(buildParam());
  24. System.out.println(result);
  25. }
  26. privatestaticOMElementbuildParam()
  27. {
  28. OMFactoryfac=OMAbstractFactory.getOMFactory();
  29. OMNamespaceomNs=fac.createOMNamespace("http://WebXml.com.cn/","");
  30. OMElementdata=fac.createOMElement("getWeatherbyCityName",omNs);
  31. OMElementinner=fac.createOMElement("theCityName",omNs);
  32. inner.setText("北京");
  33. data.addChild(inner);
  34. returndata;
  35. }
  36. privatestaticOptionsbuildOptions()
  37. {
  38. Optionsoptions=newOptions();
  39. options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
  40. options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
  41. options.setTo(targetEPR);
  42. //options.setProperty如果不是通过代理上网,此句可省
  43. //options.setProperty(HTTPConstants.PROXY,buildProxy());
  44. options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
  45. returnoptions;
  46. }
  47. /**
  48. *本机采用代理服务器上网时,需要设置代理
  49. *@return
  50. */
  51. publicstaticProxyPropertiesbuildProxy()
  52. {
  53. ProxyPropertiesproxyProperties=newProxyProperties();
  54. proxyProperties.setProxyName("代理名称");
  55. proxyProperties.setProxyPort(8080);
  56. returnproxyProperties;
  57. }
  58. publicstaticvoidmain(String[]args)throwsException
  59. {
  60. Tests=newTest();
  61. s.getResult();
  62. }
  63. }

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>

今天我们来实现一个通过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

参考代码如下:

  1. packagecom.weather.webservice;
  2. importorg.apache.axiom.om.OMAbstractFactory;
  3. importorg.apache.axiom.om.OMElement;
  4. importorg.apache.axiom.om.OMFactory;
  5. importorg.apache.axiom.om.OMNamespace;
  6. importorg.apache.axiom.soap.SOAP11Constants;
  7. importorg.apache.axis2.Constants;
  8. importorg.apache.axis2.addressing.EndpointReference;
  9. importorg.apache.axis2.client.Options;
  10. importorg.apache.axis2.client.ServiceClient;
  11. importorg.apache.axis2.transport.http.HTTPConstants;
  12. importorg.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;
  13. importorg.apache.commons.logging.Log;
  14. importorg.apache.commons.logging.LogFactory;
  15. publicclassTest
  16. {
  17. privatestaticEndpointReferencetargetEPR=newEndpointReference(
  18. "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
  19. publicvoidgetResult()throwsException
  20. {
  21. ServiceClientsender=newServiceClient();
  22. sender.setOptions(buildOptions());
  23. OMElementresult=sender.sendReceive(buildParam());
  24. System.out.println(result);
  25. }
  26. privatestaticOMElementbuildParam()
  27. {
  28. OMFactoryfac=OMAbstractFactory.getOMFactory();
  29. OMNamespaceomNs=fac.createOMNamespace("http://WebXml.com.cn/","");
  30. OMElementdata=fac.createOMElement("getWeatherbyCityName",omNs);
  31. OMElementinner=fac.createOMElement("theCityName",omNs);
  32. inner.setText("北京");
  33. data.addChild(inner);
  34. returndata;
  35. }
  36. privatestaticOptionsbuildOptions()
  37. {
  38. Optionsoptions=newOptions();
  39. options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
  40. options.setAction("http://WebXml.com.cn/getWeatherbyCityName");
  41. options.setTo(targetEPR);
  42. //options.setProperty如果不是通过代理上网,此句可省
  43. //options.setProperty(HTTPConstants.PROXY,buildProxy());
  44. options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
  45. returnoptions;
  46. }
  47. /**
  48. *本机采用代理服务器上网时,需要设置代理
  49. *@return
  50. */
  51. publicstaticProxyPropertiesbuildProxy()
  52. {
  53. ProxyPropertiesproxyProperties=newProxyProperties();
  54. proxyProperties.setProxyName("代理名称");
  55. proxyProperties.setProxyPort(8080);
  56. returnproxyProperties;
  57. }
  58. publicstaticvoidmain(String[]args)throwsException
  59. {
  60. Tests=newTest();
  61. s.getResult();
  62. }
  63. }

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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics