您的位置:首页 > 其它

DATASNAP REST WEBSERVICES中间件如何跨平台使用

2016-06-29 17:17 323 查看
准备使用DELPHI开发移动设备开发的朋友对DATASNAP REST中间件不可不了解。

DATASNAP REST新型WEBSERVICES中间件使用的通信协议和数据封装格式:

使用HTTP通信协议,HTTP协议哪个平台都支持;使用JSON作为数据的封装格式,几乎所有的开发语言都可以解析JSON数据。

REST的目的就是通过简单的URL来完成对中间层远程方法的调用并返回JSON格式的数据,调用方解析JSON数据然后将数据秀出来。

正是基于以上原因,DATASNAP REST中间件才可以为苹果和安卓的移动的NATIVE APP提供数据服务;也可以为WINDOWS、LINUX、MAC等

桌面型NATIVE APP提供数据服务。

下面笔者将对跨平台作出演示:

1.根据DELPHI的向导生成DATASNAP REST中间件(略过)。

2.在中间件远程方法里面增加一个方法:

function TServerMethods1.GetData(sql: string): tdataset;
begin
q.close;
q.sql.clear;
q.sql.text := sql;
q.Open;
Result:= q;
end;

3.客户端调用,因为演示跨平台的原因,此处只介绍通过URL调用中间件的方法。

procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
jo: ISuperObject;
ja, jb, jc: TSuperArray;
i, h: Integer;
FieldList: TStringList;
arr: array of array of string;
begin
Memo1.Clear;
FieldList:= TStringList.Create;
try
s := idhttp1.Get('http://localhost:8080/datasnap/rest/TServerMethods1/GetData/select * from t1 where iid=''2''');
Memo1.Lines.add(s);
jo := so(s);
ja := jo['result'].AsArray;
// 获取字段列表
jb := ja[0]['table'].AsArray;
for i := 0 to jb.Length-1 do begin
jc := jb[i].AsArray;
FieldList.Add(jc[0].AsString)
end;
// 数据集创建字段
cds.close;
cds.FieldDefs.Clear;
for i := 0 to fieldList.Count -1 do begin
CDS.FieldDefs.Add(fieldList[i],ftString,100, False);
end;
CDS.CreateDataSet;
// 设置表格的列宽
for i := 0 to dbgrid1.Columns.Count-1 do begin
DBGrid1.Columns[i].width := 80;
end;
// 数据集填充数据
SetLength(arr, ja[0][FieldList[0]].AsArray.Length, FieldList.Count);
for i := 0 to fieldlist.count-1 do begin // col
jb := ja[0][FieldList[i]].AsArray;
for h := 0 to jb.Length-1 do begin // row
arr[h, i] := jb[h].AsString;
end;
end;
cds.DisableControls; try
for i := 0 to jb.Length-1 do begin // row
cds.Append;
for h := 0 to fieldlist.count-1 do begin // col
cds.Fields[h].Value := arr[i, h];
end;
cds.Post;
end;
finally
cds.EnableControls;
end;

finally
FieldList.Free;
end;
end;



几乎所有的开发语言都支持通过HTTP GET,然后解析中间件返回的JSON数据。具体代码由各开发语言的程序员编写,此处

只介绍DELPHI如何URL调用的代码。

4.返回的JSON数据样例

{"result":[{"table":[["iid",26,0,0,50,102,102,0,false,false,0,false,false],["name",26,1,0,50,102,102,0,false,false,0,false,false]],"iid":["1","2"],"name":["\u6D4B\u8BD5\u4E00","\u6D4B\u8BD5\u4E8C"]}]}

各开发语言解析JSON数据然后呈现。

后记:

delphi xe5新增加了RESTCLIENT组件,用它替代indy的http控件调用REST WEBSERVICE,再也不用人工去解析返回的JSON格式去生成CLIENTDATASET数据了,设置几个属性即可,几乎是零编码。

unit Unit1;

interface

uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IPPeerClient, Data.DB, Datasnap.DBClient, REST.Response.Adapter, REST.Client, Data.Bind.Components, Data.Bind.ObjectScope, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, superobject;

type TForm1 = class(TForm) DBGrid1: TDBGrid; Button1: TButton; DataSource1: TDataSource; ClientDataSet1: TClientDataSet; IdHTTP1: TIdHTTP; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure rest_exec(cds: TClientDataSet; url: string); var jo: ISuperObject; ja, jb, jc: TSuperArray; i, h: Integer; FieldList: TStringList; arr: array of array of string; http: TIdHTTP; begin FieldList := TStringList.Create; http := TIdHTTP.Create(nil); try jo := so(http.Get(url)); ja := jo['result'].AsArray; // 获取字段列表 jb := ja[0]['table'].AsArray; for i := 0 to jb.Length - 1 do begin jc := jb[i].AsArray; FieldList.add(jc[0].AsString) end; // 数据集创建字段 cds.close; cds.FieldDefs.Clear; for i := 0 to FieldList.Count - 1 do begin cds.FieldDefs.add(FieldList[i], Data.DB.ftString, 100, False); end; cds.CreateDataSet; // 数据集填充数据 SetLength(arr, ja[0][FieldList[0]].AsArray.Length, FieldList.Count); for i := 0 to FieldList.Count - 1 do begin // col jb := ja[0][FieldList[i]].AsArray; for h := 0 to jb.Length - 1 do begin // row arr[h, i] := jb[h].AsString; end; end; cds.DisableControls; try for i := 0 to jb.Length - 1 do begin // row cds.Append; for h := 0 to FieldList.Count - 1 do begin // col cds.Fields[h].Value := arr[i, h]; end; cds.Post; end; finally cds.EnableControls; end;

finally FieldList.Free; http.Free; end; end;

procedure TForm1.Button1Click(Sender: TObject); var url: string; begin url := 'http://localhost:18888/cxg/middle/TSysMethods/rest_GetData/select * from pos_master/0'; rest_exec(ClientDataSet1, url); end;

end.
http://www.cnblogs.com/hnxxcxg/p/3280230.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: