Cocos2d-x中 发起http请求
bool httpTool::httpRequest(char * tag, char * url, char * requestdata, int requestType)
{
// 请求示例
//CCHttpRequest* request = new CCHttpRequest();
//request->setUrl(url /*"http://httpbin.org/delete"*/);
//request->setRequestType((CCHttpRequest::HttpRequestType)requestType/*CCHttpRequest::kHttpDelete*/);
//request->setResponseCallback(this, httpresponse_selector(CCSceneEx::onHttpRequestCompleted));
//request->setTag(tag/*"DELETE test2"*/);
//CCHttpClient::getInstance()->send(request);
//request->release();
//实际代码
if (tag == 0 || tag[0] == '\0' || url == 0 || url[0] = '\0' || requestType < 0 || requestType > (int)network::HttpRequest::Type::UNKNOWN)
{
return false;
}
//请求的大体思路是
//1.使用network::HttpRequest构造要请求的数据
//2.使用network::HttpClient使用这个作为发射器把数据发射出去
network::HttpRequest * request = new network::HttpRequest();
request->setUrl(url); //设置请求地址
request->setRequestType((network::HttpRequest::Type)requestType); //设置请求类型 ,有GET,POST,PUT,DELETE,UNKNOWN 五钟
if (requestdata != 0 && requestdata[0] != '\0') //如果有请求参数
{
request->setRequestData[requestdata, strlen(requestdata)); //设置请求参数
}
network::ccHttpRequestCallback httpRequestCallback = std::bind(&CCSceneEx::onHttpRequestCompleted, this, std::placeholder::-1, std::placeholder::_2);
request->setResponseCallback(httpRequestCallback); //设置回调函数
request->setTag(tag); //设置标签
network::HttpClient::getInstance()->send(request); //发送请求
request->release(); //释放请求
}
//httpRequest回调函数
void httpTool::onHttpRequestCompleted(network::HttpClient * sender, network::HttpResponse * response)
{
if (!response)
{
return;
}
const char * pTag = response->getHttpRequest()->getTag();
if(0 != strlen(pTag))
{
}
int ret = 0;
int statusCode = response->getResponseCode();
char * buffer = 0;
int bufferSize = 0;
char * errorInfo = "";
if (response->isSucceed())
{
ret = 1;
std::vector
bufferSize = vbuffer->size();
buffer = new char[bufferSize + 1];
for (int i = 0; i < bufferSize; ++i)
{
buffer[i] = (*vbuffer)[i];
}
buffer[bufferSize] = '\0';
}
else
{
ret = 0;
errorInfo = (char *)response->getErrorBuffer();
}
CLuaParam luaParams[6];
luaParams[0] = ret;
luaParams[1] = statusCode;
luaParams[2] = pTag;
luaParams[3] = bufer;
luaParams[4] = bufferSize;
luaParams[5] = errorInfo;
LuaCppData::sharedLuaCppData()->RunLuaFunction("NetUpdate_OnHttpRequestCompleted", luaParams, 6);
if (buffer != 0)
{
delete[] buffer;
相关新闻>>
- cocos2d-x-3.3-018-游戏摇杆joystick源码分享
- cocos2dx背景透明处理
- cocos2d-x 3.2 之 别踩白块(第一篇)
- Cocos2d-x回调函数 Lambda表达式
- Cocos2d-x虚拟摇杆控制精灵上下左右运动----之游戏开发《赵云要
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_2
- Cocos2d-x 自定义按钮类控制精灵攻击----之游戏开发《赵云要格斗
- Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3
- Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走
- 结合cocos2d-x开发配置sublime text
- 发表评论
-
- 最新评论 更多>>
您可能感兴趣的文章
- Cocos2d-x3.0 文件处理
- symbian UI开发小结(一)
- UIViewController的retainCount(Object C)
- cocos2d-x-3.0 alpha1与C++11练习三:飞镖忍者,如何移动精灵及
- Cocos2d-x3.0游戏实例之《别救我》第八篇——TiledMap实现关卡编
- 五 手游开发神器 cocos2d-x editor 之贴图(TexturePacker)
- cocos2d-x在mac下配置安卓开发环境
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之
- cocos2dx V3.2 mac os Xcode6.1 环境配置
- quick-cocos2d-x教程12:实现文本和密码输入界面