H__D  

  互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为例进行说明。

  气象中心的管理系统将收集的天气信息并将数据暴露出来(通过WebService Server), 而各大站点的应用就去调用它们得到天气信息并以不同的样式去展示(WebService Client)。

调用免费的WebService获取天气预报信息

  1、在网络上查询免费的天气预报webservice的地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ,浏览器访问结果如下:
    

  2、wsdl文件地址:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
    

  3、新建一个java工程,使用wsimport工具自动生成客户端代码:
    创建java工程
      
    使用wsimport工具生成代码,如下:
      
    生成客户端代码报错,这个因为WebService服务端使用.net写的,生成java客户端代码报错。需要修改wsdl文件中的部分内容,操作如下:
      a.将对应的wsdl文档保存到本地
      

      b.修改wsdl文档的部分内容:将 <s:element ref="s:schema" /><s:any /> 替换成 <s:any minOccurs="2" maxOccurs="2"/>
      

      继续生成客户端代码,如下:
      ----------------------》

  4、编写客户端调用代码,如下:

 1 package com.test.ws.client;
 2 
 3 
 4 import cn.com.webxml.ArrayOfString;
 5 import cn.com.webxml.WeatherWS;
 6 import cn.com.webxml.WeatherWSSoap;
 7 
 8 /**
 9  * 调用WebService的客户端
10  * @author H__D
11  * @date 2017年8月3日 上午10:57:00
12  *
13  */
14 public class WSClient {
15 
16     public static void main(String[] args) {
17         //创建一个用于产生WeatherWS实例的工厂,WeatherWS类是wsimport工具生成的
18         WeatherWS factory = new WeatherWS();
19         //通过工厂生成一个WeatherWSSoap实例,WeatherWSSoap是wsimport工具生成的
20         WeatherWSSoap weatherWSSoap = factory.getWeatherWSSoap();
21         System.out.println(weatherWSSoap.getClass());
22         
23         //调用WeatherWSSoap的getWeather方法,获取天气信息
24         ArrayOfString weather = weatherWSSoap.getWeather("深圳", null);
25         for (String string : weather.getString()) {
26             System.out.println(string);
27             System.out.println("----------------");
28         }
29         
30     }
31 
32 }

 

  5、控制台输出如下:
    

调用免费的WebService获取手机号码归属地信息

  1、在网络上查询免费的手机号码归属地webservice的地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ,浏览器访问结果如下:
    

  2、wsdl文件地址:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl ,如下:
     

  3、新建一个java工程,使用wsimport工具自动生成客户端代码:
    a、新建一个java工程:
      
    b、使用wsimport生成客户端代码:

      ----------------》

  4、编写客户端调用代码,如下:

 1 package com.test.ws.client;
 2 
 3 import cn.com.webxml.MobileCodeWS;
 4 import cn.com.webxml.MobileCodeWSSoap;
 5 
 6 /**
 7  * 调用WebService的客户端
 8  * @author H__D
 9  * @date 2017年8月3日 上午11:13:39
10  *
11  */
12 public class WSClient {
13 
14     public static void main(String[] args) {
15         //创建一个用于产生MobileCodeWS实例的工厂,MobileCodeWS类是wsimport工具生成的
16         MobileCodeWS factory = new MobileCodeWS();
17         //通过工厂生成一个MobileCodeWSSoap实例,MobileCodeWSSoap是wsimport工具生成的
18         MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap();
19         System.out.println(mobileCodeWSSoap.getClass());
20         
21         //调用MobileCodeWSSoap的getMobileCodeInfo方法,获取天气信息
22         String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo("151****1111", null);
23         System.out.println(mobileCodeInfo);
24         
25     }
26 }

 

  5、运行调用代码,控制台输出如下:
     


    
      


    

posted on 2017-08-03 10:11  H__D  阅读(1038)  评论(0编辑  收藏  举报