webservice编写天气预报功能

来源:互联网 发布:柴鸡蛋网络剧作品 编辑:程序博客网 时间:2024/05/22 00:29

我看过比较多的楼主用webservice编写天气预报,感觉都不符合我所想要的要求,so,我决定自己写个比较全的列子以供大家参考:tomcat+jsp+servlet+webservice+cxf 动态访问天气预报!


首先需要天气预报提供的wsdl开发文档,并导入在自己的项目里,在这里我就遇见了很多问题,等下我慢慢讲解,解决方案!


地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl (天气预报的wsdl地址)

地址:http://www.voidcn.com/blog/qq_20545159/article/p-3385073.html(常见的接口:如股票)

然后操作就是导入在你的项目里 用的是cxf

在黑窗口里,就是计算机的cmd

进入cmd里时,cd 到你的项目路径下 如图所示:AY是我的项目名




然后就是导入啦 使用wsdl2java命令 进行导入:其中-p命令是用于创建自己想要命名的包名,不用的话,系统自己默认包名,如图所示,这里我用了-p命令(这里会报错)


这是遇见的第一个问题,怎么解决的呢?这里说的是wsdl文档里的<s:element ref="s:schema"/>不能被编译,怎么弄呢,我必须要修改<s:element ref="s:schema"/>这个属性的嘛。所以这样的方式行不通,不能通过连接wsdl2java -p com.azj.service http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl来做,必须通过wsdl2java -p com.azj.service xxx.wsdl来使用!

首先把天气预报的wsdl文档里的内容导入在你自己在项目命名的xxx.wsdl里,然后在xxx.wsdl找到出错的位置<s:element ref="s:schema"/>; 这里Ctrl+F 输入<s:element ref="s:schema"/>,找到如图所示:



把<s:element ref="s:schema"/>
   <s:any/>

改成替换成<s:any minOccurs="2" maxOccurs="2"/>记得内容里的都要替换掉,我记得有两处


然后回到cmd里用输入wsdl2java  -p com.azj.service 然后把你的xxx.wsdl直接拉进来就是;(记得有空格啊)

