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

TW3JSONP

Where the class TW3HttpRequest is more akin to an ordinary http client component under delphi or freepascal, it does come with some limitations. First and foremost the same origin policy and also the fact that it only deals with raw, unformatted data.

The alternative is the TW3JSONP class which is stored in SmartCL.Net.Http unit. It is very important to understand that these two classes utilize wildly different web technologies, and you simply cant switch one for the other. At least not without some control over the responding server.

Where TW3HttpRequest issues a raw http get or post command to your server (or a server that allow cross domain communication), TW3JSONP is all about webservices. So it expects the URL to be a program that executes on the server – and returns data in a very special way: namely as an executable script.

TW3JSONP is actually a bit of a hack (like most javascript leaps of evolution are). What it does is this:

  1. Create a new script tag via code
  2. Point the source for the script tag to the JSONP webservice
  3. Add parameter to the source URL, telling the server what procedure to call

When the server is called, it parses the parameters it has received and generates not data, but rather, a javascript file which will inject the data into your webpage. When the script has been downloaded it will execute, build whatever data structure it needs to build – and then call your event handler with the result.

If that sounds very dangerous then yes, we agree, you should never call a webservice you do not trust, quite simply because you have no control over what the script you get back contains.

How to use it

The class that ships with smart is very easy to use. All in all you have to do only 3 things:

  1. write an event handler that will recieve the data
  2. call the Request method or set the url property
  3. process the data returned
procedure TMainForm.HandleClick(Sender:TObject);
var
  mJSONP: TW3JSONP;
Begin
  mJSONP:=TW3JSONP.Create;
  mJSONP.OnDataReady:=Procedure (sender:TW3JSONP)
    Begin
      writeln(sender.data);
      w3_showmessage('You got data!');
      mJSONP.free;
    end;
  mJSONP.Request('http://services.digg.com/stories/top?'
                 +'appkey=http%3A%2F%2Fmashup.com'
                 +'&type=javascript&callback=');
end;

The above example uses a relatively new pascal feature called anonymous procedures, so it might look a bit alien if you are used to classical event handlers, but at least it shows you how one could go about using it. The procedure above is an event handler for a button click, so add a button to a form and connect the HandleClick to the onClick event.

Processing the data

The data you get back from the above webservice, is a pretty dense array of records. Thankfully smart makes it childs play to work directly with untyped javascript data. But before we can actually work with it – we have to see it. If you have chrome or safari installed on your PC, then run the example there. And remember to activate the developer tools (see tools menu in either browser). I added the writeln call for a reason, namely to push the data into the console window of the debugger — hence you can inspect it:

Easy enough to work with

Easy enough to work with

Now that we know how the data inside the variant we got back from the server is organized, extracting the values is easy enough. Drop a memo component on your form and alter the JSONP event handler to:

procedure TMainForm.HandleClick(Sender:TObject);
var
  mJSONP: TW3JSONP;
Begin
  mJSONP:=TW3JSONP.Create;
  mJSONP.OnDataReady:=Procedure (sender:TW3JSONP)
    Begin
      writeln(sender.data);
      if sender.data.count>0 then
      begin
        w3memo1.text:=sender.data.stories[0].description
      end;
      mJSONP.free;
    end;
  mJSONP.Request('http://services.digg.com/stories/top?'
                 +'appkey=http%3A%2F%2Fmashup.com'
                 +'&type=javascript&callback=');
end;

And voila – run the test again, click the button, and your memo now contains the headline of the first story:

Working with javascript data is very easy with smart

Working with javascript data is very easy with smart

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