Jump to content

Recommended Posts

The documentation demo posted on http://smartmobilestudio.com/documentation/networking/tw3httprequest/works - but not with all JSON files.

 

It works with the file http://date.jsontest.com/but when I try a different JSON file the data is not displayed in the memo.

 

For example, I'm trying to download the file http://api.worldbank.org/countries/all/indicators/SP.POP.TOTL?format=json

from the World Bank open data but the file is not loaded into the memo field.

 

Could it be the JSON format?

Link to post
Share on other sites
  • Administrators

This is related to CORS settings on the server.

 

 

In short: The server won't let you download the JSON unless the request comes from "the same origin" (ie. the same domain).

 

Read more on:

- http://www.html5rocks.com/en/tutorials/cors/

- http://en.wikipedia.org/wiki/Same-origin_policy

- http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

 

You can test the CORS-settings on pages like:

- test-cors.org

Link to post
Share on other sites
  • Administrators

Regarding XML and RSS download, you can basically download anything with the TW3HttpRequest object.

It will simply give you the result of the HTTP request.

 

If the result is an HTML page, you will get the HTML-document, if the result is XML or RSS, then you will get this.

 

It's a lot easier to work with JSON in Smart, though.  You can just assign the JSON-string to a variant variable and immediately get access to all the content.

For XML, you need to parse the XML-structure...

Link to post
Share on other sites
  • Administrators

