cocos-x 3.5 网络发送数据 post

前端之家收集整理的这篇文章主要介绍了cocos-x 3.5 网络发送数据 post前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

根据demo 剥离出来独立显示的 实例

SendHttp.h 的内容

#pragma once
#include "cocos2d.h"
#include "network/HttpClient.h"

USING_NS_CC;
using namespace cocos2d::network;

class SendHttp :public Layer
{
public:
    SendHttp();
    virtual ~SendHttp();
    void MyInit();//初始化
    void onDecrease(Ref * sender);
    void onHttpRequestCompleted(HttpClient *sender,HttpResponse *response);
    //CREATE_FUNC(SendHttp);

private:
    Label * _labelStatusCode;

};

void runHttpClientTest();//在需要的地方调用函数

SendHttp.cpp 的内容

#include "SendHttp.h"




SendHttp::SendHttp()
{
    MyInit();
}

SendHttp::~SendHttp()
{

}

void SendHttp::MyInit()
{
    auto winSize = Director::getInstance()->getWinSize();

    MenuItemFont * SendHttpText = MenuItemFont::create("SendHttpText",CC_CALLBACK_1(SendHttp::onDecrease,this));//点击 字体按钮响应
    SendHttpText->setColor(Color3B(0,200,20));

    Menu * menu = Menu::create(SendHttpText,nullptr);
    menu->setPosition(300,200);
    this->addChild(menu);

    _labelStatusCode = Label::createWithSystemFont("Http Font","April",40);//显示标签的label
    _labelStatusCode->setPosition(winSize.width / 2,winSize.height / 2);
    addChild(_labelStatusCode);
}
void SendHttp::onDecrease(Ref * sender)
{
        HttpRequest* request = new (std::nothrow) HttpRequest();
        request->setUrl("http://httpbin.org/post");
        request->setRequestType(HttpRequest::Type::POST);
        request->setResponseCallback(CC_CALLBACK_2(SendHttp::onHttpRequestCompleted,this));
        const char* postData = "38779355002564963";//内容
        request->setRequestData(postData,strlen(postData));//发送自定义数据
        request->setTag("transid");//标签
        HttpClient::getInstance()->send(request);
        request->release();
}
void SendHttp::onHttpRequestCompleted(HttpClient *sender,HttpResponse *response)
{
    if (!response)
    {
        return;
    }

    // You can get original request type from: response->request->reqType
    if (0 != strlen(response->getHttpRequest()->getTag()))
    {
        log("%s completed",response->getHttpRequest()->getTag());
    }

    long statusCode = response->getResponseCode();//回调状态码
    char statusString[64] = {};//发起post时的标签
    sprintf(statusString,"HTTP Status Code: %ld,tag = %s",statusCode,response->getHttpRequest()->getTag());
    _labelStatusCode->setString(statusString);
    log("response code: %ld",statusCode);

    if (!response->isSucceed())
    {
        log("response Failed");
        log("error buffer: %s",response->getErrorBuffer());
        return;
    }

    // dump data
    std::vector<char> *buffer = response->getResponseData();//回调的内容
    log("Http Test,dump data: ");
    for (unsigned int i = 0; i < buffer->size(); i++)
    {
        log("%c",(*buffer)[i]);
    }
// log("\n");
    if (response->getHttpRequest()->getReferenceCount() != 2)
    {
        log("request ref count not 2,is %d",response->getHttpRequest()->getReferenceCount());
    }
}


void runHttpClientTest()//创建一个新的 sence 来运行
{
    auto scene = Scene::create();
    SendHttp *layer = new (std::nothrow) SendHttp();
    scene->addChild(layer);

    Director::getInstance()->replaceScene(scene);
    layer->release();
}

猜你在找的Cocos2d-x相关文章