2013-11-20 106 views
3

我是使用Cococs2d-X進行遊戲開發的新手。我試圖找到一些在我的遊戲中使用HttpClient庫的例子。任何人都可以給我一個例子嗎?Http GET/POST請求在Coccos2d-X中

+0

是否真的很難檢查其網站上的cocos2d-x文檔? http://www.cocos2d-x.org/wiki/How_to_use_CCHttpClient – Morion

+0

當網站上的代碼無法編譯,而且您不知道如何修復時,很難。 – redux

回答

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

錯誤「使用未聲明的標識符'擴展'」 – redux

0

這個代碼是基於cocos2d的的-X

測試

//測試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"); 
} 
+0

錯誤「使用未聲明的標識符CCHttpClient」。建議我更改爲cocos2d :: network :: HttpClient,其錯誤爲「在嵌套名稱說明符中命名的不完整類型cocos2d :: network :: HttpClient」。 – redux

9

對於科科斯-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 
    } 
}