webservice調用天氣服務

常見服務:http://www.webxml.com.cn/zh_cn/support.aspxjava

可能用到缺乏的ArrayOfString.java文件web

package com.test.wes.weather;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>ArrayOfString complex type的 Java 類。
 * 
 * <p>如下模式片斷指定包含在此類中的預期內容。
 * 
 * <pre>
 * &lt;complexType name="ArrayOfString">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="string" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfString", propOrder = {
    "string"
})
public class ArrayOfString {

    @XmlElement(nillable = true)
    protected List<String> string;

    /**
     * Gets the value of the string property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the string property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getString().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link String }
     * 
     * 
     */
    public List<String> getString() {
        if (string == null) {
            string = new ArrayList<String>();
        }
        return this.string;
    }

}
View Code

wsimport用法服務器

用法: wsimport [options] <WSDL_URI>

\其中 [options] 包括:
  -b <path>                 指定 jaxws/jaxb 綁定文件或附加模式
                            (每一個 <path> 都必須具備本身的 -b)
  -B<jaxbOption>            將此選項傳遞給 JAXB 模式編譯器
  -catalog <file>           指定用於解析外部實體引用的目錄文件
                            支持 TR9401, XCatalog 和 OASIS XML 目錄格式。
  -d <directory>            指定放置生成的輸出文件的位置
  -encoding <encoding>      指定源文件所使用的字符編碼
  -extension                容許供應商擴展 - 不按規範
                            指定功能。使用擴展可能會
                            致使應用程序不可移植或
                            沒法與其餘實現進行互操做
  -help                     顯示幫助
  -httpproxy:<host>:<port>  指定 HTTP 代理服務器 (端口默認爲 8080)
  -keep                     保留生成的文件
  -p <pkg>                  指定目標程序包
  -quiet                    隱藏 wsimport 輸出
  -s <directory>            指定放置生成的源文件的位置
  -target <version>         按給定的 JAXWS 規範版本生成代碼
                            默認爲 2.2, 接受的值爲 2.0, 2.1 和 2.2
                            例如, 2.0 將爲 JAXWS 2.0 規範生成兼容的代碼
  -verbose                  有關編譯器在執行什麼操做的輸出消息
  -version                  輸出版本信息
  -wsdllocation <location>  @WebServiceClient.wsdlLocation 值
  -clientjar <jarfile>      建立生成的 Artifact 的 jar 文件以及
                            調用 Web 服務所需的 WSDL 元數據。
  -generateJWS              生成存根 JWS 實現文件
  -implDestDir <directory>  指定生成 JWS 實現文件的位置
  -implServiceName <name>   生成的 JWS 實現的服務名的本地部分
  -implPortName <name>      生成的 JWS 實現的端口名的本地部分

\擴展:
  -XadditionalHeaders              映射標頭不綁定到請求或響應消息不綁定到
                                   Java 方法參數
  -Xauthfile                       用於傳送如下格式的受權信息的文件:
                                   http://username:password@example.org/stock?wsdl
  -Xdebug                          輸出調試信息
  -Xno-addressing-databinding      容許 W3C EndpointReferenceType 到 Java 的綁定
  -Xnocompile                      不編譯生成的 Java 文件
  -XdisableAuthenticator           禁用由 JAX-WS RI 使用的驗證程序,
                                   將忽略 -Xauthfile 選項 (若是設置)
  -XdisableSSLHostnameVerification 在提取 wsdl 時禁用 SSL 主機名
                                   驗證

\示例:
  wsimport stock.wsdl -b stock.xml -b stock.xjb
  wsimport -d generated http://example.org/stock?wsdl

 


 

平時的用法ide

wsimport -encoding utf-8 -p com.test.wes.weather -s . http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

ui

先把文件下載下來WeatherWS.asmxthis

 再WeatherWS.asmx裏編碼

 <s:element ref="s:schema"/><s:any/>所有替換爲​ <s:any minOccurs="2" maxOccurs="2"/>url


 

wsimport -encoding utf-8 -p com.test.wes.weather -s . WeatherWS.asmx

 

運行上面的句子,生成了下面代碼spa

 

 

 


package com.test.wec;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.test.wes.phone.MobileCodeWSSoap;
import com.test.wes.weather.ArrayOfString;
import com.test.wes.weather.WeatherWS;
import com.test.wes.weather.WeatherWSSoap;
 
 

public class ServiceClient {
 
    public static void main(String[] args) throws IOException {
        //建立WSDL的URL,注意不是服務地址
//        URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");
//        
//        //建立服務名稱
//        //1.namespaceURI - 命名空間地址
//        //2.localPart - 服務視圖名
//        QName qname = new QName("http://WebXml.com.cn/", "qqOnlineWebService");
//        
//        //建立服務視圖
//        //參數解釋:
//        //1.wsdlDocumentLocation - wsdl地址
//        //2.serviceName - 服務名稱
//        Service service = Service.create(url, qname);
//        //獲取服務實現類
////        QqOnlineWebServiceSoap qqonlineSsSoap =service.getPort(QqOnlineWebServiceSoap.class);
////        //調用查詢方法
////        String result = qqonlineSsSoap.qqCheckOnline("1565465");
////        System.out.println(result);
        
        WeatherWS factory = new WeatherWS();
        //根據工廠建立一個WeatherWSSoap對象
        WeatherWSSoap weatherWSSoap = factory.getWeatherWSSoap();
        //調用WebService提供的getWeather方法獲取南寧市的天氣預報狀況
        ArrayOfString weatherInfo = weatherWSSoap.getSupportCityString("廣東");
        List<String> lstWeatherInfo = weatherInfo.getString();
        //遍歷天氣預報信息
        for (String s : lstWeatherInfo) {
            System.out.print(s+"|");
        }
        System.out.println();
        
        ArrayOfString weatherInfo1 = weatherWSSoap.getWeather("廣州", "");
        List<String> lstWeatherInfo1 = weatherInfo1.getString();
        //遍歷天氣預報信息
        for (String s : lstWeatherInfo1) {
            System.out.print(s+"|");
        }
        System.out.println();
        
    }
}

相關文章
相關標籤/搜索