【quick-cocos2d-x】 CCHttpRequest用法

前端之家收集整理的这篇文章主要介绍了【quick-cocos2d-x】 CCHttpRequest用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.jb51.cc/article/p-bxigsxci-no.html

CCHttpRequest的用法很��

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

[html] view plaincopyprint?

  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()

	--[[
		�置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,"GET")
	request:setTimeout(30)
	request:start()

默�超����10s,���用setTimeout�置30s超�

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

--�用addPOSTValue向Form中添加�值�例如:key:value

  • forkey,valueinpairs(dataInfo)do

  • request:addPOSTValue(key,value)

  • end

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

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

    下面是回�方法

    functionHttpRequestTest:onResponseGet(event)

  • localrequest=event.request

  • ifevent.name~="completed"then--��completed表示正常�束此事件

  • print("request:getErrorCode(),request:getErrorMessage()",request:getErrorCode(),request:getErrorMessage())

  • return

  • end

  • localcode=request:getResponseStatusCode()

  • ifcode~=200then--成功

  • print("code",code)

  • localstrResponse=string.trim(request:getResponseString())

  • print(strResponse)

  • function HttpRequestTest:onResponseGet(event) local request = event.request if event.name ~= "completed" then -- ��completed表示正常�束此事件 print("request:getErrorCode(),request:getErrorMessage() ",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 - 服�器��不可用

  • 猜你在找的Cocos2d-x相关文章