cocos2d-x HttpClient

版本:cocos2d-x2.2node

聯網遊戲開發常常遇到http編程,寫一個示例,加深記憶。c++

想要在cocos2d-x中使用HttpClient須要引用cocos2d-x 的相關庫。web

右鍵單擊項目->屬性->c/c++->常規,在右邊的附件包含目錄中添加cocos2dx的extensions目錄對應的路徑。編程

而後,右鍵單擊項目->屬性->連接器->輸入,在右邊的附件依賴項中添加libcurl_imp.lib和libExtensions.lib兩個庫服務器

代碼部分.hcurl

#pragma once
#include "cocos2d.h"
#include "cocos-ext.h"
#include "spine\Json.h"
USING_NS_CC;
USING_NS_CC_EXT;
class HttpGet : public CCLayer
{
public:
    HttpGet(void);
    ~HttpGet(void);
    void getClicked();
    static CCScene*scene();
    virtual bool init();
    void onGetFinished(CCNode*node,void *data);
    CREATE_FUNC(HttpGet);
};

.cpp函數

#include "HttpGet.h"


HttpGet::HttpGet(void)
{
}


HttpGet::~HttpGet(void)
{
}
CCScene*HttpGet::scene(){
    CCScene*scene=NULL;
    do{
        scene=CCScene::create();
        CC_BREAK_IF(!scene);
        HttpGet*pLayer=HttpGet::create();
        CC_BREAK_IF(!pLayer);
        scene->addChild(pLayer);
    }while(0);
    return scene;
};
bool HttpGet::init(){
    bool bRet=false;
    do{
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        CCLayerColor*color=CCLayerColor::create(ccc4(100,100,100,20));
        this->addChild(color);
        this->getClicked();
      bRet=true;
    }while(0);
    return bRet;
};
void HttpGet::getClicked(){
    CCHttpRequest*request=new CCHttpRequest();//建立對象
    char getData[] = "email=nubb1234@126.com&pwd=623456&nicheng=welcome2";
    char url[100] ="http://i.nubb.com/mobile/adduser.htm?";
    strcat(url,getData);
    request->setUrl(url);//添加請求地址
    request->setRequestType(CCHttpRequest::kHttpGet);//設置請求類型
    request->setResponseCallback(this,callfuncND_selector(HttpGet::onGetFinished));//請求完的調用函數  
    //request->setRequestData(getData,strlen(getData));
    CCHttpClient::getInstance()->send(request);//發送請求
    request->release();
};
void HttpGet::onGetFinished(CCNode*node,void *data){
    AllocConsole();  
    freopen("CONIN$", "r", stdin);  
    freopen("CONOUT$", "w", stdout);  
    freopen("CONOUT$", "w", stderr); 
    CCHttpResponse*res=(CCHttpResponse*)data;
    if(!res){
      return;
    }
    int statusCode = res->getResponseCode();  
      CCLog("response code: %d", statusCode);  
  
  
   char statusString[64] = {};  
    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, res->getHttpRequest()->getTag());  
    CCLog(statusString);  
  
     if (!res->isSucceed())   
    {  
       CCLog("response failed");  
        CCLog("error buffer: %s", res->getErrorBuffer());  
        return;  
    }  
    std::vector<char> *buffer = res->getResponseData();  
    printf("Http Test, dump data: ");  
    std::string rest; 
    for (unsigned int i = 0; i < buffer->size(); i++)  
    {  
        rest+=(*buffer)[i];
        printf("%c", (*buffer)[i]);//這裏打印從服務器返回的數據  
          
    }  
    rest+='\0';
    Json*root=Json_create(rest.c_str());
    Json*_result=Json_getItem(root,"Result");
    if(_result->type==Json_String){
        const char*result=_result->valuestring;
        CCLOG("recult : %s",result);
    }
    Json*_resulrStr=Json_getItem(root,"ResultStr");
    if(_resulrStr->type==Json_String){
        const char*resultStr=_resulrStr->valuestring;
        CCLOG("resultStr : %s",resultStr);
}
    printf("\n"); 
    Json_dispose(root);
};

2.1.3版本到這裏已經能運行了,可是2.2版本還要加一個庫this

pthreadVCE2.liburl

相關文章
相關標籤/搜索