cocos2d-x lua 使用http(下载图片, POST JSON)
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)的更多相关文章
- cocos2dx-lua http请求下载图片,使用XMLHttpRequest类
HttpFileDownLoadSimple.lua local downloader = {} --数据拆分,以没1024*5字节拆成一段,打包写入文件 (拆完再拼接,转成字符串) local fu ...
- C++根据图片url下载图片
需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...
- .net 已知图片的网络路径,通过浏览器下载图片
没什么技术含量,主要留给自己查找方便: 如题,知道图片的完整网络路径的情况下,在浏览器中下载图片的实现: 下面这个方法实现的是把图片读取为byte数组: private byte[] GetImage ...
- (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 ...
- Android 本地/网路下载图片实现放大缩小
Android 本地加载/网路下载图片实现放大缩小拖拉效果,自定义控件. package com.example.ImageViewCustom; import android.app.Activi ...
- 利用node来下载图片到本地
本文是针对于知道图片地址的下载图片方法. 同时也是我的处男作(额,怪怪的〜);不要在意这些细节. 最近在弄项目迁移,需要把http的链接全换成https的:以前的cms不支持http的协议,然后就 ...
- SDWebImage下载图片有时候无法成功显示出来
之前用下面的方法现在图片,有时候会出现图片没有下载成功显示: - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)p ...
- 根据url地址单个或批量下载图片
我们在java开发的时候会遇到通过url地址下载图片的情况.方便起见,我把通过url地址下载图片封装了tool工具类,方便以后使用 1.根据如:http://abc.com/hotels/a.jpg ...
- 使用HttpURLConnection下载图片
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.Ht ...
随机推荐
- 多校6 1001 HDU5793 A Boring Question (推公式 等比数列求和)
题解:http://bestcoder.hdu.edu.cn/blog/ 多校6 HDU5793 A Boring Question // #pragma comment(linker, " ...
- mybatis系列-09-订单商品数据模型
9.1 数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空字段.外键字段 3.数据库级别表与 ...
- A题进行时--浙大PAT 1021-1030
1021: #include<stdio.h> #include<string.h> #include<vector> #include<queue> ...
- windows下安装和配置Weka
Weka是一款免费的,非商业化的,基于java环境下的开源的机器学习以及数据挖掘软件.Weka里含有各种数据挖掘工具:数据预处理,分类与回归,聚类,关联规则和可视化工具. 一.安装weka 我们首先需 ...
- leetcode@ [336] Palindrome Pairs (HashMap)
https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...
- NodeJS:树的序列化
本文也在我的博客edwardesire.com上,欢迎品尝. 接着上周的工作,我们把上周反序列得到的dtree对象输出到JSON,再将其序列化后存入MongoDB. 存入文档 先将上次得到的决策树对象 ...
- CentOS安装XRDP实现远程桌面访问
1.配置环境: yum install gcc pam-devel openssl-devel -y 2.进入指定目录下载并解压xrdp: 先安装 wget; sudo yum -y install ...
- HDU 5510 Bazinga (2015沈阳现场赛,子串判断)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- codeforces 653A Bear and Three Balls
A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- linux 下安装apache 快速教程
最近自学linux,看鸟哥的文章.提到了apache,所以在虚拟机redhat 5下安装了一把, 结合国内外文章写下快速可行的教程: --------------------------------- ...