LiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCode Working with filesHow do I get the content of a URL on a web server into Revolution?

How do I get the content of a URL on a web server into Revolution?

This lesson will show you how to load the content of a URL into Revolution.

To see how to monitor the amount of data that has been downloaded please see this lesson.

What We Are Going To Do

What We Are Going To Do

This lesson will show you two techniques for loading the content of a URL into Revolution. In this screenshot you see that the content of http://www.google.com (1) has been loaded into a Revolution field object (2).

Using put

Using put

First we will look at how to download a URL using the put command. In particular we will use the put URL form (1). put URL is an easy way to synchronously download a URL into Revolution. You just provide a URL and Revolution will download the URL contents and store them. In this case the contents are being stored in the variable theWebPageContent (2).

The fact that put URL is synchronous means that your revTalk code will not continue executing until the URL has finished downloading. For URLs that take a long time to download your UI will become unresponsive until the download process is complete. Because of this we can check for any errors that may have occurred while downloading the URL right after the put URL line (3).

==========

Copy & Paste

==========

on mouseUp

put the text of field "URL" Into theURL

put URL theURL into theWebPageContent

put the result into theError

if theError is empty then

set the text of field "Result" to theWebPageContent

else

set the text of field "Result" to "An error occurred:" && theError & "."

end if

end mouseUp

Using load

Using load

Now we will look at how to download a URL using the load command (1). The load command requires a little more work than using put URL but has the added benefit of being an asynchronous action.

The fact that load is asynchronous means that your revTalk code will continue executing while the URL is being downloaded. For long download operations your application will remain responsive. When calling an asynchronous command we cannot get the results of the download operation right after making the call. Instead we make use of a feature in Revolution called callbacks. A callback is a message that will be sent when the load operation has finished. In this example we tell Revolution that the message FileIsDoneDownloading should be sent when the url finishes downloading (2).

When the FileIsDoneDownloading message is sent by the engine we can get the contents of the URL. Revolution actually caches the contents of the URL on the computer so that the next time you access the URL the cached data is returned (3). When we no longer need the URL contents any longer we clear the cache by calling unload url (4).

==========

Copy & Paste

==========

on mouseUp

put the text of field "URL" Into theURL

## Start downloading URL asynchronously.

load URL theURL with message "FileIsDoneDownloading"

end mouseUp

on FileIsDoneDownloading pURL, pURLStatus

if pURLStatus is "cached" then

## Revolution has cached URL content. Accessing the url uses

## the cache.

set the text of field "Result" to URL pURL

## Remove URL from cache if you are all done with it.

unload url pURL

else

put libURLErrorData(pURL) into theError

set the text of field "Result" to "An error occurred:" && theError & "."

end if

end FileIsDoneDownloading

2 Comments

student_developer

Hi, I want to use a functionality that loads a google map on the screen. Can you please help me

Thanks

Elanor Buchanan

Hi

Have a look at this lesson

http://lessons.runrev.com/s/lessons/m/4069/l/32356-how-do-i-access-maps-on-ios

This will work on mobile or desktop, on desktop google maps will be opened in your default browser.

I hope that helps.

Elanor

Add your comment

E-Mail me when someone replies to this comment