HTTP请求实现

把以下代码拷贝到新创建的project中就能看到效果

HelloWorldScene.h

#include "cocos2d.h"
/*记得要引头文件*/
#include "extensions/cocos-ext.h"
#include "network/HttpClient.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace network; class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // a selector callback
void menuCloseCallback(cocos2d::Ref* pSender); void onMenuGetTestClicked(Ref* sender); void onMenuPostBinaryTestClicked(Ref* sender); //Http Response Callback
void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};

HelloWorldScene.cpp

#include "HelloWorldScene.h"

Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create(); // 'layer' is an autorelease object
auto layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto winSize = Director::getInstance()->getWinSize(); const int MARGIN = 40;
const int SPACE = 70; auto label = Label::createWithTTF("Http Request Test", "arial.ttf", 28);
label->setPosition(Point(winSize.width / 2, winSize.height - MARGIN));
addChild(label); auto menuRequest = Menu::create();
menuRequest->setPosition(Point::ZERO);
addChild(menuRequest); //Get
auto labelGet = Label::createWithTTF("Test Get", "arial.ttf", 44);
auto itemGet = MenuItemLabel::create(labelGet, CC_CALLBACK_1(HelloWorld::onMenuGetTestClicked, this));
itemGet->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - SPACE));
menuRequest->addChild(itemGet); auto labelPostBinary = Label::createWithTTF("Test Post Binary", "arial.ttf", 44);
auto itemPostBinary = MenuItemLabel::create(labelPostBinary, CC_CALLBACK_1(HelloWorld::onMenuPostBinaryTestClicked, this));
itemPostBinary->setPosition(Point(winSize.width / 2, winSize.height - MARGIN - 3 * SPACE));
menuRequest->addChild(itemPostBinary); return true;
} void HelloWorld::onMenuGetTestClicked(Ref* sender)
{
HttpRequest* request = new HttpRequest();
request->setUrl("GET请求网址");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestCompleted));
request->setTag("GET test1");
HttpClient::getInstance()->send(request);
request->release();
} void HelloWorld::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
{
HttpRequest* request = new HttpRequest();
request->setUrl("post请求网址");
request->setRequestType(HttpRequest::Type::POST);
request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestCompleted)); const char* postData = "參数 params = Value";
request->setRequestData(postData,strlen(postData) ); request->setTag("POST test1");
HttpClient::getInstance()->send(request);
request->release(); } void HelloWorld::onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response)
{
if (!response) {
return; } if (0 != strlen(response->getHttpRequest()->getTag())) {
log("%s completed",response->getHttpRequest()->getTag());
} long statusCode = response->getResponseCode();
char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %ld, tag = %s",statusCode,response->getHttpRequest()->getTag());
log("response code: %ld",statusCode); if (!response->isSucceed()) {
log("response failed");
log("error buffer: %s",response->getErrorBuffer());
return; } std::vector<char>* buffer = response->getResponseData();
printf("Http Test, dump data: ");
for (unsigned int i = 0 ; i < buffer->size();i++) {
printf("%c",(*buffer)[i]);
}
printf("\n");
}

