Beispiel RestAPI über Proxy

Rund um die LCL und andere Komponenten
Antworten
catweasel
Beiträge: 230
Registriert: Di 17. Mär 2009, 10:51
OS, Lazarus, FPC: Win10 64Bit // Linux Mint 20.0 - (L 2.2.0 FPC 3.2.2)

Beispiel RestAPI über Proxy

Beitrag von catweasel »

Hallo zusammen

Ich hatte mit dem Beitrag "Beispiel RestAPI" (http://www.lazarusforum.de/viewtopic.php?f=15&t=11417)
schon mal das Thema RestAPI und schreiben der Daten in ein ListView auf dem Plan.
Nun muß ich das ganze leider auch mit einem Proxy im Hintergrund zum laufen bringen, was bisher nicht notwendig war.

Leider weis ich zu wenig über Proxy im zusammenhang mit fphttpclient um das Beispielprogramm auch mit Proxy zum Laufen zu bekommen.
Kann mir da jemand unter die Arme greifen?


Bei dem Desktop vom Beispielprogramm ist ein Button "Button1" und ein einfaches ListView1 (3 items, ViewStyle: vsReport)

Gruß
Michael


Code: Alles auswählen

unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  Grids, StdCtrls, ComCtrls, VirtualTrees, fphttpclient, fpjson, jsonparser,
  jsonscanner;
 
type
 
  { TForm1 }
 
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    ListView1: TListView;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
  private
    function GetData: String;
    function ParseJson(const Json: String): TJSONArray;
  public
 
  end;
 
var
  Form1: TForm1;
 
 
 
implementation
 
{$R *.lfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  Data        : TJSONArray;
  DataString  : String;
  o           : TJSONObject;
  i           : Integer;
begin
  Screen.Cursor:=crHourGlass; //crDefault
 
  DataString := GetData;
  try
    ListView1.BeginUpdate;
    ListView1.Clear;
 
    Data := ParseJson(DataString);
    for i := 0 to (Data.Count - 1) do begin
      o := Data.Objects[i];
      ListView1.Items.Add;
      ListView1.Items.Item[i].Caption:=o.Strings['id'];
      ListView1.Items.Item[i].SubItems.Add(o.Strings['userId']);
      ListView1.Items.Item[i].SubItems.Add(o.Strings['title']);
    end;
 
  finally
    FreeAndNil(Data);
    ListView1.EndUpdate;
    Screen.Cursor:=crDefault; //crHourGlass
  end;
end;
 
 
 
function TForm1.GetData: String;
var
  Client: TFPHTTPClient;
  resp_code:integer;
  resp_text:string;
begin
  try
    Client := TFPHTTPClient.Create(nil);
 
    // die notwendigen dll sind vom Laptop-Verzeichnis C:\ProgramData\WebEx\WebEx\T30_MC
    // libeay32.dll
    // ssleay32.dll
 
    Result := Client.Get('http://jsonplaceholder.typicode.com/todos');
    //Result := Client.Get('https://jsonplaceholder.typicode.com/todos');
   //Result := Client.Get(Edit1.Text);
 
  finally
    resp_code:=client.ResponseStatusCode;
    resp_text:=client.ResponseStatusText;
    //showmessage('Code '+inttostr(resp_code)+' -- '+resp_text);
 
    FreeAndNil(Client);
  end;
end;
 
function TForm1.ParseJson(const Json: String): TJSONArray;
begin
  //with TJSONParser.Create(Json, [joUTF8]) do begin
  with TJSONParser.Create(Json, true) do begin
    Result := Parse as TJSONArray;
    Free;
  end;
end;
 
end.

sstvmaster
Beiträge: 575
Registriert: Sa 22. Okt 2016, 23:12
OS, Lazarus, FPC: W10, L 2.2.6
CPU-Target: 32+64bit
Wohnort: Dresden

Re: Beispiel RestAPI über Proxy

Beitrag von sstvmaster »

schau mal hier gibt es glaub ich einen Hinweis dazu: http://forum.lazarus.freepascal.org/ind ... ic=42111.0
LG Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

catweasel
Beiträge: 230
Registriert: Di 17. Mär 2009, 10:51
OS, Lazarus, FPC: Win10 64Bit // Linux Mint 20.0 - (L 2.2.0 FPC 3.2.2)

Re: Beispiel RestAPI über Proxy

Beitrag von catweasel »

sstvmaster hat geschrieben:schau mal hier gibt es glaub ich einen Hinweis dazu: http://forum.lazarus.freepascal.org/ind ... ic=42111.0

Das hat geholfen, Danke :)


Ein Problem habe ich noch bestimmte Daten auszulesen, nämlich genau die welche hier unter installPassword verschachtelt sind (0..5):

Code: Alles auswählen

4:   
  id:                "Router-1234"
  custId:            101
  communicationTyp:  "DSL/LTE"
  state:             "Betrieb"
  approvalDate:      "2016-04-14T00:00:00.000Z"
  routerTyp:         "RS353jv-4G"
  installPassword:   
    type:            "Buffer"
      data:   
        0:            103
        1:            101
        2:            104
        3:            101
        4:            105
        5:            109
  wanIpAdress:        "11.22.33.44"
  wanIpAdressBackup:  "55.66.77.88"


Die ersten Einträge lese ich mit folgender Zeile aus und schreibe sie in ein ListView:

Code: Alles auswählen

ListView1.Items.Item[i].SubItems.Add(o.Strings['custId']);

Wie komme ich an die verschaltelten Daten unter installPassword heran?



Code: Alles auswählen

  DataString := GetData;
  try
    ListView1.BeginUpdate;
    ListView1.Clear;
    Data := ParseJson(DataString);
 
    for i := 0 to (Data.Count - 1) do begin
      o := Data.Objects[i];
      ListView1.Items.Add;
 
      // Für ROM
      ListView1.Items.Item[i].Caption:=o.Strings['id'];
      ListView1.Items.Item[i].SubItems.Add(o.Strings['custId']);
      ListView1.Items.Item[i].SubItems.Add(o.Strings['communicationTyp']);
      ListView1.Items.Item[i].SubItems.Add(o.Strings['state']);
      ListView1.Items.Item[i].SubItems.Add(o.Strings['approvalDate']);
      ListView1.Items.Item[i].SubItems.Add(o.Strings['routerTyp']);
 
     // ListView1.Items.Item[i].SubItems.Add(o.Strings['installPassword:type:data:0']);  // Unter installPassword
 
 
      ListView1.Items.Item[i].SubItems.Add(o.Strings['wanIpAdress']);
      ListView1.Items.Item[i].SubItems.Add(o.Strings['wanIpAdressBackup']);

Antworten