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

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/longolder"
local request = 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, value in pairs(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
function HttpRequestTest:onResponseGet(event)
local request = event.request
if  event.name ~=  "completed"  then -- 當為completed表示正常結束此事件
     print( "request:getErrorCode(), request:getErrorMessage() " , request:getErrorCode(), request:getErrorMessage())
     return 
end
 
local code = request:getResponseStatusCode()
if  code ~= 200 then -- 成功
     print( "code " , code)
     return 
end
 
local strResponse = string.trim(request:getResponseString())
print(strResponse)
end


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

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

404 - 请求的网页不存在;

503 - 服务器暂时不可用;


来源网址:http://blog.csdn.net/longolder/article/details/37884989

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值