Cocos2d-x 3.2 Lua example XMLHttpRequestTest (Http network request)
Cocos2d-x 3.2 Lua example XMLHttpRequestTest (Http network request)
This blog introduces XMLHttpRequestTest in the Cocos2d-x 3.2Lua example, which provides two http request methods: GET and POST. The returned data types include:Cc. XMLHTTPREQUEST_RESPONSE_STRING = 0 -- returns the string type.
Cc. XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER = 1 -- returns the byte array type.
Cc. XMLHTTPREQUEST_RESPONSE_BLOB = 2 -- returns the Binary large object type.
Cc. XMLHTTPREQUEST_RESPONSE_DOCUMENT = 3 -- type of the returned Document Object
Cc. XMLHTTPREQUEST_RESPONSE_JSON = 4 -- return the JSON data type.
The definitions of these constants can be found in Cocos2dConstants. lua.
Sample Code:
-- [[========================= XMLHttpRequestTest. luahttp request =======================]] -- require ("json") local function XMLHttpRequestLayer () local layer = cc. layer: create () -- create Layer local winSize = cc. director: getInstance (): getWinSize () -- Obtain the window size local margin = 40 -- spacing local space = 35 -- width local function init () local label = cc. label: createWithTTF ("XML Http Request Test", s_arialPath, 28) -- use the label Label: setAnchorPoint (cc. p (0.5, 0.5) -- set the anchor label: setPosition (cc. p (winSize. width/2, winSize. height-margin) -- sets the display position. The width is in the middle of the screen. The height is the screen height minus the Spacing layer: addChild (label, 0) -- add tags to layer -- display the return code label local labelStatusCode = cc. label: createWithTTF ("HTTP Status Code", s_markerFeltFontPath, 20) labelStatusCode: setAnchorPoint (cc. p (0.5, 0.5) labelStatusCode: setPosition (cc. p (winSize. width/2, winSize. height-margin-6 * space) layer: addChild (labelStatusCode) local menuRequest = cc. menu: create () -- create Menu menuRequest: setPosition (cc. p (0, 0) layer: addChild (menuRequest) -- add menu -- Get local function onMenuGetClicked () local xhr = cc. XMLHttpRequest: new () -- http request xhr. responseType = cc. XMLHTTPREQUEST_RESPONSE_STRING -- response type xhr: open ("GET "," http://httpbin.org/get ") -- Open link -- call local function onReadyStateChange () when the Status changes -- display the Status text local statusString =" Http Status Code :".. xhr. statusText labelStatusCode: setString (statusString) print (xhr. response) end -- register the script callback method xhr: registerScriptHandler (onReadyStateChange) xhr: send () -- send the request labelStatusCode: setString ("waiting... ") end -- test the Get label local labelGet = cc. label: createWithTTF ("Test Get", s_arialPath, 22) labelGet: setAnchorPoint (cc. p (0.5, 0.5) local itemGet = cc. menuItemLabel: create (labelGet) -- menu tag itemGet: registerScriptTapHandler (onMenuGetClicked) -- menu Click Event itemGet: setPosition (cc. p (winSize. width/2, winSize. height-margin-space) menuRequest: addChild (itemGet) -- add menu item -- Post local function onMenuPostClicked () local xhr = cc. XMLHttpRequest: new () -- creates an XMLHttpRequest object xhr. responseType = cc. XMLHTTPREQUEST_RESPONSE_STRING -- the corresponding type is string xhr: open ("POST "," http://httpbin.org/post ") -- Post method local function onReadyStateChange () labelStatusCode: setString (" Http Status Code :".. xhr. statusText) print (xhr. response) end -- register the script method callback xhr: registerScriptHandler (onReadyStateChange) xhr: send () -- send labelStatusCode: setString ("waiting... ") end -- test the Post label local labelPost = cc. label: createWithTTF ("Test Post", s_arialPath, 22) labelPost: setAnchorPoint (cc. p (0.5, 0.5) -- set the local itemPost = cc. menuItemLabel: create (labelPost) -- set the menu item tag itemPost: registerScriptTapHandler (onMenuPostClicked) -- register the menu item and click the callback method itemPost: setPosition (cc. p (winSize. width/2, winSize. height-margin-2 * space) menuRequest: addChild (itemPost) -- Post Binary local function onMenuPostBinaryClicked () local xhr = cc. XMLHttpRequest: new () -- creates an XMLHttpRequest object xhr. responseType = cc. XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER -- the returned data is a byte stream xhr: open ("POST "," http://httpbin.org/post ") -- Open Socket -- call the local function onReadyStateChange () local response = xhr when the status changes. response -- Obtain the returned data. local size = table. getn (response) -- get the returned data size local strInfo = "" for I = 1, size do if 0 = response [I] then strInfo = strInfo .. "\ '\ 0 \'" else strInfo = strInfo .. string. char (response [I]) end labelStatusCode: setString ("Http Status Code :".. xhr. statusText) print (strInfo) end -- register the script method callback xhr: registerScriptHandler (onReadyStateChange) xhr: send () -- send labelStatusCode: setString ("waiting... ") end -- Test the use of the Post request to send the byte stream local labelPostBinary = cc. label: createWithTTF ("Test Post Binary", s_arialPath, 22) labelPostBinary: setAnchorPoint (cc. p (0.5, 0.5) local itemPostBinary = cc. menuItemLabel: create (labelPostBinary) itemPostBinary: registerScriptTapHandler (onMenuPostBinaryClicked) itemPostBinary: setPosition (cc. p (winSize. width/2, winSize. height-margin-3 * space) menuRequest: addChild (itemPostBinary) -- Post Json local function onMenuPostJsonClicked () local xhr = cc. XMLHttpRequest: new () -- creates an XMLHttpRequest object xhr. responseType = cc. XMLHTTPREQUEST_RESPONSE_JSON -- json data type xhr: open ("POST "," http://httpbin.org/post ") -- POST method local function onReadyStateChange () -- display Status Code, 200 labelStatusCode: setString (" Http Status Code :".. xhr. statusText) local response = xhr. response -- Obtain the response data local output = json. decode (response, 1) -- parse json data table. foreach (output, function (I, v) print (I, v) end) print ("headers are") table. foreach (output. headers, print) end -- register the script method callback xhr: registerScriptHandler (onReadyStateChange) xhr: send () -- send request labelStatusCode: setString ("waiting... ") end -- test the label local labelPostJson = cc that uses the POST method to send json. label: createWithTTF ("Test Post Json", s_arialPath, 22) labelPostJson: setAnchorPoint (cc. p (0.5, 0.5) -- The anchor local itemPostJson = cc. menuItemLabel: create (labelPostJson) -- menu item label itemPostJson: registerScriptTapHandler (onMenuPostJsonClicked) -- register a menu item and click itemPostJson: setPosition (cc. p (winSize. width/2, winSize. height-margin-4 * space) menuRequest: addChild (itemPostJson) end -- node callback event local function onNodeEvent (eventName) if "enter" = eventName then init () end -- Registration layer listening callback event layer: registerScriptHandler (onNodeEvent) return layerendfunction XMLHttpRequestTestMain () local scene = cc. scene: create () -- create scenario scene: addChild (XMLHttpRequestLayer () -- add layer scene: addChild (CreateBackMenuItem () -- add a return menu item. For more information, see helper. lua file return sceneend
As follows:
Test Get:
Test PZ environment? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vc3q6pgltzybzcm9" http://www.2cto.com/uploadfile/Collfiles/20140812/2014081208384497.jpg "alt =" \ "/>
Test Post Binary:
Test Post JSON: