REST的小例子

Delphi:XE8

看了好几天的资料了,也没有弄出来一个REST程序,尝试了XE8中带的例子,也都没有搞懂。我在网上不断搜索,看是否能够找到适合自己的文章,希望能够做出来一个REST的小例子,万幸,终于弄出来一个了!

这个小程序是把IP地址发送到淘宝的REST API,得到淘宝IP库返回的解析结果,先看看运行效果吧!


这是一个REST客户端程序,既然自己暂时编不出来REST服务器,那么最简单就是找一个可以尝试的REST服务器,所以我用这种方法先编一个RESET客户端程序,对REST程序了解一下!我们先看看我这个小程序使用了哪些控件:


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

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

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

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

[delphi]  view plain  copy
  1. unit Unit2;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,  
  7.   System.Classes, Vcl.Graphics,  
  8.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IPPeerClient, Vcl.StdCtrls, REST.Client,  
  9.   Data.Bind.Components, Data.Bind.ObjectScope;  
  10.   
  11. type  
  12.   TForm2 = class(TForm)  
  13.     RESTClient1: TRESTClient;  
  14.     RESTRequest1: TRESTRequest;  
  15.     RESTResponse1: TRESTResponse;  
  16.     Button1: TButton;  
  17.     Memo1: TMemo;  
  18.     Edit1: TEdit;  
  19.     Label1: TLabel;  
  20.     procedure Button1Click(Sender: TObject);  
  21.   private  
  22.     { Private declarations }  
  23.   public  
  24.     { Public declarations }  
  25.   end;  
  26.   
  27. var  
  28.   Form2: TForm2;  
  29.   
  30. implementation  
  31.   
  32. uses System.json;  
  33.   
  34. {$R *.dfm}  
  35.   
  36. procedure TForm2.Button1Click(Sender: TObject);  
  37. var  
  38.   JO, JData: TJSONObject;  
  39.   code: string;  
  40.   temp: string;  
  41. begin  
  42.   
  43.   RESTClient1.BaseURL := 'http://ip.taobao.com//service/getIpInfo.php?ip=' +  
  44.     trim(Edit1.Text);  
  45.   RESTRequest1.Execute;  
  46.   
  47.   // 清理先前数据  
  48.   Memo1.Clear;  
  49.   
  50.   Memo1.Lines.Add('请求时间:' + Formatdatetime('yyyy-mm-dd hh:mm:ss zzz', now)  
  51.     + #13#10);  
  52.   
  53.   // 在memo中显示得到数据  
  54.   temp := temp + '原始数据:' + #13#10;  
  55.   temp := temp + RESTResponse1.Content + #13#10;  
  56.   Memo1.Lines.Add(temp);  
  57.   
  58.   // 解析得到的JSON数据  
  59.   JO := TJSONObject.ParseJSONValue(RESTResponse1.Content) as TJSONObject;  
  60.   
  61.   // 得到错误编号  
  62.   code := (JO.Get('code').JsonValue as TJSONString).ToString;  
  63.   
  64.   if code = '0' then  
  65.   begin  
  66.   
  67.     // 解析具体数据  
  68.     JData := JO.Get('data').JsonValue as TJSONObject;  
  69.   
  70.     temp := '解析的详细数据' + #13#10;  
  71.   
  72.     // ISP  
  73.     temp := temp + ' I S P:' + (JData.Get('isp').JsonValue as TJSONString)  
  74.       .ToString + #13;  
  75.     Memo1.Lines.Add(temp);  
  76.   
  77.     // 国家  
  78.     temp := '国家:' + (JData.Get('country').JsonValue as TJSONString)  
  79.       .ToString + #13;  
  80.     Memo1.Lines.Add(temp);  
  81.   
  82.     // 地区  
  83.     temp := '地区:' + (JData.Get('area').JsonValue as TJSONString).ToString + #13;  
  84.     Memo1.Lines.Add(temp);  
  85.   
  86.     // 省份  
  87.     temp := '省份:' + (JData.Get('region').JsonValue as TJSONString)  
  88.       .ToString + #13;  
  89.     Memo1.Lines.Add(temp);  
  90.   
  91.     // 城市  
  92.     temp := '城市:' + (JData.Get('city').JsonValue as TJSONString)  
  93.       .ToString + #13#10;  
  94.     Memo1.Lines.Add(temp);  
  95.   
  96.   end;  
  97.   
  98. end;  
  99.   
  100. end.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值