Total Pageviews

Thursday, April 17, 2014

Network Cancel

network.cancel()
វាប្រើសំរាប់ cancel នៅ outstanding network request ដែលបង្កើតឡើងដោយ network.request(), network.upload() or network.download()
Syntax: network.cancel (requestId)
requestId(តំរូវការ) វាជា Object ដែលប្រើសំរាប់ requestId Object ដែលផ្តល់តាមរយះ network.request(), network.upload() or network.download()។ ការ requestId នេះនឹងត្រលប់តាមរយះ each of these functions ហើយវានឹងផ្តល់នៅ networkRequest event
សូមមើលកូដះ

For more examples, see the /CoronaSDK/SampleCode/Networking/AsynchHTTP sample project in the Corona SDK download.
Cancel a request based on user input

The following sample code starts an HTTP request, which can be cancelled by tapping a "cancel" button:
 local function networkListener( event )
    if ( event.isError ) then
        print( "Network error!")
    else
        print( "Request complete" )
    end
end

-- Start the request:
local requestId = network.request( "https://encrypted.google.com", "GET", networkListener )

-- Create a cancel button that can cancel the request:
local cancelButton = display.newImage( "cancelButton.png" )

function cancelButton:tap( event )
    print( "Canceling request via cancel button" )
    network.cancel( requestId )
end

cancelButton:addEventListener( "tap", cancelButton )

Cancel a request inside of a progress listener based on response content

The following sample code starts an image download, and in the initial progress notification determines whether or not to continue based on the files size:
 local function networkListener( event )
    if ( event.isError ) then
            print( "Network error!")
    elseif ( event.phase == "began" ) and ( event.bytesEstimated > 1000000 ) then
        print( "Canceling request, file is too big!" )
        network.cancel( event.requestId )
    end
end

-- Start the image download:
network.download(
    "http://www.coronalabs.com/demo/hello.png",
    "GET",
    networkListener,
    { progress = true },
    "helloCopy.png",
    system.TemporaryDirectory )


No comments:

Post a Comment