Coccos2d-X中的Http GET / POST请求

时间:2013-11-20 10:05:41

标签: cocos2d-x

我是使用Cococs2d-X进行游戏开发的新手。我试图找到一些在我的游戏中使用HttpClient库的例子。有人能告诉我一个例子吗?

3 个答案:

答案 0 :(得分:9)

对于Cocos-2dx 3.3,您需要包含以下标头并使用命名空间声明:

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

USING_NS_CC;
using namespace network;

然后你可以直接创建HttpRequest:

HttpRequest* request = new HttpRequest();
request->setRequestType(HttpRequest::Type::GET);
request->setUrl("http://YOUR_URL");
request->setResponseCallback(CC_CALLBACK_2(HttpClientTest::onHttpRequestCompleted, this));
request->setTag("Get test");
HttpClient::getInstance()->send(request);

request->release();

您的回调函数可以处理响应:

void HttpClientTest::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response) {
    if (response && response->getResponseCode() == 200 && response->getResponseData()) {
        vector<char> *data = response->getResponseData();
        string ret(&(data->front()), data->size());
        CCLOG("%s", ("Response message: " + ret).c_str());
    }
    else {
        CCLOG("%s", ("Error " + to_string(response->getResponseCode()) + " in request").c_str()); //error
    }
}

答案 1 :(得分:1)

这是使用getPost方法

命中网址的基本代码
cocos2d::extension::CCHttpRequest *request=new cocos2d::extension::CCHttpRequest();

request->setUrl("your url");
request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost);
request->setResponseCallback(this, httpresponse_selector(LoadingScene::yourCallBackMethod));

CCString *data = CCString::createWithFormat ("%s",yourData);

request->setRequestData(data->getCString(), data->length());
extension::CCHttpClient::getInstance()->send(request);

你这样定义yourCallBackMethod ......

yourCallBackMethod(cocos2d::extension::CCHttpClient *sender,cocos2d::extension::CCHttpResponse *response){

        std::vector<char> *buffer = response->getResponseData();
}

答案 2 :(得分:0)

此代码基于cocos2d-x测试

// test 1

        CCHttpRequest* request = new CCHttpRequest();
        request->setUrl("http://just-make-this-request-failed.com");
        request->setRequestType(CCHttpRequest::kHttpGet);
        request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
        request->setTag("GET test1");
        CCHttpClient::getInstance()->send(request);
        request->release();



 /*******************************************************************************/  

    void HttpClientTest::onHttpRequestCompleted(CCHttpClient *sender, CCHttpResponse *response)
{
    if (!response)
    {
        return;
    }

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

    int statusCode = response->getResponseCode()

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

    // dump data
    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");
}