Delphi XE7 REST获取天气实例


本文整理自网络,侵删。

 
procedure TForm1.Button1Click(Sender: TObject);
begin
  RESTRequest1.ResetToDefaults;
  RESTClient1.ResetToDefaults;
  RESTResponse1.ResetToDefaults;

  RESTClient1.BaseURL := 'http://weather.livedoor.com/';
  RESTRequest1.Resource := 'forecast/webservice/json/v1?city={CITY}';
  RESTRequest1.Params.AddItem('CITY', Edit1.Text,
    TRESTRequestParameterKind.pkURLSEGMENT);
  RESTRequest1.Execute;
end;


procedure TForm1.RESTRequest1AfterExecute(Sender: TCustomRESTRequest);
const
  KEYS: array [0 .. 2] of string = ('dateLabel', 'telop', 'date');
var
  JSONValue: TJSONValue;
  Forecasts: TJSONArray;
  Forecast: TJSONValue;
  Key: string;
begin
  Memo1.Lines.Clear;;
  Label1.Text := 'URI: ' + Sender.GetFullRequestURL + ' Execution time: ' +
    IntToStr(Sender.ExecutionPerformance.TotalExecutionTime) + 'ms';
  if Assigned(RESTResponse1.JSONValue) then
  begin
    JSONValue := RESTResponse1.JSONValue;
    Forecasts := JSONValue.GetValue<TJSONArray>('forecasts');
    for Forecast in Forecasts do
    begin
      for Key in KEYS do
      begin
        Memo1.Lines.Add(Format('%s=%s',
          [Key, Forecast.GetValue<TJSONString>(Key).ToString]));
      end;
    end;
  end;
end;

procedure TForm1.RESTRequest1HTTPProtocolError(Sender: TCustomRESTRequest);
begin
  Memo1.Lines.Add(Sender.Response.StatusText);
  Memo1.Lines.Add(Sender.Response.Content);
end;

相关阅读 >>

Delphi 蓝牙连接打印机演示

Delphi 设置系统默认打印机

Delphi httpencode编码

Delphi xe5 手机应用开发经验技巧

Delphi 使控件变成圆角的方法

Delphi gethref 获取<a href="链接"></a>中href的数据

Delphi richedit 的scrollbar自动向下滚动

Delphi webbroker standalone 模式下当作一个普通的 webserver 响应静态网页或其它文件的请求

Delphi 线程教学第一节:初识多线程

Delphi datasnap传输流/文件问题

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...