Quick-Cocos2d-x CCHttpRequest详细说明及用法

Quick-Cocos2d-x CCHttpRequest详细说明及用法,第1张

概述CCHttpRequest的用法很简单。 1、Get方法,设置URL,响应事件: 1 2 3 4 5 6 7 8 9 10 11 --[[      设置ulr: "http://blog.csdn.net/longolder"      监听:self:onResponseGet(event)      方法:GET --]] local url =  "http://blog.csdn.net

CChttpRequest的用法很简单。


1、Get方法,设置URL,响应事件:

1 2 3 4 5 6 7 8 9 10 11 --[[ 设置ulr: "http://blog.csdn.net/longolder" 监听:self:onResponseGet(event) 方法:GET --]] localurl= "http://blog.csdn.net/longolder" localrequest=network.createhttpRequest(function(event) self:onResponseGet(event) end,url, "GET" ) request:setTimeout(30) request:start()


默认超时时间为10s,这里调用setTimeout设置30s超时。


2、POST方法,与GET方法相似,只不过要另外添加数据:

1 2 3 4 --调用addPOSTValue向Form中添加键值对例如:key:value for key,valueinpairs(dataInfo) do request:addPOSTValue(key,value) end

dataInfo是一个保存要发送数据的table集合。


下面是回调方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 functionhttpRequestTest:onResponseGet(event) localrequest=event.request if event.name~= "completed" then--當為completed表示正常結束此事件 print( "request:getErrorCode(),request:getErrorMessage()" ,request:getErrorCode(),request:getErrorMessage()) return end localcode=request:getResponseStatusCode() if code~=200then--成功 print( "code" ,code) return end localstrResponse=string.trim(request:getResponseString()) print(strResponse) end


常用的状态代码有以下几个值:

200 - 服务器成功返回网页;

404 - 请求的网页不存在;

503 - 服务器暂时不可用;


来源网址:http://www.jb51.cc/article/p-bxigsxci-no.html

总结

以上是内存溢出为你收集整理的Quick-Cocos2d-x CCHttpRequest详细说明及用法全部内容,希望文章能够帮你解决Quick-Cocos2d-x CCHttpRequest详细说明及用法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存