cocos2d网络http用post传输

前端之家收集整理的这篇文章主要介绍了cocos2d网络http用post传输前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

需要加

#include "cocos-ext.h"

USING_NS_CC_EXT;


void HelloWorld::start()
{
    CCLog("%s",__FUNCTION__);
    CCHttpRequest *request=new CCHttpRequest();
    CCEditBox *Box=(CCEditBox *)this->getChildByTag(1);
    CCEditBox *Box2=(CCEditBox *)this->getChildByTag(2);
    request->setUrl(URL);
    request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost);
    CCString *string=CCString::createWithFormat("user_name=%s&user_password=%s",Box->getText(),Box2->getText());
    request->setRequestData(string->getCString(),strlen(string->getCString()));
    request->setResponseCallback(this,httpresponse_selector(HelloWorld::HttpResponse));
    CCHttpClient::getInstance()->send(request);
}
void HelloWorld::HttpResponse(CCHttpClient* client,CCHttpResponse* response)
{
    if (!response->isSucceed()) {
        CCLOG("%s",response->getErrorBuffer());
    }
    else
    {
        std::vector<char>*victor = response->getResponseData();
        char *datastring =new char((*victor).size()+1);
        for (int i=0; i<(*victor).size(); i++) {
            datastring[i]=(*victor)[i];
        }
        datastring[(*victor).size()]='\0';
        CCLOG("%s",datastring);
        
    }
    
    
}
原文链接:https://www.f2er.com/cocos2dx/346153.html

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