• cocos2d-x3.0 实现HTTP请求GET、POST


    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("
    ");
    }
    
    
    
    
    


  • 相关阅读:
    ffmpeg mp4 视频输出为 aac 的命令
    git操作远程分支
    Linux (openSUSE 15.3 ) server ssh 可以使用,sftp无法使用
    小小思考题
    Linux 命令介绍
    2021 年 如何使用VMware 安装 ubuntu 7.04 虚拟机, 配置 apt 源
    Linux 下 命令行 使用浏览器
    oracle.cmd
    Sqlcmd
    vue eslint 配置使用
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5061672.html
Copyright © 2020-2023  润新知