delphi REST客户端程序  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi REST客户端程序


RESTClient1: TRESTClient;:是REST客户端管理类。

RESTRequest1: TRESTRequest;:负责REST请求相关的工作,处理请求用的参数等。

 RESTResponse1: TRESTResponse:负责REST请求返回的结果,HTTP状态码和返回结果等。

这个小程序的整个单元的源代码在下面:

 

unit Unit2;

 

interface

 

uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,

  System.Classes, Vcl.Graphics,

  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IPPeerClient, Vcl.StdCtrls, REST.Client,

  Data.Bind.Components, Data.Bind.ObjectScope;

 

type

  TForm2 = class(TForm)

    RESTClient1: TRESTClient;

    RESTRequest1: TRESTRequest;

    RESTResponse1: TRESTResponse;

    Button1: TButton;

    Memo1: TMemo;

    Edit1: TEdit;

    Label1: TLabel;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

 

var

  Form2: TForm2;

 

implementation

 

uses System.json;

 

{$R *.dfm}

 

procedure TForm2.Button1Click(Sender: TObject);

var

  JO, JData: TJSONObject;

  code: string;

  temp: string;

begin

 

  RESTClient1.BaseURL := 'http://ip.taobao.com//service/getIpInfo.php?ip=' +

    trim(Edit1.Text);

  RESTRequest1.Execute;

 

  // 清理先前数据

  Memo1.Clear;

 

  Memo1.Lines.Add('请求时间:' + Formatdatetime('yyyy-mm-dd hh:mm:ss zzz', now)

    + #13#10);

 

  // 在memo中显示得到数据

  temp := temp + '原始数据:' + #13#10;

  temp := temp + RESTResponse1.Content + #13#10;

  Memo1.Lines.Add(temp);

 

  // 解析得到的JSON数据

  JO := TJSONObject.ParseJSONValue(RESTResponse1.Content) as TJSONObject;

 

  // 得到错误编号

  code := (JO.Get('code').JsonValue as TJSONString).ToString;

 

  if code = '0' then

  begin

 

    // 解析具体数据

    JData := JO.Get('data').JsonValue as TJSONObject;

 

    temp := '解析的详细数据' + #13#10;

 

    // ISP

    temp := temp + ' I S P:' + (JData.Get('isp').JsonValue as TJSONString)

      .ToString + #13;

    Memo1.Lines.Add(temp);

 

    // 国家

    temp := '国家:' + (JData.Get('country').JsonValue as TJSONString)

      .ToString + #13;

    Memo1.Lines.Add(temp);

 

    // 地区

    temp := '地区:' + (JData.Get('area').JsonValue as TJSONString).ToString + #13;

    Memo1.Lines.Add(temp);

 

    // 省份

    temp := '省份:' + (JData.Get('region').JsonValue as TJSONString)

      .ToString + #13;

    Memo1.Lines.Add(temp);

 

    // 城市

    temp := '城市:' + (JData.Get('city').JsonValue as TJSONString)

      .ToString + #13#10;

    Memo1.Lines.Add(temp);

 

  end;

 

end;

 

end.


来源:https://blog.csdn.net/sunylat/article/details/46399987

 


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.047709941864014 seconds