Difference between revisions of "UrlLoader"
(20 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | '''Supported platforms:''' android | + | <languages /> |
− | '''Available since:''' Gideros 2012.2.2<br/> | + | <!-- 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/> | |
− | < | + | '''<translate>Available since</translate>:''' Gideros 2012.2.2<br/> |
− | + | '''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Object|Object]]<br/> | |
− | |||
− | < | + | === <translate>Description</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. | |
− | UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants: | + | |
− | + | 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. | |
− | <ul | + | |
− | <li> | + | ==== HTTP Request Methods ==== |
− | <li> | + | UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants: |
− | <li> | + | <ul> |
− | <li> | + | <li>''UrlLoader.GET = "get"''</li> |
− | </ul | + | <li>''UrlLoader.POST = "post"''</li> |
− | + | <li>''UrlLoader.PUT = "put"''</li> | |
− | === Examples === | + | <li>''UrlLoader.DELETE = "delete"''</li> |
− | '''The example below shows downloading an image file from an URL, saving it to the documents folder and displaying it | + | </ul> |
− | on the stage. This example also shows downloading progress and handling errors | + | |
− | + | === <translate>Examples</translate> === | |
− | <source lang="lua">local loader = UrlLoader.new( | + | '''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"> | |
− | local function onComplete(event) | + | local loader = UrlLoader.new("http://example.com/image.png") |
− | local out = io.open( | + | |
− | out:write(event.data) | + | local function onComplete(event) |
− | out:close() | + | local out = io.open("|D|image.png", "wb") |
− | + | out:write(event.data) | |
− | local b = Bitmap.new(Texture.new( | + | out:close() |
− | stage:addChild(b) | + | |
− | end | + | local b = Bitmap.new(Texture.new("|D|image.png")) |
− | + | stage:addChild(b) | |
− | local function onError() | + | end |
− | print( | + | |
− | end | + | local function onError() |
− | + | print("error") | |
− | local function onProgress(event) | + | end |
− | print( | + | |
− | end | + | local function onProgress(event) |
− | + | print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal) | |
− | loader:addEventListener(Event.COMPLETE, onComplete) | + | end |
− | loader:addEventListener(Event.ERROR, onError) | + | |
− | loader:addEventListener(Event.PROGRESS, onProgress)</source> | + | loader:addEventListener(Event.COMPLETE, onComplete) |
− | '''Uploading a file''' | + | loader:addEventListener(Event.ERROR, onError) |
− | <source lang="lua">local filename = "crate.png" | + | loader:addEventListener(Event.PROGRESS, onProgress) |
+ | </source> | ||
+ | |||
+ | '''Uploading a file:''' | ||
+ | <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 66: | Line 70: | ||
loader:addEventListener(Event.COMPLETE, function(e) | loader:addEventListener(Event.COMPLETE, function(e) | ||
print(e.data) | print(e.data) | ||
− | end)</source> | + | end) |
+ | </source> | ||
+ | |||
{|- | {|- | ||
− | | style="width: 50%;"| | + | | style="width: 50%; vertical-align:top;"| |
− | === Methods === | + | === <translate>Methods</translate> === |
− | [[UrlLoader.new]] | + | [[Special:MyLanguage/UrlLoader.new|UrlLoader.new]] ''<translate>creates a new UrlLoader object</translate>''<br/><!-- GIDEROSMTD:UrlLoader.new(url,method,headers,body) creates a new UrlLoader object --> |
− | [[UrlLoader:close]] | + | [[Special:MyLanguage/UrlLoader:close|UrlLoader:close]] ''<translate>terminates the current loading operation</translate>''<br/><!-- GIDEROSMTD:UrlLoader:close() terminates the current loading operation --> |
− | [[UrlLoader:ignoreSslErrors]] | + | [[Special:MyLanguage/UrlLoader:ignoreSslErrors|UrlLoader:ignoreSslErrors]] ''<translate>Ignores SSL certificate related errors</translate>''<br/><!-- GIDEROSMTD:UrlLoader:ignoreSslErrors() Ignores SSL certificate related errors --> |
− | [[UrlLoader:load]] | + | [[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%;"| | + | |
− | === Events === | + | | style="width: 50%; vertical-align:top;"| |
− | [[Event.COMPLETE]]<br/> | + | === <translate>Events</translate> === |
− | [[Event.ERROR]]<br/> | + | <!--[[Special:MyLanguage/Event.COMPLETE|Event.COMPLETE]]<br/>--> |
− | [[Event.PROGRESS]]<br/> | + | [[Special:MyLanguage/UrlLoader_Event.COMPLETE|UrlLoader_Event.COMPLETE]]<br/> |
− | === Constants === | + | <!-- GIDEROSEVT:Event.COMPLETE complete--> |
− | [[UrlLoader.DELETE]]<br/> | + | [[Special:MyLanguage/Event.ERROR|Event.ERROR]]<br/> |
− | [[UrlLoader.GET]]<br/> | + | <!-- GIDEROSEVT:Event.ERROR error--> |
− | [[UrlLoader.POST]]<br/> | + | [[Special:MyLanguage/Event.PROGRESS|Event.PROGRESS]]<br/> |
− | [[UrlLoader.PUT]]<br/> | + | <!-- GIDEROSEVT:Event.PROGRESS progress--> |
+ | === <translate>Constants</translate> === | ||
+ | [[Special:MyLanguage/UrlLoader.DELETE|UrlLoader.DELETE]]<br/><!-- GIDEROSCST:UrlLoader.DELETE delete--> | ||
+ | [[Special:MyLanguage/UrlLoader.GET|UrlLoader.GET]]<br/><!-- GIDEROSCST:UrlLoader.GET get--> | ||
+ | [[Special:MyLanguage/UrlLoader.POST|UrlLoader.POST]]<br/><!-- GIDEROSCST:UrlLoader.POST post--> | ||
+ | [[Special:MyLanguage/UrlLoader.PUT|UrlLoader.PUT]]<br/><!-- GIDEROSCST:UrlLoader.PUT put--> | ||
|} | |} | ||
+ | |||
+ | {{GIDEROS IMPORTANT LINKS}} |
Revision as of 21:06, 11 February 2020
Supported platforms:
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)
MethodsUrlLoader.new creates a new UrlLoader object |
EventsUrlLoader_Event.COMPLETE Constants |