cocos2d-x lua 使用http(下载图片, POST JSON)
2018-03-08 15:46
579 查看
转自:https://www.cnblogs.com/songcf/p/4556780.htmlversion: cocos2d-x 3.10
1.使用http post json与服务器交互
require("src/cocos/cocos2d/json") require("src/cocos/network/NetworkConstants") -- post json local xhr = cc.XMLHttpRequest:new() xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON xhr:setRequestHeader("Content-Type", "application/json") xhr:open("POST", "www.baidu.com") local function loginCallback() print("xhr.readyState is:", xhr.readyState, "xhr.status is: ", xhr.status) if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then local response = xhr.response local output = json.decode(response) -- print table.foreach(output, function(i, v) print (i, v) end) -- success ... else -- fail ... end end xhr:registerScriptHandler(loginCallback) xhr:send(sendJson)
2.使用http get下载网络图片
local pSprite = nil pSprite = cc.Sprite:create("tupian.png") pSprite:setPosition(cc.p(150, 200)) ret:addChild(pSprite)
local xhr = cc.XMLHttpRequest:new() -- tag xhr._urlFileName = urlFileName xhr._urlSprite = pSprite xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING xhr:open("GET", "http://cocos2d-x.org/s/images/img-cocos2dx.jpg") local function onDownloadImage() print("xhr.readyState is:", xhr.readyState, "xhr.status is: ", xhr.status) if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then local fileData = xhr.response local fullFileName = cc.FileUtils:getInstance():getWritablePath() .. "/" .. xhr._urlFileName local file = io.open(fullFileName,"wb") file:write(fileData) file:close() local texture2d = cc.Director:getInstance():getTextureCache():addImage(fullFileName) local pSprite = xhr._urlSprite if texture2d then pSprite:setTexture(texture2d) end pSprite:release() -- be careful end end xhr:registerScriptHandler(onDownloadImage) xhr:send()
相关文章推荐
- cocos2d-x lua 使用http(下载图片, POST JSON)
- Android中如何使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端
- IOS 使用HTTP POST上唇图片和文本信息
- cocos2d-x lua 中使用protobuf并对http进行处理
- COCOS2D-X之CCHttpRequest下载图片Demo
- Android使用HttpPost向服务器发送Json数据
- Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端
- Android中使用Json和Xml与服务器进行通信,使用代码发送Get和Post请求,http请求辅助类
- 使用json方式上传图片时,发现上传成功后,变成下载了json... 或者无法解析到返回的结果集!!!
- 使用HttpWebRequest POST图片等文件,带参数
- Android使用HttpURLConnection下载图片
- IOS 使用 HTTP POST 上传图片和文本信息
- cocos2d-x学习之路(16)--使用libcurl下载图片
- Java和Android Http连接程序:使用java.net.URL 下载服务器图片到客户端
- COCOS2D-X之CCHttpRequest下载图片Demo