TW3HttpRequest will give you the sourcecode (http://smartmobilestudio.com/documentation/networking/tw3httprequest/)

Just replace 'http://date.jsontest.com/'with 'http://jsontest.com/' and you will see...

 

If you want to read the content of another DOM element, such as an IFRAME, you need know a little bit about how a web document is managed in the browser, and a little bit about the properties the various HTML elements have.

 

If you head over to http://www.w3schools.com/jsref/dom_obj_document.asp, you will get a good overview of the basics.

As you can see, the HTML-document is represented as an hierarchical tree of node elements.  The document object is the root node of the HTML document and the "owner" of all other nodes.

 

In this list, you'll see that document.documentElement contains the info you are looking for.

 

So, ideally, we would want to write something similar to this code:

//This code does NOT work.  Read on... 
W3Memo1.Text := W3IFrameHTMLElement1.contentDocument.documentElement.Text;

Unfortunately, we don't have all these properties exposed in the current RTL, so you need to use some of the magic features in Smart ;-)

 

Every component on Smart have a direct link to the JavaScript element trough the Handle property.

The Handle property is of THandle type, an alias for the Variant datatype.

 

When you work with Variants, you have direct access to all JavaScript properties.  This is really powerful.

(The downside is that you loose the strict typing and compiler check we love from Pascal).

 

Create a new project.

Add three buttons, an IFrame element and a Memo.

 

In initialize from, you load the page into the IFrame:

procedure TForm1.InitializeForm;
begin
  inherited;
  W3IFrameHTMLElement1.Src := 'http://jsontest.com/';
end;

Double click on the first button, and add these lines into the onclick event:

procedure TForm1.W3Button1Click(Sender: TObject);
var
  v: Variant;
begin
  v := W3IFrameHTMLElement1.Handle.contentDocument;
  W3Memo1.Text := v.title;
end;

The first line grabs the content document from the IFrame element via the Handle property.

The second line shows the "Title" property from the document.

 

Note that the properties you refer to from the variant variable are case sensitive.

 

 

This will show you the title of the web page.

 

 

Further, add this code to the event handler of the second button:

procedure TForm1.W3Button2Click(Sender: TObject);
var
  v: Variant;
begin
  v := W3IFrameHTMLElement1.Handle.contentDocument.documentElement;
  W3Memo1.Text := v.innerHTML;
end;

To get the HTML-code, we actually need to get the "DocumentElement" from the "ContentDocument".

From here we can grab "InnerHTML".  This will show you the whole document as it is maintained by the browser.

 

 

And while we're at it, lets experiment with a couple couple of the other properties we have.

Add this code to the third button:

procedure TForm1.W3Button3Click(Sender: TObject);
var
  v: Variant;
begin
  W3Memo1.Text := '';
  v := W3IFrameHTMLElement1.Handle.contentDocument.links;
  for var i := 0 to Integer(v.length)-1 do
  begin
    W3Memo1.Text := W3Memo1.Text +#13#10 + v[i].href;
  end;
end;

In the first line we clear the memo-field.

In the second line we grab all the links from the document.  This gives us a list of all links.

In the third line we start a for-loop with an inline declaration if the variable i.

Since "v.length" is un-typed at this point, we need to type cast it to integer.  The compiler wont accept anything else here.

In the for-loop, we simply adds the HREF part if the link to the list.

 

Pretty neat...

Link to post
Share on other sites

Somehow the code above doesn't work.  I am using the trial license of the Profession edition version 2.1.2

 

I have tested on Chrome, Opera and IE - the web content loads in the FrameHTML and the third button clears the memo field, but the web content does not load into memo,

 

That's the same memo loading problem I have with http://smartmobilestudio.com/documentation/networking/tw3httprequest/

 

Is it an issue with the trial license that will be resolved in the fully licensed Profession/Enterprise edition?

 

 

here is my code:

unit Form1;

interface

uses 
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, 
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Controls.Button, SmartCL.Controls.Memo, SmartCL.Controls.Elements;

type
  TForm1 = class(TW3Form)
    procedure W3Button4Click(Sender: TObject);
    procedure W3Button3Click(Sender: TObject);
    procedure W3Button2Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

{ TForm1 }

procedure TForm1.W3Button2Click(Sender: TObject);
var
  v: Variant;
begin
  v := W3IFrameHTMLElement1.Handle.contentDocument;
  W3Memo1.Text := v.title;
end;

procedure TForm1.W3Button3Click(Sender: TObject);
var
  v: Variant;
begin
  v := W3IFrameHTMLElement1.Handle.contentDocument.documentElement;
  W3Memo1.Text := v.innerHTML;
end;

procedure TForm1.W3Button4Click(Sender: TObject);
var
  v: Variant;
begin
  W3Memo1.Text := '';
  v := W3IFrameHTMLElement1.Handle.contentDocument.links;
  for var i := 0 to Integer(v.length)-1 do
  begin
    W3Memo1.Text := W3Memo1.Text +#13#10 + v[i].href;
  end;
end;

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  W3IFrameHTMLElement1.Src := 'http://jsontest.com/';
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;
 
procedure TForm1.Resize;
begin
  inherited;
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.
Link to post
Share on other sites
  • Administrators

Strange!

 

Trial shouldn't cause any trouble.  You have full access to everything during the trial period.

 

Does it work in the internal browser?

(The window that pops up when you hit F9).

 

 

I have only tested this is Chromium Embedded (the internal browser) and in Firefox, so this might be some browser compatibility issue.

We will check that.

 

Second question: Which url do you use to view the file?

- localhost

- 127.0.0.1

- LAN IP (10.x.x.x or 192.168.x.x)

- Open index.html file directly

- External IP (i.e. uploaded to a web server)

 

You have the ability to choose network in the F9 window.

Sometimes (especially under virtual machines), we get the wrong default IP from windows.  Thus you can alter this from the dropdown-list.

 

Also remember to hit "Refresh" if the page doesnt load correctly in this window.

Link to post
Share on other sites

The application runs properly in the internal browser and downloads web content into the memo using all three buttons.  In the internal browser I have 4 web server URLs (the default is http://127.0.0.1:8090/index.html). I have tested and the download works OK in the internal browser whichever URL I use.

 

But if I run the index.html a normal (external) browser, there is no download.  I have now tested with Chrome, IE, Safari & Opera - all fail to download.  

 

I have uploaded the www folder into my phone using a HTML5 wrapper, and when I open it doesn't work (behaves like an external browser).

I have also uploaded to a web server and no download into memo.

 

Looks like the script works only within the SMS IDE.

Link to post
Share on other sites

Hi!

 

To some extent yes, but it looks slightly different under HTML5.

If you have a peek at the help section for loading files, you will find the information you need:

http://smartmobilestudio.com/documentation/networking/

 

Loading a text-file is the exact same as loading a JSON file, except that you omit the JSON parsing call.

 

Here is an example:

http://smartmobilestudio.com/documentation/networking/tw3httprequest/

 

I would also urge you to use an array of string rather than a TStringList, because under Smart Pascal this is closer to the generic TList<String> concept than Delphi or FPC arrays. A Smart String Array has all the benefits of typed arrays and lists combined. Although you have to delimit the text into rows yourself.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...