13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

HTTP communications<br />

The httpResponseStatus ev<strong>en</strong>t (AIR)<br />

In Adobe AIR, the URLStream class dispatches an httpResponseStatus ev<strong>en</strong>t in addition to the httpStatus ev<strong>en</strong>t.<br />

The httpResponseStatus ev<strong>en</strong>t is delivered before any response data. The httpResponseStatus ev<strong>en</strong>t (repres<strong>en</strong>ted<br />

by the HTTPStatusEv<strong>en</strong>t class) includes a responseURL property, which is the URL that the response was returned<br />

from, and a responseHeaders property, which is an array of URLRequestHeader objects repres<strong>en</strong>ting the response<br />

headers that the response returned.<br />

Loading data from external docum<strong>en</strong>ts<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Wh<strong>en</strong> you build dynamic applications, it can be useful to load data from external files or from server-side scripts. This<br />

lets you build dynamic applications without having to edit or recompile your application. For example, if you build a<br />

“tip of the day” application, you can write a server-side script that retrieves a random tip from a database and saves it<br />

to a text file once a day. Th<strong>en</strong> your application can load the cont<strong>en</strong>ts of a static text file instead of querying the database<br />

each time.<br />

The following snippet creates a URLRequest and URLLoader object, which loads the cont<strong>en</strong>ts of an external text file,<br />

params.txt:<br />

var request:URLRequest = new URLRequest("params.txt");<br />

var loader:URLLoader = new URLLoader();<br />

loader.load(request);<br />

By default, if you do not define a request method, Flash Player and Adobe AIR load the cont<strong>en</strong>t using the HTTP GET<br />

method. To s<strong>en</strong>d the request using the POST method, set the request.method property to POST using the static<br />

constant URLRequestMethod.POST, as the following code shows:<br />

var request:URLRequest = new URLRequest("s<strong>en</strong>dfeedback.cfm");<br />

request.method = URLRequestMethod.POST;<br />

The external docum<strong>en</strong>t, params.txt, that is loaded at run time contains the following data:<br />

monthNames=January,February,March,April,May,June,July,August,September,October,November,Dece<br />

mber&dayNames=Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday<br />

The file contains two parameters, monthNames and dayNames. Each parameter contains a comma-separated list that<br />

is parsed as strings. You can split this list into an array using the String.split() method.<br />

Avoid using reserved words or language constructs as variable names in external data files, because doing so makes<br />

reading and debugging your code more difficult.<br />

Once the data has loaded, the complete ev<strong>en</strong>t is dispatched, and the cont<strong>en</strong>ts of the external docum<strong>en</strong>t are available<br />

to use in the URLLoader’s data property, as the following code shows:<br />

function completeHandler(ev<strong>en</strong>t)<br />

{<br />

var loader2 = ev<strong>en</strong>t.target;<br />

air.trace(loader2.data);<br />

}<br />

If the remote docum<strong>en</strong>t contains name-value pairs, you can parse the data using the URLVariables class by passing in<br />

the cont<strong>en</strong>ts of the loaded file, as follows:<br />

Last updated 6/6/2012<br />

815

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!