如果你的wsdl文档还报错,别怕,我把修改好了的wsdl文档给你贴出来,替代你的xxx.wsdl里的内容就是:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:tns="http://WebXml.com.cn/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://WebXml.com.cn/">
  <wsdl:documentation>&lt;a href="http://www.webxml.com.cn/" target="_blank"&gt;WebXml.com.cn&lt;/a&gt; &lt;strong&gt;天气预报 Web 服务,数据每2.5小时左右自动更新一次,准确可靠。包括 340 多个中国主要城市和 60 多个国外主要城市三日内的天气预报数据。&lt;/br&gt;此天气预报Web Services请不要用于任何商业目的,若有需要请&lt;a href="http://www.webxml.com.cn/zh_cn/contact_us.aspx" target="_blank"&gt;联系我们&lt;/a&gt;,欢迎技术交流。 QQ:8409035&lt;br /&gt;使用本站 WEB 服务请注明或链接本站:http://www.webxml.com.cn/ 感谢大家的支持&lt;/strong&gt;!&lt;br /&gt;&lt;span style="color:#999999;"&gt;通知:天气预报 WEB 服务如原来使用地址 http://www.onhap.com/WebServices/WeatherWebService.asmx 的,请改成现在使用的服务地址 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ,重新引用即可。&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;</wsdl:documentation>
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://WebXml.com.cn/">
      <s:element name="getSupportCity">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="byProvinceName" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="getSupportCityResponse">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="getSupportCityResult" type="tns:ArrayOfString"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ArrayOfString">
        <s:sequence>
          <s:element maxOccurs="unbounded" minOccurs="0" name="string" nillable="true" type="s:string"/>
        </s:sequence>
      </s:complexType>
      <s:element name="getSupportProvince">
        <s:complexType/>
      </s:element>
      <s:element name="getSupportProvinceResponse">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="getSupportProvinceResult" type="tns:ArrayOfString"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="getSupportDataSet">
        <s:complexType/>
      </s:element>
      <s:element name="getSupportDataSetResponse">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="getSupportDataSetResult">
              <s:complexType>
                <s:sequence>
                  <s:any minOccurs="2" maxOccurs="2"/>
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="getWeatherbyCityName">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="theCityName" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="getWeatherbyCityNameResponse">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="getWeatherbyCityNameResult" type="tns:ArrayOfString"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="getWeatherbyCityNamePro">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="theCityName" type="s:string"/>
            <s:element maxOccurs="1" minOccurs="0" name="theUserID" type="s:string"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="getWeatherbyCityNameProResponse">
        <s:complexType>
          <s:sequence>
            <s:element maxOccurs="1" minOccurs="0" name="getWeatherbyCityNameProResult" type="tns:ArrayOfString"/>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="ArrayOfString" nillable="true" type="tns:ArrayOfString"/>
      <s:element name="DataSet" nillable="true">
        <s:complexType>
          <s:sequence>
             <s:any minOccurs="2" maxOccurs="2"/>
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="getSupportCitySoapIn">
    <wsdl:part element="tns:getSupportCity" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getSupportCitySoapOut">
    <wsdl:part element="tns:getSupportCityResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getSupportProvinceSoapIn">
    <wsdl:part element="tns:getSupportProvince" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getSupportProvinceSoapOut">
    <wsdl:part element="tns:getSupportProvinceResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getSupportDataSetSoapIn">
    <wsdl:part element="tns:getSupportDataSet" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getSupportDataSetSoapOut">
    <wsdl:part element="tns:getSupportDataSetResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameSoapIn">
    <wsdl:part element="tns:getWeatherbyCityName" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameSoapOut">
    <wsdl:part element="tns:getWeatherbyCityNameResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameProSoapIn">
    <wsdl:part element="tns:getWeatherbyCityNamePro" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameProSoapOut">
    <wsdl:part element="tns:getWeatherbyCityNameProResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="getSupportCityHttpGetIn">
    <wsdl:part name="byProvinceName" type="s:string"/>
  </wsdl:message>
  <wsdl:message name="getSupportCityHttpGetOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getSupportProvinceHttpGetIn"/>
  <wsdl:message name="getSupportProvinceHttpGetOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getSupportDataSetHttpGetIn"/>
  <wsdl:message name="getSupportDataSetHttpGetOut">
    <wsdl:part element="tns:DataSet" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameHttpGetIn">
    <wsdl:part name="theCityName" type="s:string"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameHttpGetOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameProHttpGetIn">
    <wsdl:part name="theCityName" type="s:string"/>
    <wsdl:part name="theUserID" type="s:string"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameProHttpGetOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getSupportCityHttpPostIn">
    <wsdl:part name="byProvinceName" type="s:string"/>
  </wsdl:message>
  <wsdl:message name="getSupportCityHttpPostOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getSupportProvinceHttpPostIn"/>
  <wsdl:message name="getSupportProvinceHttpPostOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getSupportDataSetHttpPostIn"/>
  <wsdl:message name="getSupportDataSetHttpPostOut">
    <wsdl:part element="tns:DataSet" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameHttpPostIn">
    <wsdl:part name="theCityName" type="s:string"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameHttpPostOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameProHttpPostIn">
    <wsdl:part name="theCityName" type="s:string"/>
    <wsdl:part name="theUserID" type="s:string"/>
  </wsdl:message>
  <wsdl:message name="getWeatherbyCityNameProHttpPostOut">
    <wsdl:part element="tns:ArrayOfString" name="Body"/>
  </wsdl:message>
  <wsdl:portType name="WeatherWebServiceSoap">
    <wsdl:operation name="getSupportCity">
      <wsdl:documentation>&lt;br /&gt;&lt;h3&gt;查询本天气预报Web Services支持的国内外城市或地区信息&lt;/h3&gt;&lt;p&gt;输入参数:byProvinceName = 指定的洲或国内的省份,若为ALL或空则表示返回全部城市;返回数据:一个一维字符串数组 String(),结构为:城市名称(城市代码)。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportCitySoapIn"/>
      <wsdl:output message="tns:getSupportCitySoapOut"/>
    </wsdl:operation>
    <wsdl:operation name="getSupportProvince">
      <wsdl:documentation>&lt;br /&gt;&lt;h3&gt;获得本天气预报Web Services支持的洲、国内外省份和城市信息&lt;/h3&gt;&lt;p&gt;输入参数:无; 返回数据:一个一维字符串数组 String(),内容为洲或国内省份的名称。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportProvinceSoapIn"/>
      <wsdl:output message="tns:getSupportProvinceSoapOut"/>
    </wsdl:operation>
    <wsdl:operation name="getSupportDataSet">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;获得本天气预报Web Services支持的洲、国内外省份和城市信息&lt;/h3&gt;&lt;p&gt;输入参数:无;返回:DataSet 。DataSet.Tables(0) 为支持的洲和国内省份数据,DataSet.Tables(1) 为支持的国内外城市或地区数据。DataSet.Tables(0).Rows(i).Item("ID") 主键对应 DataSet.Tables(1).Rows(i).Item("ZoneID") 外键。&lt;br /&gt;Tables(0):ID = ID主键,Zone = 支持的洲、省份;Tables(1):ID 主键,ZoneID = 对应Tables(0)ID的外键,Area = 城市或地区,AreaCode = 城市或地区代码。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportDataSetSoapIn"/>
      <wsdl:output message="tns:getSupportDataSetSoapOut"/>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityName">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数&lt;/h3&gt;&lt;p&gt;调用方法如下:输入参数:theCityName = 城市中文名称(国外城市可用英文)或城市代码(不输入默认为上海市),如:上海 或 58367,如有城市名称重复请使用城市代码查询(可通过 getSupportCity 或 getSupportDataSet 获得);返回数据: 一个一维数组 String(22),共有23个元素。&lt;br /&gt;String(0) 到 String(4):省份,城市,城市代码,城市图片名称,最后更新时间。String(5) 到 String(11):当天的 气温,概况,风向和风力,天气趋势开始图片名称(以下称:图标一),天气趋势结束图片名称(以下称:图标二),现在的天气实况,天气和生活指数。String(12) 到 String(16):第二天的 气温,概况,风向和风力,图标一,图标二。String(17) 到 String(21):第三天的 气温,概况,风向和风力,图标一,图标二。String(22) 被查询的城市或地区的介绍 &lt;br /&gt;&lt;a href="http://www.webxml.com.cn/images/weather.zip"&gt;下载天气图标&lt;img src="http://www.webxml.com.cn/images/download_w.gif" border="0" align="absbottom" /&gt;&lt;/a&gt;(包含大、中、小尺寸) &lt;a href="http://www.webxml.com.cn/zh_cn/weather_icon.aspx" target="_blank"&gt;天气图例说明&lt;/a&gt; &lt;a href="http://www.webxml.com.cn/files/weather_eg.zip"&gt;调用此天气预报Web Services实例下载&lt;/a&gt; (VB ASP.net 2.0)&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getWeatherbyCityNameSoapIn"/>
      <wsdl:output message="tns:getWeatherbyCityNameSoapOut"/>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityNamePro">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数(For商业用户)&lt;/h3&gt;&lt;p&gt;调用方法同 getWeatherbyCityName,输入参数:theUserID = 商业用户ID&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getWeatherbyCityNameProSoapIn"/>
      <wsdl:output message="tns:getWeatherbyCityNameProSoapOut"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:portType name="WeatherWebServiceHttpGet">
    <wsdl:operation name="getSupportCity">
      <wsdl:documentation>&lt;br /&gt;&lt;h3&gt;查询本天气预报Web Services支持的国内外城市或地区信息&lt;/h3&gt;&lt;p&gt;输入参数:byProvinceName = 指定的洲或国内的省份,若为ALL或空则表示返回全部城市;返回数据:一个一维字符串数组 String(),结构为:城市名称(城市代码)。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportCityHttpGetIn"/>
      <wsdl:output message="tns:getSupportCityHttpGetOut"/>
    </wsdl:operation>
    <wsdl:operation name="getSupportProvince">
      <wsdl:documentation>&lt;br /&gt;&lt;h3&gt;获得本天气预报Web Services支持的洲、国内外省份和城市信息&lt;/h3&gt;&lt;p&gt;输入参数:无; 返回数据:一个一维字符串数组 String(),内容为洲或国内省份的名称。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportProvinceHttpGetIn"/>
      <wsdl:output message="tns:getSupportProvinceHttpGetOut"/>
    </wsdl:operation>
    <wsdl:operation name="getSupportDataSet">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;获得本天气预报Web Services支持的洲、国内外省份和城市信息&lt;/h3&gt;&lt;p&gt;输入参数:无;返回:DataSet 。DataSet.Tables(0) 为支持的洲和国内省份数据,DataSet.Tables(1) 为支持的国内外城市或地区数据。DataSet.Tables(0).Rows(i).Item("ID") 主键对应 DataSet.Tables(1).Rows(i).Item("ZoneID") 外键。&lt;br /&gt;Tables(0):ID = ID主键,Zone = 支持的洲、省份;Tables(1):ID 主键,ZoneID = 对应Tables(0)ID的外键,Area = 城市或地区,AreaCode = 城市或地区代码。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportDataSetHttpGetIn"/>
      <wsdl:output message="tns:getSupportDataSetHttpGetOut"/>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityName">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数&lt;/h3&gt;&lt;p&gt;调用方法如下:输入参数:theCityName = 城市中文名称(国外城市可用英文)或城市代码(不输入默认为上海市),如:上海 或 58367,如有城市名称重复请使用城市代码查询(可通过 getSupportCity 或 getSupportDataSet 获得);返回数据: 一个一维数组 String(22),共有23个元素。&lt;br /&gt;String(0) 到 String(4):省份,城市,城市代码,城市图片名称,最后更新时间。String(5) 到 String(11):当天的 气温,概况,风向和风力,天气趋势开始图片名称(以下称:图标一),天气趋势结束图片名称(以下称:图标二),现在的天气实况,天气和生活指数。String(12) 到 String(16):第二天的 气温,概况,风向和风力,图标一,图标二。String(17) 到 String(21):第三天的 气温,概况,风向和风力,图标一,图标二。String(22) 被查询的城市或地区的介绍 &lt;br /&gt;&lt;a href="http://www.webxml.com.cn/images/weather.zip"&gt;下载天气图标&lt;img src="http://www.webxml.com.cn/images/download_w.gif" border="0" align="absbottom" /&gt;&lt;/a&gt;(包含大、中、小尺寸) &lt;a href="http://www.webxml.com.cn/zh_cn/weather_icon.aspx" target="_blank"&gt;天气图例说明&lt;/a&gt; &lt;a href="http://www.webxml.com.cn/files/weather_eg.zip"&gt;调用此天气预报Web Services实例下载&lt;/a&gt; (VB ASP.net 2.0)&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getWeatherbyCityNameHttpGetIn"/>
      <wsdl:output message="tns:getWeatherbyCityNameHttpGetOut"/>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityNamePro">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数(For商业用户)&lt;/h3&gt;&lt;p&gt;调用方法同 getWeatherbyCityName,输入参数:theUserID = 商业用户ID&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getWeatherbyCityNameProHttpGetIn"/>
      <wsdl:output message="tns:getWeatherbyCityNameProHttpGetOut"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:portType name="WeatherWebServiceHttpPost">
    <wsdl:operation name="getSupportCity">
      <wsdl:documentation>&lt;br /&gt;&lt;h3&gt;查询本天气预报Web Services支持的国内外城市或地区信息&lt;/h3&gt;&lt;p&gt;输入参数:byProvinceName = 指定的洲或国内的省份,若为ALL或空则表示返回全部城市;返回数据:一个一维字符串数组 String(),结构为:城市名称(城市代码)。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportCityHttpPostIn"/>
      <wsdl:output message="tns:getSupportCityHttpPostOut"/>
    </wsdl:operation>
    <wsdl:operation name="getSupportProvince">
      <wsdl:documentation>&lt;br /&gt;&lt;h3&gt;获得本天气预报Web Services支持的洲、国内外省份和城市信息&lt;/h3&gt;&lt;p&gt;输入参数:无; 返回数据:一个一维字符串数组 String(),内容为洲或国内省份的名称。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportProvinceHttpPostIn"/>
      <wsdl:output message="tns:getSupportProvinceHttpPostOut"/>
    </wsdl:operation>
    <wsdl:operation name="getSupportDataSet">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;获得本天气预报Web Services支持的洲、国内外省份和城市信息&lt;/h3&gt;&lt;p&gt;输入参数:无;返回:DataSet 。DataSet.Tables(0) 为支持的洲和国内省份数据,DataSet.Tables(1) 为支持的国内外城市或地区数据。DataSet.Tables(0).Rows(i).Item("ID") 主键对应 DataSet.Tables(1).Rows(i).Item("ZoneID") 外键。&lt;br /&gt;Tables(0):ID = ID主键,Zone = 支持的洲、省份;Tables(1):ID 主键,ZoneID = 对应Tables(0)ID的外键,Area = 城市或地区,AreaCode = 城市或地区代码。&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getSupportDataSetHttpPostIn"/>
      <wsdl:output message="tns:getSupportDataSetHttpPostOut"/>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityName">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数&lt;/h3&gt;&lt;p&gt;调用方法如下:输入参数:theCityName = 城市中文名称(国外城市可用英文)或城市代码(不输入默认为上海市),如:上海 或 58367,如有城市名称重复请使用城市代码查询(可通过 getSupportCity 或 getSupportDataSet 获得);返回数据: 一个一维数组 String(22),共有23个元素。&lt;br /&gt;String(0) 到 String(4):省份,城市,城市代码,城市图片名称,最后更新时间。String(5) 到 String(11):当天的 气温,概况,风向和风力,天气趋势开始图片名称(以下称:图标一),天气趋势结束图片名称(以下称:图标二),现在的天气实况,天气和生活指数。String(12) 到 String(16):第二天的 气温,概况,风向和风力,图标一,图标二。String(17) 到 String(21):第三天的 气温,概况,风向和风力,图标一,图标二。String(22) 被查询的城市或地区的介绍 &lt;br /&gt;&lt;a href="http://www.webxml.com.cn/images/weather.zip"&gt;下载天气图标&lt;img src="http://www.webxml.com.cn/images/download_w.gif" border="0" align="absbottom" /&gt;&lt;/a&gt;(包含大、中、小尺寸) &lt;a href="http://www.webxml.com.cn/zh_cn/weather_icon.aspx" target="_blank"&gt;天气图例说明&lt;/a&gt; &lt;a href="http://www.webxml.com.cn/files/weather_eg.zip"&gt;调用此天气预报Web Services实例下载&lt;/a&gt; (VB ASP.net 2.0)&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getWeatherbyCityNameHttpPostIn"/>
      <wsdl:output message="tns:getWeatherbyCityNameHttpPostOut"/>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityNamePro">
      <wsdl:documentation>&lt;br&gt;&lt;h3&gt;根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数(For商业用户)&lt;/h3&gt;&lt;p&gt;调用方法同 getWeatherbyCityName,输入参数:theUserID = 商业用户ID&lt;/p&gt;&lt;br /&gt;</wsdl:documentation>
      <wsdl:input message="tns:getWeatherbyCityNameProHttpPostIn"/>
      <wsdl:output message="tns:getWeatherbyCityNameProHttpPostOut"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="WeatherWebServiceSoap" type="tns:WeatherWebServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getSupportCity">
      <soap:operation soapAction="http://WebXml.com.cn/getSupportCity" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportProvince">
      <soap:operation soapAction="http://WebXml.com.cn/getSupportProvince" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportDataSet">
      <soap:operation soapAction="http://WebXml.com.cn/getSupportDataSet" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityName">
      <soap:operation soapAction="http://WebXml.com.cn/getWeatherbyCityName" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityNamePro">
      <soap:operation soapAction="http://WebXml.com.cn/getWeatherbyCityNamePro" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="WeatherWebServiceSoap12" type="tns:WeatherWebServiceSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getSupportCity">
      <soap12:operation soapAction="http://WebXml.com.cn/getSupportCity" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportProvince">
      <soap12:operation soapAction="http://WebXml.com.cn/getSupportProvince" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportDataSet">
      <soap12:operation soapAction="http://WebXml.com.cn/getSupportDataSet" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityName">
      <soap12:operation soapAction="http://WebXml.com.cn/getWeatherbyCityName" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityNamePro">
      <soap12:operation soapAction="http://WebXml.com.cn/getWeatherbyCityNamePro" style="document"/>
      <wsdl:input>
        <soap12:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="WeatherWebServiceHttpGet" type="tns:WeatherWebServiceHttpGet">
    <http:binding verb="GET"/>
    <wsdl:operation name="getSupportCity">
      <http:operation location="/getSupportCity"/>
      <wsdl:input>
        <http:urlEncoded/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportProvince">
      <http:operation location="/getSupportProvince"/>
      <wsdl:input>
        <http:urlEncoded/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportDataSet">
      <http:operation location="/getSupportDataSet"/>
      <wsdl:input>
        <http:urlEncoded/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityName">
      <http:operation location="/getWeatherbyCityName"/>
      <wsdl:input>
        <http:urlEncoded/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityNamePro">
      <http:operation location="/getWeatherbyCityNamePro"/>
      <wsdl:input>
        <http:urlEncoded/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="WeatherWebServiceHttpPost" type="tns:WeatherWebServiceHttpPost">
    <http:binding verb="POST"/>
    <wsdl:operation name="getSupportCity">
      <http:operation location="/getSupportCity"/>
      <wsdl:input>
        <mime:content type="application/x-www-form-urlencoded"/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportProvince">
      <http:operation location="/getSupportProvince"/>
      <wsdl:input>
        <mime:content type="application/x-www-form-urlencoded"/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getSupportDataSet">
      <http:operation location="/getSupportDataSet"/>
      <wsdl:input>
        <mime:content type="application/x-www-form-urlencoded"/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityName">
      <http:operation location="/getWeatherbyCityName"/>
      <wsdl:input>
        <mime:content type="application/x-www-form-urlencoded"/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="getWeatherbyCityNamePro">
      <http:operation location="/getWeatherbyCityNamePro"/>
      <wsdl:input>
        <mime:content type="application/x-www-form-urlencoded"/>
      </wsdl:input>
      <wsdl:output>
        <mime:mimeXml part="Body"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="WeatherWebService">
    <wsdl:documentation>&lt;a href="http://www.webxml.com.cn/" target="_blank"&gt;WebXml.com.cn&lt;/a&gt; &lt;strong&gt;天气预报 Web 服务,数据每2.5小时左右自动更新一次,准确可靠。包括 340 多个中国主要城市和 60 多个国外主要城市三日内的天气预报数据。&lt;/br&gt;此天气预报Web Services请不要用于任何商业目的,若有需要请&lt;a href="http://www.webxml.com.cn/zh_cn/contact_us.aspx" target="_blank"&gt;联系我们&lt;/a&gt;,欢迎技术交流。 QQ:8409035&lt;br /&gt;使用本站 WEB 服务请注明或链接本站:http://www.webxml.com.cn/ 感谢大家的支持&lt;/strong&gt;!&lt;br /&gt;&lt;span style="color:#999999;"&gt;通知:天气预报 WEB 服务如原来使用地址 http://www.onhap.com/WebServices/WeatherWebService.asmx 的,请改成现在使用的服务地址 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ,重新引用即可。&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;</wsdl:documentation>
    <wsdl:port binding="tns:WeatherWebServiceSoap" name="WeatherWebServiceSoap">
      <soap:address location="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"/>
    </wsdl:port>
    <wsdl:port binding="tns:WeatherWebServiceSoap12" name="WeatherWebServiceSoap12">
      <soap12:address location="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"/>
    </wsdl:port>
    <wsdl:port binding="tns:WeatherWebServiceHttpGet" name="WeatherWebServiceHttpGet">
      <http:address location="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"/>
    </wsdl:port>
    <wsdl:port binding="tns:WeatherWebServiceHttpPost" name="WeatherWebServiceHttpPost">
      <http:address location="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

