zoukankan      html  css  js  c++  java
  • 我的第一个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 plaincopy
     
    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.  

    源代码下载:

    http://download.csdn.net/detail/sunylat/8781273

    参考资料:

    淘宝IP地址查询

    http://ip.taobao.com/instructions.php

    http://blog.csdn.net/maxwoods/article/details/24265667

    http://www.cnblogs.com/xalion/p/3370459.html

    http://blog.csdn.net/sunylat/article/details/41407945

    http://blog.csdn.net/sunylat/article/details/46399987

  • 相关阅读:
    jni java与c++交互返回三维数组jobjectArray
    Android开发之EditText 详解(addTextChangedListener监听用户输入状态)
    Android JNI和NDK学习(09)--JNI实例二 传递类对象
    JavaDoc的生成规则---ShinePans
    Android 电话自己主动接听和挂断具体解释
    传统线程技术(一)
    Linux内核启动及根文件系统载入过程
    eclipse、MyEclipse实现批量改动文件编码
    菜鸟开发—应具备的搜索技巧
    Java之GUI编程(一)
  • 原文地址:https://www.cnblogs.com/findumars/p/5083438.html
Copyright © 2011-2022 走看看