Difference between revisions of "UrlLoader"

From GiderosMobile
 
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
<!-- GIDEROSOBJ:UrlLoader -->
 
<!-- GIDEROSOBJ:UrlLoader -->
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
+
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
'''<translate>Available since</translate>:''' Gideros 2012.2.2<br/>
+
'''Available since:''' Gideros 2012.2.2<br/>
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Object|Object]]<br/>
+
'''Inherits from:''' [[Object]]<br/>
=== <translate>Description</translate> ===
 
<translate><br />
 
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.<br />
 
  
<h3>HTTP Request Methods</h3><br />
+
=== Description ===
<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.
UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants:<br />
+
 
<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.
<ul><br />
+
 
<li>''UrlLoader.GET = "get"''</li><br />
+
==== HTTP Request Methods ====
<li>''UrlLoader.POST = "post"''</li><br />
+
UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants:
<li>''UrlLoader.PUT = "put"''</li><br />
+
*''UrlLoader.GET = "get"''
<li>''UrlLoader.DELETE = "delete"''</li><br />
+
*''UrlLoader.POST = "post"''
</ul><br />
+
*''UrlLoader.PUT = "put"''
<br /></translate>
+
*''UrlLoader.DELETE = "delete"''
=== <translate>Examples</translate> ===
+
 
'''The example below shows downloading an image file from an URL, saving it to the documents folder and displaying it<br />
+
==== HTTPS support on Windows ====
on the stage. This example also shows downloading progress and handling errors.<br />
+
To enable https support on windows additional files are required. [https://forum.giderosmobile.com/discussion/comment/66294/#Comment_66294 See this forum post].
<br />'''<br/>
+
 
<source lang="lua">local loader = UrlLoader.new(&quot;http://example.com/image.png&quot;)
+
=== Examples ===
 +
'''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''':
 +
<syntaxhighlight lang="lua">
 +
local loader = UrlLoader.new("http://example.com/image.png")
  
 
local function onComplete(event)
 
local function onComplete(event)
local out = io.open(&quot;|D|image.png&quot;, &quot;wb&quot;)
+
local out = io.open("|D|image.png", "wb")
 
out:write(event.data)
 
out:write(event.data)
 
out:close()
 
out:close()
  
local b = Bitmap.new(Texture.new(&quot;|D|image.png&quot;))
+
local b = Bitmap.new(Texture.new("|D|image.png"))
 
stage:addChild(b)
 
stage:addChild(b)
 
end
 
end
  
 
local function onError()
 
local function onError()
print(&quot;error&quot;)
+
print("error")
 
end
 
end
  
 
local function onProgress(event)
 
local function onProgress(event)
print(&quot;progress: &quot; .. event.bytesLoaded .. &quot; of &quot; .. event.bytesTotal)
+
print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
 
end
 
end
  
 
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)
'''Uploading a file'''<br/>
+
</syntaxhighlight>
<source lang="lua">local filename = "crate.png"
+
 
 +
'''Uploading a file''':
 +
<syntaxhighlight 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 61: Line 63:
 
local headers = {
 
local headers = {
 
["Content-Type"] = "multipart/form-data; boundary="..boundary,
 
["Content-Type"] = "multipart/form-data; boundary="..boundary,
["Content-Length"] = #send,
 
 
}
 
}
 
   
 
   
Line 68: Line 69:
 
loader:addEventListener(Event.COMPLETE, function(e)
 
loader:addEventListener(Event.COMPLETE, function(e)
 
print(e.data)
 
print(e.data)
end)</source>
+
end)
 +
</syntaxhighlight>
 +
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
[[Special:MyLanguage/UrlLoader.new|UrlLoader.new]] ''<translate>creates a new UrlLoader object</translate>''<br/><!-- GIDEROSMTD:UrlLoader.new -->
+
[[UrlLoader.new]] ''creates a new UrlLoader object''<br/><!--GIDEROSMTD:UrlLoader.new(url,method,headers,body) creates a new UrlLoader object-->
[[Special:MyLanguage/UrlLoader:close|UrlLoader:close]] ''<translate>terminates the current loading operation</translate>''<br/><!-- GIDEROSMTD:UrlLoader:close -->
+
[[UrlLoader:close]] ''terminates the current loading operation''<br/><!--GIDEROSMTD:UrlLoader:close() terminates the current loading operation-->
[[Special:MyLanguage/UrlLoader:ignoreSslErrors|UrlLoader:ignoreSslErrors]] ''<translate>Ignores SSL certificate related errors</translate>''<br/><!-- GIDEROSMTD:UrlLoader:ignoreSslErrors -->
+
[[UrlLoader:ignoreSslErrors]] ''ignores SSL certificate related errors''<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 -->
+
[[UrlLoader:load]] ''loads data from the specified URL''<br/><!--GIDEROSMTD:UrlLoader:load(url,method,headers,body) loads data from the specified URL-->
 +
[[UrlLoader:setStreaming]] ''enables streaming mode''<br/><!--GIDEROSMTD:UrlLoader:setStreaming(bool) enables streaming mode-->
 +
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
[[Special:MyLanguage/Event.COMPLETE|Event.COMPLETE]]<br/><!-- GIDEROSEVT:Event.COMPLETE -->
+
[[UrlLoader_Event.COMPLETE]]<br/><!--GIDEROSEVT:Event.COMPLETE complete-->
[[Special:MyLanguage/Event.ERROR|Event.ERROR]]<br/><!-- GIDEROSEVT:Event.ERROR -->
+
[[Event.ERROR]]<br/><!--GIDEROSEVT:Event.ERROR error-->
[[Special:MyLanguage/Event.PROGRESS|Event.PROGRESS]]<br/><!-- GIDEROSEVT:Event.PROGRESS -->
+
[[Event.PROGRESS]]<br/><!--GIDEROSEVT:Event.PROGRESS progress-->
=== <translate>Constants</translate> ===
+
=== Constants ===
[[Special:MyLanguage/UrlLoader.DELETE|UrlLoader.DELETE]]<br/><!-- GIDEROSCST:UrlLoader.DELETE -->
+
[[UrlLoader.DELETE]]<br/><!--GIDEROSCST:UrlLoader.DELETE delete-->
[[Special:MyLanguage/UrlLoader.GET|UrlLoader.GET]]<br/><!-- GIDEROSCST:UrlLoader.GET -->
+
[[UrlLoader.GET]]<br/><!--GIDEROSCST:UrlLoader.GET get-->
[[Special:MyLanguage/UrlLoader.POST|UrlLoader.POST]]<br/><!-- GIDEROSCST:UrlLoader.POST -->
+
[[UrlLoader.POST]]<br/><!--GIDEROSCST:UrlLoader.POST post-->
[[Special:MyLanguage/UrlLoader.PUT|UrlLoader.PUT]]<br/><!-- GIDEROSCST:UrlLoader.PUT -->
+
[[UrlLoader.PUT]]<br/><!--GIDEROSCST:UrlLoader.PUT put-->
 
|}
 
|}
 +
 +
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 22:28, 9 October 2023

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"

HTTPS support on Windows

To enable https support on windows additional files are required. See this forum post.

Examples

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,
}
 
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
UrlLoader:setStreaming enables streaming mode

Events

UrlLoader_Event.COMPLETE
Event.ERROR
Event.PROGRESS

Constants

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