Difference between revisions of "UrlLoader"

From GiderosMobile
Line 6: Line 6:
  
 
=== <translate>Description</translate> ===
 
=== <translate>Description</translate> ===
<translate>
+
The [[Special:MyLanguage/UrlLoader|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.
The [[Special:MyLanguage/UrlLoader|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 [[Special:MyLanguage/Event.COMPLETE|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.
 
Downloaded data is delivered at ''event.data'' field of [[Special:MyLanguage/Event.COMPLETE|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 ====
 
==== HTTP Request Methods ====
UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants:<br />
+
UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants:
 
<ul>
 
<ul>
 
<li>''UrlLoader.GET = "get"''</li>
 
<li>''UrlLoader.GET = "get"''</li>
Line 18: Line 18:
 
<li>''UrlLoader.DELETE = "delete"''</li>
 
<li>''UrlLoader.DELETE = "delete"''</li>
 
</ul>
 
</ul>
</translate>
 
  
 
=== <translate>Examples</translate> ===
 
=== <translate>Examples</translate> ===
'''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.'''<br/>
+
'''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:'''
 
 
 
<source lang="lua">
 
<source lang="lua">
 
local loader = UrlLoader.new("http://example.com/image.png")
 
local loader = UrlLoader.new("http://example.com/image.png")
Line 45: Line 43:
 
loader:addEventListener(Event.COMPLETE, onComplete)
 
loader:addEventListener(Event.COMPLETE, onComplete)
 
loader:addEventListener(Event.ERROR, onError)
 
loader:addEventListener(Event.ERROR, onError)
loader:addEventListener(Event.PROGRESS, onProgress)</source>
+
loader:addEventListener(Event.PROGRESS, onProgress)
 +
</source>
  
 
+
'''Uploading a file:'''
'''Uploading a file'''<br/>
+
<source lang="lua">
 
+
local filename = "crate.png"
<source lang="lua">local filename = "crate.png"
 
 
local file = io.open(filename, "rb")
 
local file = io.open(filename, "rb")
 
local contents = file:read( "*a" )
 
local contents = file:read( "*a" )
Line 81: Line 79:
 
[[Special:MyLanguage/UrlLoader:ignoreSslErrors|UrlLoader:ignoreSslErrors]] ''<translate>Ignores SSL certificate related errors</translate>''<br/><!-- GIDEROSMTD:UrlLoader:ignoreSslErrors() Ignores SSL certificate related errors -->
 
[[Special:MyLanguage/UrlLoader:ignoreSslErrors|UrlLoader:ignoreSslErrors]] ''<translate>Ignores SSL certificate related errors</translate>''<br/><!-- GIDEROSMTD:UrlLoader:ignoreSslErrors() Ignores SSL certificate related errors -->
 
[[Special:MyLanguage/UrlLoader:load|UrlLoader:load]] ''<translate>loads data from the specified URL</translate>''<br/><!-- GIDEROSMTD:UrlLoader:load(url,method,headers,body) loads data from the specified URL -->
 
[[Special:MyLanguage/UrlLoader:load|UrlLoader:load]] ''<translate>loads data from the specified URL</translate>''<br/><!-- GIDEROSMTD:UrlLoader:load(url,method,headers,body) loads data from the specified URL -->
 +
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 
=== <translate>Events</translate> ===
 
=== <translate>Events</translate> ===
Line 92: Line 91:
 
[[Special:MyLanguage/UrlLoader.PUT|UrlLoader.PUT]]<br/><!-- GIDEROSCST:UrlLoader.PUT put-->
 
[[Special:MyLanguage/UrlLoader.PUT|UrlLoader.PUT]]<br/><!-- GIDEROSCST:UrlLoader.PUT put-->
 
|}
 
|}
 
{{UrlLoader}}
 

Revision as of 01:36, 10 December 2019


Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2012.2.2
Inherits from: Object

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("http://example.com/image.png")

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

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

local function onError()
	print("error")
end

local function onProgress(event)
	print("progress: " .. event.bytesLoaded .. " of " .. 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