加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

quick-cocos2d-x CCHttpRequest詳細說明及用法

发布时间:2020-12-14 17:22:24 所属栏目:百科 来源:网络整理
导读:? CCHttpRequest的用法很簡單 1、Get方法,設置URL,響應事件: [html] view plain copy --[[ 設置ulr:"http://blog.csdn.net/longolder" 監聽:self:onResponseGet(event) 方法:GET --]] local url = "http://blog.csdn.net/longolder" local request = n

?CCHttpRequest的用法很簡單

1、Get方法,設置URL,響應事件:

[html] view plain copy
  1. --[[
  2. 設置ulr:"http://blog.csdn.net/longolder"
  3. 監聽:self:onResponseGet(event)
  4. 方法:GET
  5. --]]
  6. localurl="http://blog.csdn.net/longolder"
  7. localrequest=network.createHTTPRequest(function(event)
  8. self:onResponseGet(event)
  9. end,url,"GET")
  10. request:setTimeout(30)
  11. request:start()

默認超時時間為10s,這裡調用setTimeout設置30s超時

2、POST方法,與GET方法相似,只不過要另外添加數據:

copy
    --調用addPOSTValue向Form中添加鍵值對例如:key:value
  1. forkey,valueinpairs(dataInfo)do
  2. request:addPOSTValue(key,value)
  3. end

dataInfo是一個保存要發送數據的table集合

下面是回調方法:

copy
    functionHttpRequestTest:onResponseGet(event)
  1. localrequest=event.request
  2. ifevent.name~="completed"then--當為completed表示正常結束此事件
  3. print("request:getErrorCode(),request:getErrorMessage()",request:getErrorCode(),request:getErrorMessage())
  4. return
  5. end
  6. localcode=request:getResponseStatusCode()
  7. ifcode~=200then--成功
  8. print("code",code)
  9. return
  10. end
  11. localstrResponse=string.trim(request:getResponseString())
  12. print(strResponse)
  13. 常用的狀態碼有以下幾個值:

    200 - 服務器成功返回網頁

    404 - 請求的網頁不存在

    503 - 服務器暫時不可用

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读