【quick-cocos2d-x】 CCHttpRequest用法

http://blog.csdn.net/longolder/article/details/37884989

CCHttpRequest的用法很���

1、Get方法,�O置URL,���事件:

[html] view plaincopyprint?

  1. --[[  

  2.     �O置ulr:"http://blog.csdn.net/longolder"  

  3.     �O�:self:onResponseGet(event)  

  4.     方法:GET  

  5. --]]  

  6. local url = "http://blog.csdn.net/longolder"  

  7. local request = network.createHTTPRequest(function(event)  

  8.     self:onResponseGet(event)  

  9. end, url, "GET")  

  10. request:setTimeout(30)  

  11. request:start()  

	--[[
		�O置ulr:"http://blog.csdn.net/longolder"
		�O�: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()

默�J超�r�r�g��10s,�@�e�{用setTimeout�O置30s超�r

2、POST方法,�cGET方法相似,只不�^要另外添加���:

[html] view plaincopyprint?

  1. -- �{用addPOSTValue向Form中添加�I值��例如:key:value  

  2. for key, value in pairs(dataInfo) do  

  3.     request:addPOSTValue(key, value)  

  4. end  

	-- �{用addPOSTValue向Form中添加�I值��例如:key:value
	for key, value in pairs(dataInfo) do
		request:addPOSTValue(key, value)
	end

dataInfo是一��保存要�l送���的table集合

下面是回�{方法:

[html] view plaincopyprint?

  1. function HttpRequestTest:onResponseGet(event)  

  2.     local request = event.request  

  3.     if event.name ~= "completed" then -- ����completed表示正常�Y束此事件  

  4.         print("request:getErrorCode(), request:getErrorMessage() ", request:getErrorCode(), request:getErrorMessage())  

  5.         return   

  6.     end  

  7.     local code = request:getResponseStatusCode()  

  8.     if code ~= 200 then -- 成功  

  9.         print("code ", code)  

  10.         return   

  11.     end  

  12.     local strResponse = string.trim(request:getResponseString())  

  13.     print(strResponse)  

  14. end  

function HttpRequestTest:onResponseGet(event)
	local request = event.request
	if event.name ~= "completed" then -- ����completed表示正常�Y束此事件
		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

常用的��B�a有以下���值:

200 - 服�掌鞒晒Ψ祷鼐W�

404 - �求的�W�不存在

503 - 服�掌��r不可用

你可能感兴趣的:(http,Quick)