如果还不行,末尾我把demo提供你下载就是!

然后是jsp内容:要有jquery-3.0.0.min.js的jar包啊

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>艾壹系统-AYXT</title>
        <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/pu.ico"/>
        <link type="text/css" rel="stylesheet" media="all" href="<%=request.getContextPath()%>/styles/global.css" />
        <link type="text/css" rel="stylesheet" media="all" href="<%=request.getContextPath()%>/styles/global_color.css" />
        <script src="/AY/js/jquery-3.0.0.min.js" type="text/javascript"></script>  
<script>
$(function(){
         $.post("weatherservlet",{"city":'北京'},function(data){//weatherservlet这是servlet
         data=eval(data);
         $("#city").text(data[0].city);
         $("#we").text(data[0].wh);
         $("#zt").text(data[0].zt);
         $("#f").text(data[0].f);
         $("#kq").text(data[0].kq);
         $("#img").attr("src","images/"+data[0].img);
          });
});
</script>
    </head>
    <body class="index">
        <div style="width:50px;margin-left:150px;position: relative;top: 200px;height: 200px;">
         <p id="city"></p><br/><br/>
         <p  style="color: green"><a id="we"></a>&nbsp;&nbsp;<a id="zt"></a><img id="img" style="margin-left: 80px;position: relative;top: -15px;" /></p><br/><br/>
         <p id="f"></p><br/><br/>
         <p id="kq" style=";width:200px;height:100px; word-wrap: break-word;"></p>
        </div>
    </body>
