Cocos2d-x3.8.1网络编程(HTTP)
来源:互联网 发布:我的手机淘宝没有社区 编辑:程序博客网 时间:2024/05/27 16:41
//coco2d-x游戏引擎对HTTP协议进行了封装整合
使用前加入头文件,命名空间
#include "cocos2d.h"
#include "network/HttpClient.h"
#include "network/HttpRequest.h"
#include "network/HttpResponse.h"
USING_NS_CC;
using namespace cocos2d::network;
//接下来对一些接口的简单介绍
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
//创建请求对象
auto request=new HttpRequest();
request->setUrl("http://www.liujunliang.com.cn");
request->setTag("get");
//请求方式
//本例以GET请求方式,常用的还有POST等........
request->setRequestType(HttpRequest::Type::GET);
//发送内容
char value[16]="HelloWord";
request->setRequestData(value,strlen(value));
request->setResponseCallback(CC_CALLBACK_2(HelloWorld::result,this));
//创建客户端对象
//使用单例模式目的为了让类的一个对象成为系统中的唯一实例
auto client=HttpClient::getInstance();
client->setTimeoutForConnect(60);
client->setTimeoutForRead(120);
//client2->sendImmediate(postReq);//即时发送即时应答
client->send(request);
request->release();//回收请求
return true;
}
void HelloWorld::result(cocos2d::network::HttpClient* client,cocos2d::network::HttpResponse* response)
{
//返回404:服务器没有内容,400:服务器错误,200:访问正常
log("Code:%d",response->getResponseCode());
//获取请求对象进而获得标签
log("tag:%s",response->getHttpRequest()->getTag());
if (response->isSucceed())
{
std::stringstream stream;//C++字符流
std::vector<char>* buffer=response->getResponseData();
for (int i = 0; i < buffer->size(); i++)
{
stream<<(*buffer)[i];
}
std::string str=stream.str();
log("value:%s",str.c_str());
}
else
{
log("error:%s",response->getErrorBuffer());
}
}
//这是控制台输出窗口的输出内容(VS2012)
//输出的网页部分内容没有截下来
- Cocos2d-x3.8.1网络编程(HTTP)
- Cocos2d-x3.8.1网络编程(WebSocket)
- Cocos2d-x3.8.1网络编程(Socket)
- cocos2d-Lua10网络Http&Socket编程
- Cocos2d-x 之 Http 网络编程
- cocos2d-x3.2与服务端框架Firefly的网络编程(初级网络通讯)
- cocos2d-x3.2与服务端框架Firefly的网络编程(深入网络通讯)
- cocos2d-x3.0 实现HTTP请求GET、POST
- cocos2d-x3.x 实现HTTP请求GET、POST
- cocos2d-x3.0之请求网络(php服务器)
- Cocos2d-x3.8.1加载csb文件
- Cocos2d-x3.8.1暂停与继续游戏
- Cocos2d-x3.8.1解析Json文件
- Cocos2d-x3.8.1制作愤怒的小鸟
- cocos2d-x网络编程
- cocos2d-x网络编程
- Cocos2d-x_HTTP网络编程
- Cocos2d-x 网络编程
- 互联网热门岗位面试指导:后端工程师
- Codeforces Round #374 (Div. 2)D. Maxim and Array(贪心)
- 删除一个字符串中的指定字母,如:字符串 "aca",删除其中的 a 字母。
- 学习的第二天
- HDU 5823 color 状压DP
- Cocos2d-x3.8.1网络编程(HTTP)
- Binary Watch
- 感知哈希算法——找出相似的图片
- twig模板引擎
- 剑指offer:往上到下打印二叉树(java)
- 我的Direct3D之路1:旅途的开始
- java冒泡排序
- 程序设计框架图和框架加载流程
- 数据结构_线段树_基础模板