Game Development Reference
In-Depth Information
The accelerometer application running in Codea
Downloading Data
In your app, you may want to download data from the Internet while it's running—this might be data,
graphics, or news. The LuaSocket Library is integrated into Codea providing functions to work with
network data and functionality. The
http
class has a
request
function that allows for downloading
the data from the Internet.
function done(theData, status, headers)
data = theData
if status == 200 then
print("YAY!! Done")
print(theData)
else
print("Error downloading the file")
end
end
http.request("
http://www.oz-apps.com
", done)
When we call the
request
function, we pass it the URL of the data we want to download and a
function that is called when the download completes. The function gets three parameters:
theData
,
status
, and the
headers
table. The
status
is the
http
code, which is
200
when it succeeds,
404
when
the page cannot be found,
403
for forbidden, and so on.