cocos2d-x3.0 实现HTTP请求GET、POST的更多相关文章

  1. Asp.net 4.0,首次请求目录下的文件时响应很慢

    原文:Asp.net 4.0,首次请求目录下的文件时响应很慢 1. 问题起因2. 尝试过的处理思路3. 解决方法 1. 问题起因 一个从VS2003(.Net Framework 1.1)升级到.ne ...

  2. swift3.0 原生GET请求 POST同理

    swift3.0 原生GET请求  POST同理 func getrequest(){ let url = URL(string: "http://117.135.196.139:" ...

  3. 如何在Cocos2D 1.0 中掩饰一个精灵(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并 ...

  4. 如何在Cocos2D 1.0 中掩饰一个精灵(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 原帖来自Ray Wunderlich写的精彩的文章 How To ...

  5. Cocos2D v2.0至v3.x简洁转换指南(三)

    Cocos2D 3.3中的注意事项 如果你在使用Cocos2D 3.3+(是SpriteBuilder 1.3+的一部分)你将不得不替分别的换所有存在的UITouch和UITouchEvent为CCT ...

  6. Cocos2D v2.0至v3.x简洁转换指南(二)

    触摸处理 我们在稍后将完成Cocos2d 3.0中触摸处理的完整教程.而现在最重要的是知道如何去启用触摸处理在你的CCNode中: self.userInteractionEnabled = TRUE ...

  7. 如何将各种低版本的discuz版本升级到discuz x3.0

    最近在做discuz改版的项目,遇到了很多问题,相信很多拥有discuz论坛的版主,站长和程序猿在升级或改版discuz的过程中遇到过和我一样的问题,所以我开了一个discuz专栏,为大家讲解一下di ...

  8. QPS从0到4000请求每秒,谈达达后台架构演化之路(转载)

    https://blog.csdn.net/czbing308722240/article/details/52350219 QPS从0到4000请求每秒,谈达达后台架构演化之路   达达是全国领先的 ...

  9. Sender IP字段为"0.0.0.0"的ARP请求报文

    今天在研究免费ARP的过程中,抓到了一种Sender IP字段为“0.0.0.0”的ARP请求报文(广播),抓包截图如下: 这让我很疑惑.一个正常的ARP请求不应该只是Target MAC字段为全0吗 ...

  10. cocos2d 2.0和UIKit混合编程, Push CCDirector的时候出现黑屏的天坑

    症状 使用cocos2d 2.0和UIKit混合编程, 有一块用cocos2d编写的小程序, 将CCDirector push到一个UINavigationController里面. 虽然事先在后台初 ...

随机推荐

  1. boost::asio网络传输错误码的一些实验结果(recv error_code)

    错误码很重要,可以由此判断网络连接到底发生了神马事情,从而驱动高层逻辑的行为.只有笼统的错误码判断的网络层是不够规范的,鄙人觉得有些错误码还是需要在网络层就区分开的,特此记录一些当前实验的错误码以及发 ...

  2. 红眼技术博客 » redis连接池红眼技术博客 » redis连接池

    红眼技术博客 » redis连接池 redis连接池

  3. 怎样改动Myeclipse10.7的Servlet模板

    (1)在myeclipse10.0曾经的版本号中咱庄文件夹仅仅有叶仅仅需找到plugins在文件夹下找到: com.genuitec.eclipse.wizards_9.0.0.me201211011 ...

  4. Xamarin.forms 自定义dropdownview控件

    一 基本说明 想用xamarin做个像美团这样的下拉列表进行条件选择的功能,但是但是找了半天好像没有现成的,也没有其他类似的控件可以走走捷径,再则也没有找到popwindow之类的东东,这里只好使用s ...

  5. ExtJs4 笔记(5) Ext.Button 按钮

    id="li2"></li> <li id="li3"></li> </ul> </div> ...

  6. C语言中输入输出重定,freopen()妙用。

    使用的理由(范围):如果输入数据很庞大,需要一次又一次的重新输入和调试时可采用本函数. freopen ()函数: 1.格式 FILE * freopen ( const char * filenam ...

  7. Common lisp菜鸟指南(译)

    Common lisp菜鸟指南(译) Common lisp菜鸟指南(译)

  8. html中的table在android端显示

    转载请注明出处:http://blog.csdn.net/u012338845/article/details/46773245 開始都是用Html.fromHtml(source).来显示html的 ...

  9. RF+Selenium2Library+Sikuli集成环境搭建

    Sikuli是通过截图来编写代码的脚本语言,他是对于Selenium不好处理的一些模态窗口.flash等的利器.废话少说,直接开始安装吧.安装RF+Selenium2Library的环境这里就不说了, ...

  10. Storyboard 经常用法总结-精华版

    1.prepareForSegue: Now we know what the destinationViewController is we can set its data properties. ...