天天看點

Delphi 2010 調用WebService接口

Delphi 調用WebService接口

此測試程式使用Delphi2010編寫

打開Delphi2010

一、File-> New-> Application 建立一個工程

在窗體上放置三個 Button按鈕,一個ComboBox,兩個Edit,  一個Memo和一個HTTPRIO

Delphi 2010 調用WebService接口

二、到File-> New-> Other-> WebServices-> WSDL importer 

Delphi 2010 調用WebService接口

在打開的WDSL對話框,在Location of WSDL File or URL:

填寫WSDL位址http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

Next>> Next>> Next>>Finish

之後系統會自動根據WSDL位址生成可以調用的單元檔案WeatherWS.pas ,如下圖

Delphi 2010 調用WebService接口

三、在HTTPRIO控件,設定URL屬性http://ws.webxml.com.cn/WebServices/WeatherWS.asmx

四、在主窗體引用生成的WeatherWS.pas

Delphi 2010 調用WebService接口

五、編寫按鈕事件

第一個,擷取省份,調用函數不用參數

Delphi 2010 調用WebService接口

procedure TFormMain.tbrCheLiangItems0Click(Sender: TObject);

var

  pras : ArrayOfString;

  i : Integer;

begin

  pras := (htpr as WeatherWSSoap).getRegionProvince;

  for i:= Low(pras) to High(pras) do

  begin

    cbb.Items.Add(pras[i]);

  end;

end;

第二個,擷取目前省份裡的城市名稱,調用函數需要一個參數:省份名稱,如北京 或 河北

Delphi 2010 調用WebService接口

procedure TFormMain.tbrCheLiangItems1Click(Sender: TObject);

var

  cityweather : ArrayOfString;

  t : string;

   i: Integer;

begin

  cityweather := (htpr as WeatherWSSoap).getSupportCityString(edt.Text);

  for i := Low(cityweather) to High(cityweather) do

  begin

    t := cityweather[i];

    mmo.Lines.Add(t);

  end;

end;

第三個,擷取天氣,調用函數需要兩個參數:城市代碼 和 使用者ID(需要注冊擷取)

Delphi 2010 調用WebService接口

使用者ID在ws.webxml.com.cn網站,注冊登入後在會員專區裡可以檢視,并且申請試用此接口的權限,5天測試時間。

Delphi 2010 調用WebService接口

procedure TFormMain.tbrCheLiangItems2Click(Sender: TObject);

var

  cityweather : ArrayOfString;

  t : string;

   i: Integer;

begin

  cityweather := (htpr as WeatherWSSoap).getWeather(edtCity.Text,'fb5d2d13926c40c28cfb07cdba220589');

  for i := Low(cityweather) to High(cityweather) do

  begin

    t := cityweather[i];

    mmo.Lines.Add(t);

  end;

end;

六、編譯,執行!

此示例是根據https://blog.csdn.net/heihei122/article/details/7975574所制作。

因www.webxml.com.cn的服務主機位址變更,原示例直接操作下來不能正常編譯和執行,故寫此流程。

自己做個記錄,友善以後檢視。

如需源碼,請發站内信,留下你的郵件位址。

寫得不好請見諒。

繼續閱讀