轉自:http://blog.csdn.net/sg619262284/article/details/20144087
在使用之前需要設置一些參數:參考:http://blog.csdn.net/wangbin_jxust/article/details/9632771
在完成上面的操作后,還需要在鏈接器的輸入里面添加一個參數pthreadVCE2.lib;
使用CCHttpRequest方法實現:(異步連接)
void HallView::Qudian()
{
//網絡異步連接方法 cocos2d::extension::CCHttpRequest* postRequest=new cocos2d::extension::CCHttpRequest(); postRequest->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost);//設置發送類型 postRequest->setUrl("");//設置網址 postRequest->setResponseCallback(this,callfuncND_selector(HallView::onHttpRequestCompleted));//回調函數,處理接收到的信息 string caozuo=""; CCString *data=CCString::stringWithString(caozuo); postRequest->setRequestData(data->getCString(),data->length());//這里的代碼會接在網絡地址后面,一起發送。 cocos2d::extension::CCHttpClient* httpClient=cocos2d::extension::CCHttpClient::getInstance(); httpClient->setTimeoutForConnect(10);<span style="font-family: Arial, Helvetica, sans-serif;">//設置連接超時時間</span> httpClient->setTimeoutForRead(10);//設置發送超時時間 httpClient->send(postRequest);//設置接收數據類型 postRequest->release();//釋放 }
添加一個回調方法。
void HallView::onHttpRequestCompleted(cocos2d::CCNode *sender ,void *data)
{
cocos2d::extension::CCHttpResponse* response=(cocos2d::extension::CCHttpResponse*)data; if(!response) {CCLOG("Log:response =null,plase check it."); return;} //請求失敗 if(!response->isSucceed()) { this->removeChildByTag(Animate_loading,true); CCDictionary* pDict = CCDictionary::createWithContentsOfFile("chines.xml"); platform::showMsg(((CCString*)pDict->objectForKey("networking"))->getCString()); CCLOG("ERROR BUFFER:%s",response->getErrorBuffer()); return; } int codeIndex=response->getResponseCode(); const char* tag=response->getHttpRequest()->getTag(); //請求成功 std::vector<char>* buffer=response->getResponseData(); std::string temp(buffer->begin(),buffer->end()); CCString* responseData=CCString::create(temp); Json::Reader reader;//json解析 Json::Value value;//表示一個json格式的對象 if(reader.parse(responseData->getCString(),value))//解析出json放到json中區 { //這里就可以對返回來的信息做處理 } }
使用異步連接,程序和聯網的方法將互相不干擾,聯網方法將為一個獨立的線程。
使用CURL方法實現:(同步連接)
第一個方法
需要加入 頭文件#include "curl/curl.h"
void HallView::denglu(){ //登陸游戲 CURL *curl; CURLcode res; string cc; curl=curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, ""); //設置請求的地址 curl_easy_setopt(curl, CURLOPT_POST, true); //設置數據類型 string caozuo=""; curl_easy_setopt(curl, CURLOPT_POSTFIELDS,caozuo.c_str()); //將操作代碼,和連接的網站組合,一起發送! curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,HallView::writehtml); //數據處理回調函數 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cc);//緩沖的內存 curl_easy_setopt(curl,CURLOPT_TIMEOUT_MS,5000); //設置連接超時時間 res=curl_easy_perform(curl); if(res!=CURLE_OK) { CCDictionary* pDict = CCDictionary::createWithContentsOfFile("chines.xml"); string mes=((CCString*)pDict->objectForKey("networking"))->getCString(); platform::showMsg(mes); } curl_easy_cleanup(curl); } else { CCLog("curl is null"); } }
在定義回調函數:這個方法為靜態方法,如果里面要引用其他變量,需要為靜態變量。
size_t HallView::writehtml(uint8_t* ptr,size_t size,size_t number,void *stream) { CCString* a=CCString::createWithFormat("%s",ptr); std::string str1=a->getCString(); Json::Reader reader;//json解析 Json::Value value;//表示一個json格式的對象 if(reader.parse(str1,value))//解析出json放到json中區 { string out=value["gameId"].asString(); gameda->gameId=out; out=value["newIMSI"].asString(); gameda->newIMSI=out; } return size*number;//這里一定要返回實際返回的字節數 }
在.h中定義:
static size_t writehtml(uint8_t* ptr,size_t size,size_t number,void *stream);
使用同步連接,聯網方法的啟動就直接阻塞游戲主進程的運行,直到獲取到返回值為止。
curl_easy_setopt::屬性
curlopt_url//URL地址值
curlopt_writefunction//將得到的數據傳遞相應的函數
curlopt_writeddata//將函數傳遞給相應的第四個參數里
curlopt_header//如果設置為1,可以返回http頭的值;如果設置為非0值,則可以把一個頭包含在輸出中
CURLOPT_TIMEOUT_MS //設置cURL允許執行的最長毫秒數。
curlopt_low_speed_limit//設置一個長整型。控制傳送多少字節
curlopt_cookie//傳遞一個包含httpcookie的頭連接
curlopt_flie//傳送到輸出文件
curlopt_infile//傳送過來的輸出文件
curlopt_writeheader//輸出頭部分
curlopt_proxyuserpwd//傳遞一個形如[username]:[password]格式的字符串去連接http代理
curlopt_postfields//傳遞一個作為httppost操作的所有數據的字符串
curlopt_referer //在http請求中包含一個referer頭的字符串
curlopt_useragent//在http請求中包含一個user-agent 頭的字符串
curlpot_ftpport 傳遞一個包含被ftppost指令使用的IP地址
使用格式curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1L); //第一個參數實例化的curl,第二個數屬性,第三個為屬性值
如果,獲取的返回值是josn格式,我的博客中有方法非常方便提取指定的值。