Delphi调用REST很简单,首先在界面上放上:
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;
然后简单调用即可:
RESTClient1.BaseURL:=edtURL.Text;
RESTRequest1.Execute;
memLog.Text:=RESTResponse1.Content;
还可以对结果进行进一部处理,比如解析JSON:
procedure TfrmMain.btnGetClick(Sender: TObject); var jo,jo2:TJSONObject; jv:TJSONValue; ja:TJSONArray; jp:TJSONPair; i:Integer; begin RESTClient1.BaseURL:=edtURL.Text; RESTRequest1.Execute; memLog.Text:=RESTResponse1.Content; jo:=TJSONObject.Create; ja:=jo.ParseJSONValue(RESTResponse1.Content) as TJSONArray; for jv in ja do begin jo2:=jv as TJSONObject; for i:=0 to jo2.Count-1 do begin jp:=jo2.Pairs[i]; memLog.Lines.Add(jp.JsonString.ToString+':'+jp.JsonValue.ToString); end; end; end;
在这里我使用的是Delphi自带的JSON解析,注意引用单元system.json。