cocos2d-x lua 使用http(下载图片, POST JSON)

version: cocos2d-x 3.6

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下载网络图片

-- get (image)

pSprite:retain()  -- a sprite
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()
  • 在使用xhr时,可以添加自己的数据(如:xhr._urlSprite = pSprite),存放用户的临时数据,方便返回时使用。

cocos2d-x lua 使用http(下载图片, POST JSON)的更多相关文章

  1. cocos2dx-lua http请求下载图片,使用XMLHttpRequest类

    HttpFileDownLoadSimple.lua local downloader = {} --数据拆分,以没1024*5字节拆成一段,打包写入文件 (拆完再拼接,转成字符串) local fu ...

  2. C++根据图片url下载图片

    需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...

  3. .net 已知图片的网络路径,通过浏览器下载图片

    没什么技术含量,主要留给自己查找方便: 如题,知道图片的完整网络路径的情况下,在浏览器中下载图片的实现: 下面这个方法实现的是把图片读取为byte数组: private byte[] GetImage ...

  4. (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

    想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...

  5. Android 本地/网路下载图片实现放大缩小

     Android 本地加载/网路下载图片实现放大缩小拖拉效果,自定义控件. package com.example.ImageViewCustom; import android.app.Activi ...

  6. 利用node来下载图片到本地

      本文是针对于知道图片地址的下载图片方法. 同时也是我的处男作(额,怪怪的〜);不要在意这些细节. 最近在弄项目迁移,需要把http的链接全换成https的:以前的cms不支持http的协议,然后就 ...

  7. SDWebImage下载图片有时候无法成功显示出来

    之前用下面的方法现在图片,有时候会出现图片没有下载成功显示: - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)p ...

  8. 根据url地址单个或批量下载图片

    我们在java开发的时候会遇到通过url地址下载图片的情况.方便起见,我把通过url地址下载图片封装了tool工具类,方便以后使用 1.根据如:http://abc.com/hotels/a.jpg  ...

  9. 使用HttpURLConnection下载图片

    import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.Ht ...

随机推荐

  1. c++ 概念及学习/c++ concept&learning(三)

    这一篇继续说说程序设计中的基本语句:控制块 一 if类控制语句 if if else if  , else if ,else if(条件语句){如果条件为真,要做的一些事情}  if(条件语句) {如 ...

  2. [算法] 冒泡排序 Bubble Sort

    冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...

  3. 用原生javascript模拟经典FC游戏公路争霸

    #用原生javascript模拟经典FC游戏公路争霸 前几天看了园子里面的随笔 [原生javascript开发仿微信打飞机小游戏](http://www.cnblogs.com/Mr-Nobody/p ...

  4. web.py实现jsonp

    浏览器端请求 $.getJSON("/currenttime?callback=?", function (json){ $("#time").html(jso ...

  5. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

  6. Failed to allocate the network(s), not rescheduling

    Failed to allocate the network(s), not rescheduling 在计算节点的/etc/nova/nova.conf中添加下面两句 #Fail instance ...

  7. 第二百四十七天 how can I 坚持

    今天去了趟北海公园,看到地铁宣传图片挺好看的,实景也倒是不错,环境好了,哪都好,今天是蓝天白云啊. 回来的路上看了,扎克伯格对质疑的回应.哎.改变世界在硅谷是行动,而不是口号.change the w ...

  8. ocp 1Z0-051 23-70题解析

    23. Examine thestructure proposed for the TRANSACTIONS table: name Null Type TRANS_ID NOT NULLNUMBER ...

  9. codeforces 651A Joysticks

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  10. BestCoder Round #66 (div.2) hdu5592

    GTW likes math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...