Difference between revisions of "UrlLoader"

Line 3: Line 3:
 
'''Available since:''' Gideros 2012.2.2<br/>
 
'''Available since:''' Gideros 2012.2.2<br/>
 
=== Description ===
 
=== Description ===
<br />
+
<translate><br />
The `UrlLoader` class is used to download data from an URL. It can be used to download (and optionally save) text files, XML files, JSON files, image files or binary files, etc.<br />
+
The [[[UrlLoader]]] class is used to download data from an URL. It can be used to download (and optionally save) text files, XML files, JSON files, image files or binary files, etc.<br />
Downloaded data is delivered at `event.data` field of `Event.COMPLETE` event as string. Lua is eight-bit clean and so strings may contain characters with any numeric value, including embedded zeros. That means that you can store any binary data into a string.<br />
+
Downloaded data is delivered at [[[event.data` field of `Event.COMPLETE]]] event as string. Lua is eight-bit clean and so strings may contain characters with any numeric value, including embedded zeros. That means that you can store any binary data into a string.<br />
  
 
<h3>HTTP Request Methods</h3><br />
 
<h3>HTTP Request Methods</h3><br />
Line 12: Line 12:
 
<br />
 
<br />
 
<ul><br />
 
<ul><br />
<li>`UrlLoader.GET = "get"`</li><br />
+
<li>[[[UrlLoader.GET = "get"]]]</li><br />
<li>`UrlLoader.POST = "post"`</li><br />
+
<li>[[[UrlLoader.POST = "post"]]]</li><br />
<li>`UrlLoader.PUT = "put"`</li><br />
+
<li>[[[UrlLoader.PUT = "put"]]]</li><br />
<li>`UrlLoader.DELETE = "delete"`</li><br />
+
<li>[[[UrlLoader.DELETE = "delete"]]]</li><br />
 
</ul><br />
 
</ul><br />
<br />
+
<br /></translate>
 
=== Examples ===
 
=== Examples ===
 
'''The example below shows downloading an image file from an URL, saving it to the documents folder and displaying it<br />
 
'''The example below shows downloading an image file from an URL, saving it to the documents folder and displaying it<br />
 
on the stage. This example also shows downloading progress and handling errors.<br />
 
on the stage. This example also shows downloading progress and handling errors.<br />
 
<br />'''<br/>
 
<br />'''<br/>
<source lang="lua">local loader = UrlLoader.new(&quot;http://example.com/image.png&quot;)<br />
+
<source lang="lua">local loader = UrlLoader.new(&quot;http://example.com/image.png&quot;)
<br />
+
 
local function onComplete(event)<br />
+
local function onComplete(event)
local out = io.open(&quot;|D|image.png&quot;, &quot;wb&quot;)<br />
+
local out = io.open(&quot;|D|image.png&quot;, &quot;wb&quot;)
out:write(event.data)<br />
+
out:write(event.data)
out:close()<br />
+
out:close()
<br />
+
 
local b = Bitmap.new(Texture.new(&quot;|D|image.png&quot;))<br />
+
local b = Bitmap.new(Texture.new(&quot;|D|image.png&quot;))
stage:addChild(b)<br />
+
stage:addChild(b)
end<br />
+
end
<br />
+
 
local function onError()<br />
+
local function onError()
print(&quot;error&quot;)<br />
+
print(&quot;error&quot;)
end<br />
+
end
<br />
+
 
local function onProgress(event)<br />
+
local function onProgress(event)
print(&quot;progress: &quot; .. event.bytesLoaded .. &quot; of &quot; .. event.bytesTotal)<br />
+
print(&quot;progress: &quot; .. event.bytesLoaded .. &quot; of &quot; .. event.bytesTotal)
end<br />
+
end
<br />
+
 
loader:addEventListener(Event.COMPLETE, onComplete)<br />
+
loader:addEventListener(Event.COMPLETE, onComplete)
loader:addEventListener(Event.ERROR, onError)<br />
+
loader:addEventListener(Event.ERROR, onError)
 
loader:addEventListener(Event.PROGRESS, onProgress)</source>
 
loader:addEventListener(Event.PROGRESS, onProgress)</source>
 
'''Uploading a file'''<br/>
 
'''Uploading a file'''<br/>

Revision as of 14:33, 23 August 2018

Supported platforms: android, ios, mac, pc
Available since: Gideros 2012.2.2

Description


The [[[UrlLoader]]] class is used to download data from an URL. It can be used to download (and optionally save) text files, XML files, JSON files, image files or binary files, etc.
Downloaded data is delivered at [[[event.data` field of `Event.COMPLETE]]] event as string. Lua is eight-bit clean and so strings may contain characters with any numeric value, including embedded zeros. That means that you can store any binary data into a string.

HTTP Request Methods



UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants:


  • [[[UrlLoader.GET = "get"]]]

  • [[[UrlLoader.POST = "post"]]]

  • [[[UrlLoader.PUT = "put"]]]

  • [[[UrlLoader.DELETE = "delete"]]]



Examples

The example below shows downloading an image file from an URL, saving it to the documents folder and displaying it
on the stage. This example also shows downloading progress and handling errors.


local loader = UrlLoader.new(&quot;http://example.com/image.png&quot;)

local function onComplete(event)
	local out = io.open(&quot;|D|image.png&quot;, &quot;wb&quot;)
	out:write(event.data)
	out:close()

	local b = Bitmap.new(Texture.new(&quot;|D|image.png&quot;))
	stage:addChild(b)
end

local function onError()
	print(&quot;error&quot;)
end

local function onProgress(event)
	print(&quot;progress: &quot; .. event.bytesLoaded .. &quot; of &quot; .. event.bytesTotal)
end

loader:addEventListener(Event.COMPLETE, onComplete)
loader:addEventListener(Event.ERROR, onError)
loader:addEventListener(Event.PROGRESS, onProgress)

Uploading a file

local filename = "crate.png"
local file = io.open(filename, "rb")
local contents = file:read( "*a" )
local boundary = "somerndstring"
 
local send = "--"..boundary..
			"\r\nContent-Disposition: form-data; "..
			"name="..filename.."; filename="..filename..
			"\r\nContent-type: image/png"..
			"\r\n\r\n"..contents..
			"\r\n--"..boundary.."--\r\n";
 
local headers = {
	["Content-Type"] = "multipart/form-data; boundary="..boundary,
	["Content-Length"] = #send,
}
 
local loader = UrlLoader.new("http://localhost/gideros.php", UrlLoader.POST, headers, send)
 
loader:addEventListener(Event.COMPLETE, function(e)
	print(e.data)
end)

Methods

UrlLoader.new - creates a new UrlLoader object
UrlLoader:close - terminates the current loading operation
UrlLoader:ignoreSslErrors - Ignores SSL certificate related errors
UrlLoader:load - loads data from the specified URL

Events

Event.COMPLETE
Event.ERROR
Event.PROGRESS

Constants

UrlLoader.DELETE
UrlLoader.GET
UrlLoader.POST
UrlLoader.PUT