HTTP

This HTTP demonstration is based on the HttpAndJsonp demo in the A Smart Book Demos folder. You need to add inet.txt to Resources. This would be a good way of storing data in a dictionary (as used in Adam Renak's program Crossword) or in a map containing positions of objects to be used in a game.

The output is designed for a smart phone. The screenshots shows the results of pressing the buttons. The tests use an Apache web server installed on a Raspberry Pi.

Response to top button

Response to top button

Response to second button

Response to second button

See below the Pascal code of the main unit and the xml file with the properties of form components (two buttons, two labels and two memos).

unit Form1;

interface

uses 
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, 
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Controls.Button,
  SmartCL.Controls.Label, SmartCL.Controls.Memo, SmartCL.Inet, SmartCL.Layout;

type
  TForm1=class(TW3form)
  private
    {$I 'Form1:intf'}
    FLayout: TLayout;
    FHttp: TW3HttpRequest;
    procedure Download(url: string);
    procedure HandleDataReady(Sender: TW3HttpRequest);
    procedure HandleReadyStateChange(Sender: TW3HttpRequest);
  protected
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  FLayout := Layout.Client(Layout{1}.Margins(20).Spacing(10), [
    Layout{2}.Top(btnGetInetTxt),
    Layout{3}.Top(btnGetResNotHere),
    Layout{4}.Top(lblResult),
    Layout{5}.Top(memoResult),
    Layout{6}.Top(lblLog),
    Layout{7}.Top(memoLog)
  ]);
  btnGetInetTxt.OnClick := procedure (Sender: TObject)
    begin
      Download('res/inet.txt');
    end;
  btnGetResNotHere.OnClick := procedure (Sender: TObject)
    begin
      Download('res/not.here');
    end;
 end;

procedure TForm1.Download(url: string);
begin
  memoLog.Text := '';
  memoResult.Text := '';
  FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady := HandleDataReady;
  FHttp.OnReadyStateChange := HandleReadyStateChange;
  FHttp.Get(url);
end;

procedure TForm1.HandleDataReady(Sender: TW3HttpRequest);
begin
  memoResult.Text := FHttp.ResponseText;
end;

procedure TForm1.HandleReadyStateChange(Sender: TW3HttpRequest);
begin
  if Sender.ReadyState = 4 then
    memoLog.Text := memoLog.Text + 'ReadyState: 4   ' + 'Status: ' + Sender.Status.ToString;
end;

procedure TForm1.Resize;
begin
  inherited;
  FLayout.Resize(Self);
end;

initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);

end. 
   

Code of the Form

<SMART>
  <Form version="2" subversion="1">
    <Created>2015-06-06T19:11:26.573</Created>
    <Modified>2015-06-07T10:01:49.714</Modified>
    <object type="TW3Form">
      <Caption>W3Form</Caption>
      <Width>200</Width>
      <Height>64</Height>
      <Name>Form1</Name>
      <object type="TW3Button">
        <Caption>Get res/inet.txt</Caption>
        <Width>100</Width>
        <Top>24</Top>
        <Left>248</Left>
        <Height>31</Height>
        <Name>btnGetInetTxt</Name>
      </object>
      <object type="TW3Label">
        <Caption>Log</Caption>
        <Width>256</Width>
        <Top>160</Top>
        <Left>32</Left>
        <Height>24</Height>
        <Name>lblLog</Name>
      </object>
      <object type="TW3Label">
        <Caption>Result</Caption>
        <Width>256</Width>
        <Top>160</Top>
        <Left>320</Left>
        <Height>24</Height>
        <Name>lblResult</Name>
      </object>
      <object type="TW3Memo">
        <Width>256</Width>
        <Top>192</Top>
        <Left>32</Left>
        <Height>40</Height>
        <Name>memoLog</Name>
      </object>
      <object type="TW3Memo">
        <Width>256</Width>
        <Top>192</Top>
        <Left>320</Left>
        <Height>200</Height>
        <Name>memoResult</Name>
      </object>
      <object type="TW3Button">
        <Caption>Get res/not.here</Caption>
        <Width>100</Width>
        <Top>96</Top>
        <Left>256</Left>
        <Height>31</Height>
        <Name>btnGetResNotHere</Name>
      </object>
    </object>
  </Form>
</SMART>
Programming - a skill for life!

Storage and retrieval of data using local storage, cookies, HTTP and MySQL