Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

TW3HttpRequest

The TW3Httprequest object is a fairly low level object for talking with servers via the HTTP protocol. As expected you can issue commands like GET and POST in order to retrieve or send data. There are also details like ready-state and status properties to keep an eye on while you work with the object. It is also important to note that all connectivity in the browser is a-sync (non blocking), so you must use the events to handle incoming data.

NOTE: It is extremely important to remember that the browser is bound by something called “the same origin policy”. This means that you cant (unless the server allows it) download media from anywhere you like. The request has to be within the same domain as your application comes from. In short, you can only download things from your own server where the smart app is stored. Some servers allow cross domain downloading but there are security issues you must follow when working.

Downloading a html document

Downloading a html document from a website (which of-course can be a php function or a REST method) is very straightforward. In this example I am using a visual application that has a form. So create a new visual project.

2014-08-07_19-31-57

 

 

Add a button and a memo to the form.

2014-08-07_19-38-25

Then add SmartCL.Inet to the uses list, and the following (bold) code in the forms private section:

uses 
  {...}
  SmartCL.Net.Http;

type
  TForm1 = class(TW3Form)
  private
    {$I 'Form1:intf'}
    FHttp: TW3HttpRequest;
    procedure HandleHttpDataReady(Sender:TW3HttpRequest);
    procedure HandleHttpReadyStateChanged(Sender:TW3HttpRequest);
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

Setting up the HTTP request object

Initialize the HTTP object in the InitializeForm method:

 

procedure TForm1.InitializeForm;
begin
  inherited;

  (* Setup the http request object *)
  FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady        := HandleHttpDataReady;
  FHttp.OnReadyStateChange := HandleHttpReadyStateChanged;
end;

Taking care of the TW3HttpRequest events

Hit CTRL + SHIFT + C to automatically create the two new methods we declared.  Then add these lines:

procedure TForm1.HandleHttpDataReady(Sender:TW3HttpRequest);
begin
  (* When the http document is downloaded, display the text in the memo field *)
  W3Memo1.Text := Sender.ResponseText;
end;

procedure TForm1.HandleHttpReadyStateChanged(Sender:TW3HttpRequest);
begin
  (* Just for watching the status codes *)
  if Sender.ReadyState = 4 then
  begin
    WriteLn(Sender.Status);
    WriteLn(Sender.ReadyState);
  end;
end;

Taking care of the button event

Double click the button in the visual designer to create the event method.  Then add this code:

procedure TForm1.W3Button1Click(Sender: TObject);
begin
  (* Getting a test-response from JSONTest.com *)
  FHttp.Get('http://date.jsontest.com/');
end;

The end result is as expected

2014-08-07_20-40-12

 

One more thing

In the Resize method, you can easily scale the components to better fit the layout.

If you, for instance, add these lines, the memo will align to the bottom of your browser/device.  Resize is triggered whenever you resize the browser or rotate the device.

 

procedure TForm1.Resize;
begin
  inherited;

  (* Scale the memo to fit the screen *)
  W3Memo1.Left := 8;
  W3Memo1.Width := Self.Width - 16;
  W3Memo1.Height := Self.Height - W3Memo1.Top - 8;
end;

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS