//// Created by liangwei on 12-9-10.//// To change the template use AppCode | Preferences | File Templates.//#include "MyHttpClient.h"
#include "CCLuaEngine.h"
voidMyHttpClient::doGet(constchar*url,inthandler){MyHttpClient*myHttpClient=newMyHttpClient();CCHttpRequest*request=newCCHttpRequest();request->setUrl(url);myHttpClient->m_nHandler=handler;request->setRequestType(CCHttpRequest::kHttpGet);request->setResponseCallback(myHttpClient,callfuncND_selector(MyHttpClient::onHttpRequestCompleted));CCHttpClient::getInstance()->send(request);request->release();}voidMyHttpClient::executeFunction(intresponseCode,constchar*data){CCScriptEngineProtocol*engine=CCScriptEngineManager::sharedManager()->getScriptEngine();lua_State*m_state=engine->getLuaState();lua_pushinteger(m_state,responseCode);lua_pushstring(m_state,data);engine->executeFunctionByHandler(this->m_nHandler,2);}voidMyHttpClient::onHttpRequestCompleted(CCNode*sender,void*resp){CCLog("onHttpRequestCompleted");CCHttpResponse*response=(CCHttpResponse*)resp;if(!response){return;}intstatusCode=response->getResponseCode();std::vector*buffer=response->getResponseData();chardata[buffer->size()+1];for(unsignedinti=0;i<buffer->size();i++){data[i]=(*buffer)[i];}data[buffer->size()]='\0';this->executeFunction(statusCode,data);}