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. 基于visual Studio2013解决面试题之0407数组差

     题目

  2. javascript事件委托,事件代理,元素绑定多个事件之练习篇

    <ul id="parent-list"> <li id="post-1">item1</li> <li id=&qu ...

  3. ym——安卓巴士总结了近百个Android优秀开源项

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 1.Android团队提供的演示样例项目 假设不是从学习Android SDK中提供的 ...

  4. C嵌入汇编

    概述:linux内核源码中,有很多C语言中嵌入了汇编语句,如何理解这些汇编语句,对理解内核有很重要的作用. 具有输入和输出参数的嵌入式汇编语句的基本格式为: asm("汇编语句" ...

  5. vim Diff,Easy,Read-Only 的区别

    我用的是vim 7.4,在windows上面安装完 vim 之后会出现不同的vim图标,gVim ,vim gVim 是在windows下的Gui图形用户界面的的 vim (GUI Vim),支持wi ...

  6. objective-C 中的内存管理解说

    初学objectice-C的朋友都有一个困惑,总觉得对objective-C的内存管理机制琢磨不透,程序经常内存泄漏或莫名其妙的崩溃.我在这里总结了自己对objective-C内存管理机制的研究成果和 ...

  7. Swift - UIColor使用自定义的RGB配色

    1,比如rgb 色值为55. 186 .89 那么给UIColor设置里面要除以255 1 UIColor(red: 55/255, green: 186/255, blue: 89/255, alp ...

  8. pc机进入android的shell

    一直都知道自己非常死板,刚刚再一次验证了.. 下载下来android开发必备的工具之后,就按部就班的一步步的来了.没想过这些工具有没有其它用处,更有甚者,在刚開始接触android的时候.居然不知道自 ...

  9. go iota

    package main import ( "fmt" ) const ( a = 'A' b c = iota d ) func main() { fmt.Println(a) ...

  10. c coding style之学习篇

    1. 使用do-while结构去避免潜在的内存泄漏问题. do {     p1 = malloc(10);     if (null == p1)     {         break;     ...