Cocos2d-x 3.2 Lua demo example xmlhttprequesttest (HTTP network request)

Source: Internet
Author: User
Tags addchild
Cocos2d-x 3.2 Lua demo example xmlhttprequesttest (HTTP network request)

This blog introduces xmlhttprequesttest in the demo of Cocos2d-x 3.2lua, which provides two HTTP request methods: Get and post. The returned data types are as follows: 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 form 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 the label to the 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 status text local statusstring = "HTTP Status Code :".. xhr. statustext labelstatuscode: setstring (statusstring) print (xhr. response) end -- Note callback script callback method xhr: registerscripthandler (onreadystatechange) xhr: Send () -- send request labelstatuscode: setstring ("waiting... ") end -- else try to get the 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 -- note the callback script method callback xhr: registerscripthandler (onreadystatechange) xhr: Send () -- send labelstatuscode: setstring ("waiting... ") end -- Future the label of the post test: 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) -- Note the callback menu item 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 byte stream xhr: open ("Post", "http://httpbin.org/post") -- open socket -- call 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 -- note that the callback script method calls back xhr: registerscripthandler (onreadystatechange) xhr: Send () -- send labelstatuscode: setstring ("waiting... ") end -- begin try to use the post Request Method 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, successfully display 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 -- note the callback script method callback xhr: registerscripthandler (onreadystatechange) xhr: Send () -- send request labelstatuscode: setstring ("waiting... ") end -- else try to use the post method to send the JSON tag local labelpostjson = cc. 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) -- 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 end -- note the callback event layer: registerscripthandler (onnodeevent) return layerendfunction xmlhttprequesttestmain () local scene = cc. scene: Create () -- create scenario scene: addchild (xmlhttprequestlayer () -- add layer scene: addchild (createbackmenuitem () -- add the return menu item to view the helper for details. lua file return sceneend


For example:
Test get:

Test post:

Test post binary:


Test post JSON:



Cocos2d-x 3.2 Lua demo example xmlhttprequesttest (HTTP network request)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.