对cocos2dx lua中http请求的简要封装和使用

使用

[python]  view plain copy
  1.     ----------------------- 创建自定义事件 start  
  2.     local function eventCustomListener1(event)  
  3.         local str = "response: "..event._usedata  
  4.         labelStatusCode:setString(str)  
  5.           
  6.           -- 如果返回的是 json 数据,这里解析          
  7. --        local data =  json.decode(event._usedata)  
  8. --        table.foreach(data,  
  9. --            function(key, var)  
  10. --                print("-----"..key)  
  11. --                table.foreach(var,  
  12. --                    function(a, b)  
  13. --                        print(a.."-"..b)  
  14. --                    end)  
  15. --            end)  
  16.           
  17.     end    
  18.       
  19.     local listener1 = cc.EventListenerCustom:create("customEvent1",eventCustomListener1)  
  20.     cc.Director:getInstance():setNotificationNode(cc.Node:create())  
  21.     local eventDispatcher = cc.Director:getInstance():getNotificationNode():getEventDispatcher()  
  22.     eventDispatcher:addEventListenerWithFixedPriority(listener1, 6)  
  23.   
  24.     -- 将事件分配器赋值到HttpSingleton.eventDispatcher  
  25.     -- 用来在http请求返回的回调函数中使用,因为回调函数是在异步线程中执行,必须用自定义事件更新ui线程数据  
  26.     local tmpHttp = HttpSingleton:getInstance()  
  27.     tmpHttp.eventDispatcher = eventDispatcher  
  28.     ----------------------- 创建自定义事件 end  
  29.   
  30.     local function testSingletonPost(sender)  
  31.       
  32.         local tmp = HttpSingleton:getInstance()  
  33.           
  34.         local function callback(xhr)  
  35.             local event = cc.EventCustom:new("customEvent1")  
  36.             event._usedata = xhr.response  
  37.             eventDispatcher:dispatchEvent(event)  
  38.             print("post callback code = "..xhr.statusText)  
  39.         end  
  40.           
  41.         local type = tmp.POST  
  42.         local url = "http://localhost:8080/b_springmvc/luaTestPost.do"  
  43.         local dataPost = {}  
  44.         dataPost.data = "hello"  
  45.         dataPost.aaa = "world"  
  46.         dataPost.bbb = "yang"  
  47.         tmp:send(type, url, dataPost, callback)  
  48.   
  49.     end  
  50.   
  51.     local menu4 = createMenu("singletonPost",150,50,testSingletonPost)  
  52.     layer:addChild(menu4)  

封装http请求代码

[python]  view plain copy
  1. require "json"  
  2.   
  3. HttpSingleton = {}  
  4. HttpSingleton.__index = HttpSingleton  
  5. HttpSingleton.instance = nil  
  6. HttpSingleton.callback = nil  
  7. HttpSingleton.POST = "POST"  
  8. HttpSingleton.GET = "GET"  
  9.   
  10. function HttpSingleton:new()  
  11.     local self = {}  
  12.     setmetatable(self,HttpSingleton)  
  13.     return self  
  14. end  
  15.   
  16. function HttpSingleton:getInstance()  
  17.     if nil == self.instance then  
  18.         self.instance = self:new()  
  19.     end  
  20.     return self.instance  
  21. end  
  22.   
  23. -- 数据转换,将请求数据由 table 型转换成 string,参数:table  
  24. function HttpSingleton:dataParse(data)  
  25.     if "table" ~= type(data) then  
  26.         print("data is not a table")  
  27.         return nil  
  28.     end  
  29.   
  30.     local tmp = {}  
  31.     for key, value in pairs(data) do  
  32.         table.insert(tmp,key.."="..value)  
  33.     end  
  34.   
  35.     local newData = ""  
  36.     for i=1,#tmp do  
  37.         newData = newData..tostring(tmp[i])  
  38.         if i<#tmp then  
  39.             newData = newData.."&&"  
  40.         end  
  41.     end  
  42.     print("------- name is "..newData)  
  43.     return newData  
  44. end  
  45.   
  46. -- 发送数据,参数:string,string,table  
  47. function HttpSingleton:send(type, url, data, callback)  
  48.     local xhr = cc.XMLHttpRequest:new() --new 一个http request 实例  
  49.     self.callback = callback    --设置需要执行的函数  
  50.       
  51.    local newData = self:dataParse(data)  
  52.    if nil == newData or "" == newData then  
  53.     return   
  54.    end  
  55.       
  56.     -- response回调函数  
  57.     local function responseCallback()  
  58.         print("httpSingleton - "..xhr.response)  
  59.         if nil ~= self.callback then  
  60.             self.callback(xhr)  
  61.         else  
  62.             print("callback is nil")  
  63.         end  
  64.     end  
  65.   
  66.     -- 设置返回值类型及回调函数  
  67.      xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING  
  68.      xhr:registerScriptHandler(responseCallback)  
  69.            
  70.     -- 请求方式判断  
  71.     if self.POST == type then  
  72.         xhr:open(self.POST, url)  
  73.         xhr:registerScriptHandler(responseCallback)  
  74.         xhr:send(newData)  
  75.     elseif self.GET == type then  
  76.         xhr:open(self.GET, url.."?"..newData)  
  77.         xhr:send()  
  78.     else  
  79.         print("ERROR : type only can be \"Post\" or \"GET\"")  
  80.     end  
  81. end  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值