Advertisement
adrianoschneider

Teste Código Delphi

Aug 16th, 2017
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 23.49 KB | None | 0 0
  1. {
  2. Autor: Adriano Schneider
  3. Data: 2017-08
  4. }
  5.  
  6. unit ufrmPrincipal;
  7.  
  8. interface
  9.  
  10. uses
  11.   System.SysUtils,
  12.   System.Types,
  13.   System.UITypes,
  14.   System.Variants,
  15.   System.Rtti,
  16.   System.JSON,
  17.   IPPeerClient,
  18.   FMX.Types,
  19.   FMX.Controls,
  20.   FMX.Forms,
  21.   FMX.Graphics,
  22.   FMX.Dialogs,
  23.   FMX.TabControl,
  24.   FMX.Layouts,
  25.   FMX.Controls.Presentation,
  26.   FMX.StdCtrls,
  27.   FMX.Objects,
  28.   FMX.ScrollBox,
  29.   FMX.Memo,
  30.   FMX.ComboEdit,
  31.   FMX.Edit,
  32.   FMX.Grid.Style,
  33.   FMX.Grid,
  34.   FMX.ListView.Types,
  35.   FMX.ListView.Appearances,
  36.   FMX.ListView.Adapters.Base,
  37.   FMX.ListView,
  38.   REST.Client,
  39.   REST.Types,
  40.   System.JSON.Builders,
  41.   System.JSON.Readers,
  42.   Data.Bind.Components,
  43.   Data.Bind.ObjectScope,
  44.   FireDAC.Stan.Intf,
  45.   FireDAC.Stan.Option,
  46.   FireDAC.Stan.Param,
  47.   FireDAC.Stan.Error,
  48.   FireDAC.DatS,
  49.   FireDAC.Phys.Intf,
  50.   FireDAC.DApt.Intf,
  51.   Datasnap.DBClient,
  52.   Data.DB,
  53.   FireDAC.Comp.DataSet,
  54.   FireDAC.Comp.Client,
  55.   System.Classes,
  56.   System.Generics.Collections,
  57.   System.Math.Vectors;
  58.  
  59. type
  60.   TfrmPrincipal = class(TForm)
  61.     Layout1: TLayout;
  62.     Layout2: TLayout;
  63.     Layout3: TLayout;
  64.     TabControl: TTabControl;
  65.     tabRegister: TTabItem;
  66.     tabLogin: TTabItem;
  67.     Layout4: TLayout;
  68.     Layout5: TLayout;
  69.     Label1: TLabel;
  70.     pnlTitulo: TRectangle;
  71.     lblTitulo: TLabel;
  72.     SpeedButton1: TSpeedButton;
  73.     tabList: TTabItem;
  74.     tabGet: TTabItem;
  75.     tabUpdate: TTabItem;
  76.     Layout6: TLayout;
  77.     Layout7: TLayout;
  78.     tabDelete: TTabItem;
  79.     TabItem7: TTabItem;
  80.     Layout8: TLayout;
  81.     Layout9: TLayout;
  82.     Layout10: TLayout;
  83.     Layout11: TLayout;
  84.     Memo: TMemo;
  85.     Layout12: TLayout;
  86.     edtAPIKEY: TEdit;
  87.     edtURL: TComboEdit;
  88.     Rectangle1: TRectangle;
  89.     ListView1: TListView;
  90.     Splitter1: TSplitter;
  91.     Layout13: TLayout;
  92.     Label7: TLabel;
  93.     edtRegistrarNome: TEdit;
  94.     Layout14: TLayout;
  95.     Label8: TLabel;
  96.     edtRegistrarEmail: TEdit;
  97.     Layout15: TLayout;
  98.     Label9: TLabel;
  99.     edtRegistrarSenha: TEdit;
  100.     Layout16: TLayout;
  101.     Label2: TLabel;
  102.     edtEmail: TEdit;
  103.     Label10: TLabel;
  104.     Layout17: TLayout;
  105.     Label11: TLabel;
  106.     edtPassword: TEdit;
  107.     Layout18: TLayout;
  108.     Label12: TLabel;
  109.     edtRegistrarCelular: TEdit;
  110.     Label13: TLabel;
  111.     Label3: TLabel;
  112.     Label4: TLabel;
  113.     Label5: TLabel;
  114.     tabCreate: TTabItem;
  115.     Layout19: TLayout;
  116.     Label6: TLabel;
  117.     Layout20: TLayout;
  118.     Label14: TLabel;
  119.     edtRegistrarReSenha: TEdit;
  120.     Layout21: TLayout;
  121.     Label15: TLabel;
  122.     Edit8: TEdit;
  123.     Layout22: TLayout;
  124.     Label16: TLabel;
  125.     btnLogin: TButton;
  126.     Layout23: TLayout;
  127.     Label17: TLabel;
  128.     btnRegistrar: TButton;
  129.     Layout24: TLayout;
  130.     Label18: TLabel;
  131.     Button3: TButton;
  132.     Layout25: TLayout;
  133.     Label19: TLabel;
  134.     Edit9: TEdit;
  135.     Layout26: TLayout;
  136.     Label20: TLabel;
  137.     Button4: TButton;
  138.     Layout27: TLayout;
  139.     Label21: TLabel;
  140.     Edit10: TEdit;
  141.     Layout28: TLayout;
  142.     Label22: TLabel;
  143.     Button5: TButton;
  144.     Layout29: TLayout;
  145.     Label23: TLabel;
  146.     Edit11: TEdit;
  147.     RESTClient: TRESTClient;
  148.     RESTRequest: TRESTRequest;
  149.     Layout30: TLayout;
  150.     Label24: TLabel;
  151.     lblError: TLabel;
  152.     Layout31: TLayout;
  153.     Label25: TLabel;
  154.     lblRegistrarError: TLabel;
  155.     Layout32: TLayout;
  156.     Button1: TButton;
  157.     Layout33: TLayout;
  158.     Label26: TLabel;
  159.     Button2: TButton;
  160.     edtID: TEdit;
  161.     btnVaiRegister: TButton;
  162.     Layout34: TLayout;
  163.     Label27: TLabel;
  164.     Button9: TButton;
  165.     Button6: TButton;
  166.     Layout35: TLayout;
  167.     Label28: TLabel;
  168.     Edit1: TEdit;
  169.     Layout36: TLayout;
  170.     Label29: TLabel;
  171.     Edit2: TEdit;
  172.     Layout37: TLayout;
  173.     Label30: TLabel;
  174.     Edit3: TEdit;
  175.     Layout38: TLayout;
  176.     Label31: TLabel;
  177.     Edit4: TEdit;
  178.     StringGrid: TStringGrid;
  179.     tabTeste: TTabItem;
  180.     Button7: TButton;
  181.     Memo2: TMemo;
  182.     Memo1: TMemo;
  183.     Button8: TButton;
  184.     Button10: TButton;
  185.     Layout39: TLayout;
  186.     Button11: TButton;
  187.     Splitter2: TSplitter;
  188.     tabTeste2: TTabItem;
  189.     Layout41: TLayout;
  190.     btnSave: TButton;
  191.     Button22: TButton;
  192.     Memo3: TMemo;
  193.     Splitter3: TSplitter;
  194.     TabItem1: TTabItem;
  195.     Layout42: TLayout;
  196.     Button21: TButton;
  197.     Button23: TButton;
  198.     Splitter4: TSplitter;
  199.     Memo4: TMemo;
  200.     TabItem2: TTabItem;
  201.     Layout43: TLayout;
  202.     Splitter5: TSplitter;
  203.     Memo5: TMemo;
  204.     StrGrdCsv: TStringGrid;
  205.     StringColumn1: TStringColumn;
  206.     StringColumn2: TStringColumn;
  207.     StringColumn3: TStringColumn;
  208.     StringColumn4: TStringColumn;
  209.     StringColumn5: TStringColumn;
  210.     FDMemTable1: TFDMemTable;
  211.     ClientDataSet1: TClientDataSet;
  212.     DataSource1: TDataSource;
  213.     Grid1: TGrid;
  214.     Button12: TButton;
  215.     Layout40: TLayout;
  216.     Layout44: TLayout;
  217.     Label32: TLabel;
  218.     edtNome: TEdit;
  219.     Layout45: TLayout;
  220.     Label33: TLabel;
  221.     Layout46: TLayout;
  222.     Label34: TLabel;
  223.     edtIdade: TEdit;
  224.     Layout48: TLayout;
  225.     Label36: TLabel;
  226.     Layout49: TLayout;
  227.     RadioButton1: TRadioButton;
  228.     RadioButton2: TRadioButton;
  229.     btnObjectToJson: TButton;
  230.     btnJsonToObject: TButton;
  231.     Button13: TButton;
  232.     procedure SpeedButton1Click(Sender: TObject);
  233.     procedure FormActivate(Sender: TObject);
  234.     procedure btnLoginClick(Sender: TObject);
  235.     procedure btnRegistrarClick(Sender: TObject);
  236.     procedure Button1Click(Sender: TObject);
  237.     procedure Button3Click(Sender: TObject);
  238.     procedure Button2Click(Sender: TObject);
  239.     procedure Button4Click(Sender: TObject);
  240.     procedure Button5Click(Sender: TObject);
  241.     procedure btnVaiRegisterClick(Sender: TObject);
  242.     procedure Button9Click(Sender: TObject);
  243.     procedure Button6Click(Sender: TObject);
  244.     procedure Button7Click(Sender: TObject);
  245.     procedure Button8Click(Sender: TObject);
  246.     procedure Button10Click(Sender: TObject);
  247.     procedure Button11Click(Sender: TObject);
  248.     procedure btnSaveClick(Sender: TObject);
  249.     procedure Button21Click(Sender: TObject);
  250.     procedure btnObjectToJsonClick(Sender: TObject);
  251.     procedure btnJsonToObjectClick(Sender: TObject);
  252.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  253.     procedure Button13Click(Sender: TObject);
  254.   private
  255.     procedure ParseObject;
  256.     procedure JsonToDataset(aDataset: TDataSet; aJSON: string);
  257.     function JsonListObjeto(jsonString: String; classe: TClass): TList<TObject>;
  258.     function JsonListObjeto1(valor: String; classe: TClass): TList<TObject>;
  259.     function DataSetToJsonTxt(pDataSet: TDataSet): string;
  260.     { Private declarations }
  261.   public
  262.     { Public declarations }
  263.   end;
  264.  
  265. Type
  266.   TPessoa = class
  267.     strict private
  268.       FNome:String;
  269.       FIdade:integer;
  270.       FEstadoCivil:Boolean;
  271.     public
  272.       property Nome:String read FNome write FNome;
  273.       property Idade:integer read FIdade write FIdade;
  274.       property EstadoCivil:Boolean read FEstadoCivil write FEstadoCivil;
  275.   end;
  276.  
  277. type
  278.   THelper = record
  279.   end;
  280.  
  281. type
  282.   TCliente = record
  283.   end;
  284.  
  285. var
  286.   frmPrincipal: TfrmPrincipal;
  287.   objValueJson:TJSONValue;
  288.   FobjValueJson:TJSONValue;
  289.  
  290. implementation
  291.  
  292. {$R *.fmx}
  293.  
  294. uses
  295.   REST.Response.Adapter,
  296.   DBXJSon,
  297.   DBXJsonReflect;
  298.  
  299. procedure TfrmPrincipal.btnLoginClick(Sender: TObject);
  300. var
  301.   vJSON: string;
  302.   valJSON: TJSONValue;
  303. begin
  304.   RESTClient.ContentType := 'application/x-www-form-urlencoded';
  305.   RESTClient.BaseURL := edtURL.text+'/login';
  306.   RESTRequest.Method := TRESTRequestMethod.rmPOST;
  307.   RESTRequest.Params.Clear;
  308.   RESTRequest.AddParameter(
  309.     {AName} 'email',
  310.     {AValue} edtEmail.Text,
  311.     {AKind} TRESTRequestParameterKind.pkGETorPOST
  312.   );
  313.   RESTRequest.AddParameter(
  314.     {AName} 'password',
  315.     {AValue} edtPassword.Text,
  316.     {AKind} TRESTRequestParameterKind.pkGETorPOST
  317.   );
  318.   RESTRequest.SynchronizedEvents :=  False;
  319.   RESTRequest.Execute;
  320.   Memo.Lines.Clear;
  321.   Memo.Lines.Add(RESTClient.BaseURL);
  322.   vJSON := RESTRequest.Response.JSONText;
  323.   valJSON := TJSONObject.ParseJSONValue(vJSON);
  324.   if (valJSON.GetValue<string>('error')='false') then
  325.   begin
  326.     edtAPIKEY.Text := valJSON.GetValue<string>('apiKey');
  327.     lblError.Text := '';
  328.   end
  329.   else
  330.   begin
  331.     edtAPIKEY.Text := '';
  332.     lblError.Text := valJSON.GetValue<string>('message');;
  333.   end;
  334.   Memo.Lines.Add(vJSON);
  335.   TabControl.ActiveTab := tabList;
  336. end;
  337.  
  338. procedure TfrmPrincipal.btnRegistrarClick(Sender: TObject);
  339. var
  340.   vJSON: string;
  341.   valJSON: TJSONValue;
  342. begin
  343.   if (edtRegistrarSenha.Text<>edtRegistrarReSenha.Text) then
  344.   begin
  345.     lblRegistrarError.Text := 'A digitação da senha não coincidiu!';
  346.     edtRegistrarSenha.SetFocus;
  347.   end
  348.   else
  349.   begin
  350.     lblRegistrarError.Text := '';
  351.     RESTClient.ContentType := 'application/x-www-form-urlencoded';
  352.     RESTClient.BaseURL := edtURL.text+'/register';
  353.     RESTRequest.Method := TRESTRequestMethod.rmPOST;
  354.     RESTRequest.Params.Clear;
  355.     RESTRequest.AddParameter(
  356.       {AName} 'name',
  357.       {AValue} edtRegistrarNome.Text,
  358.       {AKind} TRESTRequestParameterKind.pkGETorPOST
  359.     );
  360.     RESTRequest.AddParameter(
  361.       {AName} 'celular',
  362.       {AValue} edtRegistrarCelular.Text,
  363.       {AKind} TRESTRequestParameterKind.pkGETorPOST
  364.     );
  365.     RESTRequest.AddParameter(
  366.       {AName} 'email',
  367.       {AValue} edtRegistrarEmail.Text,
  368.       {AKind} TRESTRequestParameterKind.pkGETorPOST
  369.     );
  370.     RESTRequest.AddParameter(
  371.       {AName} 'password',
  372.       {AValue} edtRegistrarSenha.Text,
  373.       {AKind} TRESTRequestParameterKind.pkGETorPOST
  374.     );
  375.     RESTRequest.SynchronizedEvents :=  False;
  376.     RESTRequest.Execute;
  377.     Memo.Lines.Clear;
  378.     Memo.Lines.Add(RESTClient.BaseURL);
  379.     vJSON := RESTRequest.Response.JSONText;
  380.     valJSON := TJSONObject.ParseJSONValue(vJSON);
  381.     if (valJSON.GetValue<string>('error')='false') then
  382.     begin
  383.       lblRegistrarError.Text := '';
  384.     end
  385.     else
  386.     begin
  387.       lblRegistrarError.Text := valJSON.GetValue<string>('message');;
  388.     end;
  389.     Memo.Lines.Add(vJSON);
  390.   end;
  391. end;
  392.  
  393. procedure TfrmPrincipal.btnVaiRegisterClick(Sender: TObject);
  394. begin
  395.   TabControl.ActiveTab := tabRegister;
  396. end;
  397.  
  398. procedure TfrmPrincipal.Button10Click(Sender: TObject);
  399. var
  400.   o: TJSONObject;
  401.   a: TJSONArray;
  402.   book: TJSONObject;
  403.   idx: integer;
  404.   idy: integer;
  405. begin
  406.   o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONObject;
  407.   try
  408.     a := TJSONArray(o.Get('books').JsonValue);
  409.     for idx := 0 to pred(a.size) do begin
  410.       book := TJSONObject(a.Get(idx));
  411.       for idy := 0 to pred(book.Count) do begin
  412.         ShowMessage( book.Pairs[idy].JsonString.ToString + ':' + book.Pairs[idy].JsonValue.ToString );
  413.       end;
  414.     end;
  415.   finally
  416.     o.Free;
  417.   end;
  418. end;
  419.  
  420. procedure TfrmPrincipal.Button11Click(Sender: TObject);
  421. var
  422.   oJSON: TJSONObject;
  423.   aJSON: TJSONArray;
  424.   oRecordJSON: TJSONObject;
  425.   idx: integer;
  426.   idy: integer;
  427. begin
  428.   oJSON := TJSONObject.ParseJSONValue(
  429.              TEncoding.ASCII.GetBytes(Memo2.Lines.Text),0) as TJSONObject;
  430.   ShowMessage(oJSON.GetValue('total').ToString);
  431.   try
  432.     aJSON := TJSONArray(oJSON.Get('registros').JsonValue);
  433.     for idx := 0 to pred(aJSON.size) do begin
  434.       oRecordJSON := TJSONObject(aJSON.Get(idx));
  435.       for idy := 0 to pred(oRecordJSON.Count) do begin
  436.         ShowMessage(oRecordJSON.Pairs[idy].JsonString.ToString + ':' +
  437.                     oRecordJSON.Pairs[idy].JsonValue.ToString);
  438.       end;
  439.     end;
  440.   finally
  441.     oJSON.Free;
  442.   end;
  443. end;
  444.  
  445. function TfrmPrincipal.JsonListObjeto(jsonString: String; classe: TClass): TList<TObject>;
  446. var lista:Tlist<TObject>;
  447.     jo: TJSONObject;
  448.     obj: TObject;
  449.     jl: TJSONArray;
  450.     i:integer;
  451.     unMarshal: TJSONUnMarshal;
  452.     //xxxxx
  453. begin
  454.   lista := TList<TObject>.Create;
  455.   jo := TJSONObject.Create;
  456.   obj := TObject.Create;
  457.   jl := TJSONArray.Create;
  458.   unMarshal := TJSONUnMarshal.Create;
  459.   jl := TJSONObject.ParseJSONValue(jsonString) as TJsonArray;
  460.   if Assigned(jl) then
  461.   begin
  462.     for i := 0 to jl.Count -1 do
  463.     begin
  464.       jo := jl.Items[i] as TJSONObject;
  465.       //Verificar como funcionava...
  466.       //obj :=   unMarshal.CreateObject(classe,jo);
  467.       lista.Add(obj);
  468.     end;
  469.   end;
  470.   result := lista;
  471. end;
  472.  
  473. procedure TfrmPrincipal.Button13Click(Sender: TObject);
  474. begin
  475.   Memo5.Lines.Text := DataSetToJsonTxt(ClientDataSet1);//(PasseAquiSeuDataSet)
  476. end;
  477.  
  478. function TfrmPrincipal.JsonListObjeto1(valor: String; classe: TClass): TList<TObject>;
  479. var
  480.   lista:Tlist<TObject>;
  481.   jo: TJSONObject;
  482.   obj: TObject;
  483.   jl: TJSONArray;
  484.   i:integer;
  485.   unMarshal: TJSONUnMarshal;
  486. begin
  487.   lista := TList<TObject>.Create;
  488.   jo := TJSONObject.Create;
  489.   obj := TObject.Create;
  490.   jl := TJSONArray.Create;
  491.   unMarshal := TJSONUnMarshal.Create;
  492.   jl := TJSONObject.ParseJSONValue(valor) as TJsonArray;
  493.   for i := 0 to jl.Count -1 do
  494.   begin
  495.     jo := jl.Items[i] as TJSONObject;
  496.     //obj := unMarshal.CreateObject(classe,jo);
  497.     lista.Add(obj);
  498.   end;
  499.   result := lista;
  500. end;
  501.  
  502. procedure TfrmPrincipal.btnObjectToJsonClick(Sender: TObject);
  503. var
  504.   M: TJSONMarshal;
  505.   pessoa: TPessoa;
  506. begin
  507.   pessoa := TPessoa.Create;
  508.   try
  509.     pessoa.Nome := edtNome.Text;
  510.     pessoa.Idade := StrToIntDef(edtIdade.Text, 0);
  511.     //case rdgEstadoCivil.ItemIndex of
  512.     //  0: pessoa.EstadoCivil := true;
  513.     //  1: pessoa.EstadoCivil := false
  514.     //end;
  515.     M := TJSONMarshal.Create(TJSONConverter.Create);
  516.     try
  517.       FobjValueJson.Free;
  518.       FobjValueJson := M.Marshal(pessoa);
  519.       Memo5.Lines.text := FobjValueJson.ToString;
  520.     finally
  521.       M.Free;
  522.     end;
  523.   finally
  524.     pessoa.Free;
  525.   end;
  526. end;
  527.  
  528. procedure TfrmPrincipal.btnJsonToObjectClick(Sender: TObject);
  529. var
  530.   unM: TJSONUnMarshal;
  531.   pessoa: TPessoa;
  532. begin
  533.   if not Assigned(FobjValueJson) then
  534.   begin
  535.     exit();
  536.   end;
  537.   unM := TJSONUnMarshal.Create;
  538.   pessoa := TPessoa(unM.Unmarshal(FObjValueJSon));
  539.   edtNome.Text := pessoa.Nome;
  540.   edtIdade.Text := IntToStr(pessoa.Idade);
  541.   //case integer(pessoa.EstadoCivil) of
  542.   //  0: rdgEstadoCivil.ItemIndex := 0;
  543.   //  1: rdgEstadoCivil.ItemIndex := 1;
  544.   //end;
  545.   Memo5.Text := FobjValueJson.ToString;
  546.   unM.Free;
  547.   pessoa.Free;
  548. end;
  549.  
  550. procedure TfrmPrincipal.Button1Click(Sender: TObject);
  551. begin
  552.   RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
  553.   RESTClient.BaseURL := edtURL.text+'/rfid';
  554.   RESTRequest.Method := TRESTRequestMethod.rmGET;
  555.   RESTRequest.Execute;
  556.   Memo.Lines.Clear;
  557.   Memo.Lines.Add(RESTRequest.Response.JSONText);
  558. end;
  559.  
  560. procedure TfrmPrincipal.btnSaveClick(Sender: TObject);
  561. var
  562.   i: Integer;
  563.   lJsonObj: TJSONObject;
  564. begin
  565.   lJsonObj := TJSONObject.Create;
  566.   if btnSave.Text = 'Salvar' then
  567.   begin
  568.     for i := 0 to StrGrdCsv.ColumnCount do
  569.     begin
  570.       lJsonObj.AddPair(StrGrdCsv.Cells[i, 0], StrGrdCsv.Cells[i, 1]);
  571.     end;
  572.     //TFile.WriteAllText(cmbBXDBTables.Text + '.json', lJsonObj.ToString);
  573.     btnSave.Text := 'Load Data';
  574.   end;
  575. end;
  576.  
  577. procedure TfrmPrincipal.JsonToDataset(aDataset : TDataSet; aJSON : string);
  578. var
  579.   JObj: TJSONArray;
  580.   vConv : TCustomJSONDataSetAdapter;
  581. begin
  582.   if (aJSON = EmptyStr) then
  583.   begin
  584.     Exit;
  585.   end;
  586.   JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONArray;
  587.   vConv := TCustomJSONDataSetAdapter.Create(Nil);
  588.   try
  589.     vConv.Dataset := aDataset;
  590.     vConv.UpdateDataSet(JObj);
  591.   finally
  592.     vConv.Free;
  593.     JObj.Free;
  594.   end;
  595. end;
  596.  
  597. procedure TfrmPrincipal.Button21Click(Sender: TObject);
  598. var
  599.   sJSON : string;
  600. begin
  601.   sJSON := Memo4.Text;
  602.   JsonToDataset(FDMemTable1, sJSON);
  603.   JsonToDataset(ClientDataSet1, sJSON);
  604. end;
  605.  
  606. procedure TfrmPrincipal.Button2Click(Sender: TObject);
  607. begin
  608.   RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
  609.   RESTClient.BaseURL := edtURL.text+'/rfid/'+edtID.Text;
  610.   RESTRequest.Method := TRESTRequestMethod.rmGET;
  611.   RESTRequest.Params.Clear;
  612.   RESTRequest.Execute;
  613.   Memo.Lines.Clear;
  614.   Memo.Lines.Add(RESTRequest.Response.JSONText);
  615. end;
  616.  
  617. procedure TfrmPrincipal.Button3Click(Sender: TObject);
  618. begin
  619.   RESTClient.ContentType := 'application/x-www-form-urlencoded';
  620.   RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
  621.   RESTClient.BaseURL := edtURL.text+'/rfid';
  622.   RESTRequest.Method := TRESTRequestMethod.rmPOST;
  623.   RESTRequest.Params.Clear;
  624.   RESTRequest.AddParameter(
  625.     {AName} 'task',
  626.     {AValue} 'Tarefa nro: '+FormatDateTime('dd/mm/yy hh:nn:ss zzz', Now),
  627.     {AKind} TRESTRequestParameterKind.pkGETorPOST
  628.   );
  629.   RESTRequest.SynchronizedEvents :=  False;
  630.   RESTRequest.Execute;
  631.   Memo.Lines.Clear;
  632.   Memo.Lines.Add(RESTRequest.Response.JSONText);
  633. end;
  634.  
  635. procedure TfrmPrincipal.Button4Click(Sender: TObject);
  636. begin
  637.   RESTClient.ContentType := 'application/x-www-form-urlencoded';
  638.   RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
  639.   RESTClient.BaseURL := edtURL.text+'/rfid/'+edtID.Text;
  640.   RESTRequest.Method := TRESTRequestMethod.rmPUT;
  641.   RESTRequest.Params.Clear;
  642.   RESTRequest.AddParameter(
  643.     {AName} 'task',
  644.     {AValue} 'Tarefa '+FormatDateTime('dd/mm/yyyy hh:nn:ss zzz', now)+' alterada',
  645.     {AKind} TRESTRequestParameterKind.pkGETorPOST
  646.   );
  647.   RESTRequest.AddParameter(
  648.     {AName} 'status',
  649.     {AValue} '2',
  650.     {AKind} TRESTRequestParameterKind.pkGETorPOST
  651.   );
  652.   RESTRequest.SynchronizedEvents :=  False;
  653.   RESTRequest.Execute;
  654.   Memo.Lines.Clear;
  655.   Memo.Lines.Add(RESTRequest.Response.JSONText);
  656. end;
  657.  
  658. procedure TfrmPrincipal.Button5Click(Sender: TObject);
  659. begin
  660.   RESTClient.ContentType := 'application/x-www-form-urlencoded';
  661.   RESTClient.Params.AddHeader('Authorization', edtAPIKEY.Text);
  662.   RESTClient.BaseURL := edtURL.text+'/rfid/'+edtID.Text;
  663.   RESTRequest.Method := TRESTRequestMethod.rmDELETE;
  664.   RESTRequest.Params.Clear;
  665.   RESTRequest.SynchronizedEvents :=  False;
  666.   RESTRequest.Execute;
  667.   Memo.Lines.Clear;
  668.   Memo.Lines.Add(RESTRequest.Response.JSONText);
  669. end;
  670.  
  671. procedure TfrmPrincipal.Button6Click(Sender: TObject);
  672. begin
  673.   TabControl.ActiveTab := tabLogin;
  674. end;
  675.  
  676. procedure TfrmPrincipal.Button7Click(Sender: TObject);
  677. begin
  678.   ParseObject;
  679. end;
  680.  
  681. procedure TfrmPrincipal.Button8Click(Sender: TObject);
  682. var
  683.   o: TJSONObject;
  684.   a: TJSONArray;
  685.   book: TJSONObject;
  686. begin
  687.   // Create the outer JSON object which parents the others.
  688.   o := TJSONObject.Create;
  689.   try
  690.     // Create the books object, which contains the array of books...
  691.     a := TJSONArray.Create();
  692.     // add the array to the object.
  693.     o.AddPair('books',a);
  694.     // Create the first book
  695.     book := TJSONObject.Create;
  696.     book.AddPair( TJSONPair.Create('title','Zen') );
  697.     book.AddPair( TJSONPair.Create('subtitle','and The art of motorcycle maintenance.') );
  698.     book.AddPair( TJSONPair.Create('author','Robert M Pirsig') );
  699.     book.AddPair( TJSONPair.Create('isbn','9780061908019') );
  700.     // Add the book to the array
  701.     a.AddElement(book);
  702.     // Create the second book
  703.     book := TJSONObject.Create;
  704.     book.AddPair( TJSONPair.Create('title','Coding in Delphi') );
  705.     book.AddPair( TJSONPair.Create('subtitle','') );
  706.     book.AddPair( TJSONPair.Create('author','Nick Hodges') );
  707.     book.AddPair( TJSONPair.Create('isbn','978-1941266038') );
  708.     // Add the book to the array
  709.     a.AddElement(book);
  710.   finally
  711.     Memo1.Lines.Text := o.ToString;
  712.     o.Free;
  713.   end;
  714. end;
  715.  
  716. Procedure TfrmPrincipal.ParseObject;
  717. var
  718.   LIterator: TJSONIterator;
  719.   LJsonTextReader: TJsonTextReader;
  720.   LStringReader: TStringReader;
  721.   vJSON: string;
  722. begin
  723.   //
  724.   LStringReader := TStringReader.Create(Memo2.Text);
  725.   //
  726.   LJsonTextReader := TJsonTextReader.Create(LStringReader);
  727.   LIterator := TJSONIterator.Create(LJsonTextReader);
  728.   LIterator.Recurse; // prepare to enter array
  729.   LIterator.Next;
  730.   LIterator.Recurse;  // enter array
  731.   Memo1.Lines.Add(LIterator.Key); // Transaction
  732.   LIterator.Recurse; // prepare to enter object
  733.   LIterator.Next;
  734.   LIterator.Recurse; // enter array object
  735.   LIterator.Next;
  736.   Memo1.Lines.Add(LIterator.Key +': '+ IntToStr(LIterator.AsInteger)); // id : 662713
  737.   LIterator.Next;
  738.   Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); //  firstName : John
  739.   LIterator.Next;
  740.   Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // lastName : Doe
  741.   LIterator.Next;
  742.   Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsDouble.ToString);   // price : 2.1
  743.   LIterator.Next;
  744.   if LIterator.IsNull then
  745.     Memo1.Lines.Add(LIterator.Key +': null');  // parent_id : null
  746.   LIterator.Next;
  747.   if LIterator.AsBoolean then
  748.     Memo1.Lines.Add(LIterator.Key +': true') // validated : true
  749.   else
  750.     Memo1.Lines.Add(LIterator.Key +': false');
  751.   LIterator.Return; // back to parent structure
  752.   LIterator.Recurse; // prepare to enter object
  753.   LIterator.Next;
  754.   LIterator.Recurse; // enter object
  755.   LIterator.Next;
  756.   Memo1.Lines.Add(LIterator.Key +': '+ IntToStr(LIterator.AsInteger)); // id : 662714
  757.   LIterator.Next('price');  // move to key 'price'
  758.   Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsDouble.ToString); // price : 4.5
  759.   LIterator.Next;
  760.   if LIterator.IsNull then
  761.   Memo1.Lines.Add(LIterator.Key +': null');  // parent_id : null
  762.   LIterator.Return;  // back to parent structure
  763.   LIterator.Recurse;  // prepare to enter object
  764.   LIterator.Next;
  765.   LIterator.Recurse; // enter object
  766.   LIterator.Next;
  767.   LIterator.Next('firstName'); // move to key 'firstName'
  768.   Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // firstName : Peter
  769.   LIterator.Next;
  770.   Memo1.Lines.Add(LIterator.Key +': '+ LIterator.AsString); // lastName : Jones
  771. end;
  772.  
  773. procedure TfrmPrincipal.Button9Click(Sender: TObject);
  774. begin
  775.   ShowMessage('Enviar email');
  776. end;
  777.  
  778. function TfrmPrincipal.DataSetToJsonTxt(pDataSet: TDataSet): string;
  779. var
  780.   ArrayJSon: TJSONArray;
  781.   ObjJSon: TJSONObject;
  782.   strJSon: TJSONString;
  783.   intJSon: TJSONNumber;
  784.   TrueJSon: TJSONTrue;
  785.   FalseJSon: TJSONFalse;
  786.   I: Integer;
  787.   pField: TField;
  788. begin
  789.   ArrayJSon := TJSONArray.Create;
  790.     try
  791.       pDataSet.First;
  792.         while not pDataSet.Eof do
  793.           begin
  794.             ObjJSon := TJSONObject.Create;
  795.             for pField in pDataSet.Fields do
  796.             case pField.DataType of
  797.               ftString:
  798.                 begin
  799.                   strJSon := TJSONString.Create(pField.AsString);
  800.                   ObjJSon.AddPair(pField.FieldName,strJSon);
  801.                 end;
  802.              ftInteger:
  803.                  begin
  804.                   IntJSon := TJSONNumber.Create(pField.AsInteger);
  805.                   ObjJSon.AddPair(pField.FieldName,IntJSon);
  806.                 end;
  807. //             ftBoolean:
  808. //               case rdgEstadoDataset.ItemIndex of
  809. //                 0:begin
  810. //                     TrueJSon := TJSONTrue.Create;
  811. //                     ObjJSon.AddPair(pField.FieldName,TrueJSon);
  812. //                   end;
  813. //                 1:begin
  814. //                      FalseJSon := TJSONFalse.Create;
  815. //                      ObjJSon.AddPair(pField.FieldName,FalseJSon);
  816. //                   end;
  817. //          end;
  818.       else //casos gerais são tratados como string
  819.       begin
  820.         strJSon := TJSONString.Create(pField.AsString);
  821.         ObjJSon.AddPair(pField.FieldName,strJSon);
  822.       end;
  823.     end;
  824.     ArrayJSon.AddElement(ObjJSon);
  825.     pDataSet.next;
  826.   end;
  827.   result := ArrayJSon.ToString;
  828.   finally
  829.     ArrayJSon.Free;
  830.   end;
  831. end;
  832.  
  833. procedure TfrmPrincipal.FormActivate(Sender: TObject);
  834. begin
  835.   RESTClient.Accept := 'application/json, text/plain; q=0.9, text/html;q=0.8,';
  836.   RESTClient.AcceptCharset := 'UTF-8, *;q=0.8';
  837.   TabControl.ActiveTab := tabTeste;
  838. end;
  839.  
  840. procedure TfrmPrincipal.FormClose(Sender: TObject; var Action: TCloseAction);
  841. begin
  842.   FobjValueJson.Free;
  843. end;
  844.  
  845. procedure TfrmPrincipal.SpeedButton1Click(Sender: TObject);
  846. begin
  847.   Layout10.Visible := not Layout10.Visible;
  848. end;
  849.  
  850. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement