Total Pageviews

Thursday, April 17, 2014

Network Request

network.request()
ជាប្រភេទហ្វាំងសិនប្រើសំរាប់បង្កើត Asynchronous HTTP or HTTPS ដែល request ទៅកាន់ URL។ សំរាប់ហ្វាំងសិននេះត្រឡប់ handle ដែលនឹងបញ្ចូលទៅកាន់ network.cancel() ក្នុងការធ្វើ cancel the request
ចំនាំះ
មិនអាចប្រតិ្តបត្តិលើ network request អំឡុងពេលដែល system suspend/Exit event។ បន្ទាប់ពី Corona suspends, no call backs will fire។ វាអាចប្រតិបត្តិលើតាមរយះការ Saving the request you wish to make a file នៅលើ system suspend/ Exit event។ ពេលនោះ system resume event សូមពិនិត្យមើលបើសិន ជាវា pending request saved and if there is, execute it។ សំរាប់ Content-Type នៃការ Request default ទៅកាន់ text/plain ហើយបើសិនជា POSTIng form data ដែលត្រូវការ ក្នុងការបញ្ចូល (for more detail check: HTTP POST with custom headers ដូចខាងក្រោម)

Android: ត្រូវបន្ថែម following permission ទៅកាន់ build.settings
settings =
{
   android =
   {
      usesPermissions =
      {
         "android.permission.INTERNET",
      },
   },
}

Syntax: network.request (url, method, listener [, params])

url (តំរូវការ) វាជា String ដែល HTTP request HRL
method (តំរូវការ) វាជា String ដែល HTTP ជា method, តំលៃត្រឹមត្រូវរបស់វាគឺ “GET” (the default), “HEAD”,”PUT”, and “DELETE”
listener (តំរូវការ) វាជា Listener ដែលជាហ្វាំងសិន Invoked at various phases របស់ HTTP Operation វានឹងបញ្ចូលនៅ networkRequest event
សំរាប់ Listener ហ្វាំងសិនរបស់វាអាចទទួល Events តាមរយះ phases ដូចខាងក្រោមះ
“began” វាជា Notification ទីមួយ, វាផ្ទុកនៅទំហំដែលវាយតំលៃ, បើសិនជាដឹង
“progress” an intermediate progress notification
“ended” the final notification, when the request is finished

តាមរយះតំលៃដើម, the listener will only receive “ended” events។ បើសិនជា Params ដំណើរការ​ដោយ “upload” or “download” ពេលនោះ Listener នឹងទទួល “began” and “progress” events
បើសិនជា response body ត្រូវបានដោយផ្ទាល់តាមរយះហ្វាល់ដោយការប្រើប្រាស់ params.response ហើយវានឹង response was successfully ដែលនឹងត្រូវសរសេរទៅកាន់ហ្វាល់ ពេលនោះ event.response នឹងផ្ទុកនៅ table indicating the filename and baseDirectory សំរាប់ហ្វាល់ output។ បើសិនជាសំនូមពរ ត្រូវបានបំពេញ ប៉ុន្តែវានឹងបង្កើតជា error response ពេលនោះ អ្វីដែល Error body នឹងផ្តល់នៅ String event.response ជំនួសវិញ។ this behavior prevents the destination file from being written/overwritten with an error response instead of the desired payload

Params: វាជា table ដែល specifies HTTP request or response processing options, វារួមបញ្ចូលទាំង custom request headers or body
                Params.headers: វាជា table specifying ដែលធ្វើការ request header values ជាមួយនឹង string key
                Params.body: វាជា String ដែល​contain request body, or alternatively, ជា table ដែលផ្ទុកនៅ filename ហើយនឹង optionally baseDirectory សំរាប់ហ្វាល់ដែលផ្ទុកត្រូវបានប្រើសំរាប់ request body
                Params.bodyType: វាជា String ដែល indicating ថាតើ a string request body វាជា “text” ឬក៏ “binary” បើសិនជា params.body គឺជា String បើជា binary​ វាជា table specifying a file
                Params.progress: វាជាតំលៃ String ដែល indicating ប្រភេទរបស់ progress notifications desired, បើសិនជាវាជា “upload” or “download”។ សំរាប់ notification phases រួមបញ្ចូលទាំង “began” and “progress” phase events សំរាប់ desired direction of progress។ តំលៃដើមរបស់វាគឺ nil, indicating ថាវាជា​Only “ended” phase event is desired
                Params.response:​​ វាជា table value indicating ថា response body ត្រូវសរសេរទៅកាន់ហ្វាល់ ហើយ Optionally the baseDirectory សំរាប់ response file។ បើសិនជាតំលៃនេះមិនបានផ្តល់ឪ្យ the response body is provided as a string
                Params.timeout: វាជា time out នៅក្នុង seconds, default is 30 seconds
                params.handleRedirects: វាជា Boolean ដែល ​indicating whether automatic  redirect handling (the default) is desired។ ការបញ្ចូលវាទៅជា false បើសិនជាចង់ទទួល 302 responses and handle them yourself។ វាត្រូវការ certain kinds of login schemes or custom cookies handling

ចំណាំះ បើសិនជា filename table ត្រូវបាន specified នៅក្នុង params.body or in params.response, baseDirectory គឺជា optional constant ជា default ទៅកាន់ system.DocumentsDirectory។ ក្នុងករណីដែល params.response, baseDirectory មិនអាចបញ្ចូលទៅកាន់ system.ResourceDirecttory តាំងពី directory គឺ read only

សូមមើលកូដះ
For more examples, see the /CoronaSDK/SampleCode/Networking/AsynchHTTP sample project in the Corona SDK download.
For examples of canceling a request, see the documentation for network.cancel().

HTTPS/SSL
The following sample code contacts Google's encrypted search over SSL, and prints the response (in this case, the HTML source of the home page) to the Corona terminal:
 local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        else
                print ( "RESPONSE: " .. event.response )
        end
end

-- Access Google over SSL:
network.request( "https://encrypted.google.com", "GET", networkListener )

HTTP POST with custom headers

The following code demonstrates a sending data via HTTP post, specifying custom request headers and request body.
 local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        else
                print ( "RESPONSE: " .. event.response )
        end
end

local headers = {}

headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Accept-Language"] = "en-US"

local body = "color=red&size=small"

local params = {}
params.headers = headers
params.body = body

network.request( "http://127.0.0.1/formhandler.php", "POST", networkListener, params)

Download file with progress updates

The following code demonstrates how to download a file, with progress updates.
 local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        elseif ( event.phase == "began" ) then
                if event.bytesEstimated <= 0 then
                        print( "Download starting, size unknown" )
                else
                        print( "Download starting, estimated size: " .. event.bytesEstimated )
                end
        elseif ( event.phase == "progress" ) then
                if event.bytesEstimated <= 0 then
                        print( "Download progress: " .. event.bytesTransferred )
                else
                        print( "Download progress: " .. event.bytesTransferred .. " of estimated: " .. event.bytesEstimated )
                end
        elseif ( event.phase == "ended" ) then
                print( "Download complete, total bytes transferred: " .. event.bytesTransferred )
        end
end

local params = {}

-- This tells network.request() that we want the 'began' and 'progress' events...
params.progress = "download"

-- This tells network.request() that we want the output to go to a file...
params.response = {
        filename = "corona.jpg",
        baseDirectory = system.DocumentsDirectory
        }

network.request( "https://www.coronalabs.com/image.jpg", "GET", networkListener,  params )

Upload text file

The following code demonstrates an HTTP post, where the request body is text that comes from a file.

Note: This example demonstrates one possible way to upload a file. Different web servers and applications support different methods of file upload. For example, some REST services support upload of a binary file in the body of a PUT request. Many web servers only allow file uploads using multipart/form-encoded (text) request bodies in a POST request. If you are going to attempt file upload, you must first understand the specific mechanism supported by the specific web server or application that you will be working with, and then you must form your request data and choose your request method appropriately.
 local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        else
                print ( "Upload complete!" )
        end
end

local headers = {}

headers["Content-Type"] = "application/json"
headers["X-API-Key"] = "13b6ac91a2"

local params = {}
params.headers = headers

-- This tells network.request() to get the request body from a file...
params.body = {
        filename = "object.json",
        baseDirectory = system.DocumentsDirectory       
        }

network.request( "http://127.0.0.1/restapi.php", "POST", networkListener, params)


No comments:

Post a Comment