博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x3.0 实现HTTP请求GET、POST
阅读量:6426 次
发布时间:2019-06-23

本文共 4342 字,大约阅读时间需要 14 分钟。

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 instancebool 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
* buffer = response->getResponseData(); printf("Http Test, dump data: "); for (unsigned int i = 0 ; i < buffer->size();i++) { printf("%c",(*buffer)[i]); } printf("\n");}

转载地址:http://njyga.baihongyu.com/

你可能感兴趣的文章
《HTML与CSS入门经典(第8版)》——2.6 总结
查看>>
新手指南:在 Ubuntu 和 Fedora 上安装软件包
查看>>
在 CentOS7.0 上搭建 Chroot 的 Bind DNS 服务器
查看>>
大型网站的 HTTPS 实践(二):HTTPS 对性能的影响
查看>>
《Swift 权威指南》——第6章,第6.10节嵌套函数
查看>>
《自己动手做交互系统》——1.3 本章小结
查看>>
Mobile devices bundled with malware?
查看>>
《JavaScript面向对象精要》——1.5 访问属性
查看>>
《Python数据可视化编程实战》—— 第 1 章 准备工作环境
查看>>
Android应用性能优化最佳实践.1.1 Android Studio的优势
查看>>
《设计模式解析(第2版•修订版)》—第2章 2.2节什么是UML
查看>>
【直播】APP全量混淆和瘦身技术揭秘
查看>>
10个大坑,当你产品上架AppStore会遇到
查看>>
【shell 脚本】两种登录方式
查看>>
学习编程的方法
查看>>
升级linux自带的Python
查看>>
百度地图2.0瓦片地址获取(窗口内瓦片)
查看>>
我的友情链接
查看>>
.JDK1.6安装配置后的测试
查看>>
判断闰年的函数
查看>>