</html>


然后是servlet的编写

package com.azj.utils;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonUtil;
import cn.com.webxml.ArrayOfString;
import cn.com.webxml.WeatherWebService;
import cn.com.webxml.WeatherWebServiceSoap;
@WebServlet("/weatherservlet")
public class weatherservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
    public weatherservlet() {
        super();
        
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String city=request.getParameter("city");
JSONArray ja=new JSONArray();
if(null==session.getAttribute(city)){
WeatherWebService ww=new WeatherWebService();//调用你wsdl导出的类
WeatherWebServiceSoap ws = ww.getWeatherWebServiceSoap();

ArrayOfString as = ws.getWeatherbyCityName(city);
List<String> ss = as.getString();

                      /*System.out.println(city);
for (int i = 0; i < ss.size(); i++) {
System.out.println(ss.get(i));
}打印效果,要看,自己揭开看*/


                JSONObject jo=new JSONObject();
jo.put("city", ss.get(1));//城市
jo.put("wh", ss.get(5));//天气
jo.put("zt", ss.get(6).substring(ss.get(6).length()-1,ss.get(6).length()));
jo.put("f", ss.get(7));
jo.put("img", ss.get(8));//图片
jo.put("img1", ss.get(9));
jo.put("kq", ss.get(10));//空气质量
ja.add(jo);
session.setAttribute(city, ja);
}else{
ja = (JSONArray) session.getAttribute(city);
}

response.setCharacterEncoding("utf-8");
response.getWriter().println(ja);

}

}


这里面,里如果吧cxf包导入进去啦,会报错的,这里不要cxf的jar包也可以,如要导jar包,注意cxf-rt-frontend-jaxws-3.1.6.jar,有冲突,删掉就可以

这里还必须导入json包,上面JSONArray 就是用的jar数据的

然后启动tomcat,访问就可以啦!


我把demo的下载地址贴出来供下载:http://download.csdn.net/detail/rainjm/9755475

里面有struts2+hibernate4.1+spring4.0+cxf+jsp的全部必须jar包,比较懒,so............

0 0