cocos2dx 3.x HttpRequest GET获取数据
.h文件
#include "network/HttpClient.h" //包含头文件// GET 函数void createGetHttp();void getHttp_handshakeResponse(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);
.cpp文件
using namespace network;
// GET通信
void MainMenuLayer::createGetHttp()
{// 生成HttpRequest对象auto request = new HttpRequest();// 设置请求request->setUrl("http://localhost/test.php");// 设置请求方式 GET类型request->setRequestType(HttpRequest::Type::GET);// 设置请求完成后的回调函数request->setResponseCallback(CC_CALLBACK_2(MainMenuLayer::getHttp_handshakeResponse, this));// 设置请求tagrequest->setTag("getHttp_handshake");// 生成HttpClient对象,并且发送请求HttpClient::getInstance()->send(request);// 释放HttpRequest对象request->release();
}void MainMenuLayer::getHttp_handshakeResponse(HttpClient *sender, HttpResponse *response)
{// 使用response->getResponseData(); 获取请求返回的数据std::vector<char> *buffer = response->getResponseData();std::stringstream s;// 输出数据for (unsigned int i = 3; i < buffer->size() - 1; i++){s << (*buffer)[i];}//因为数据库里的数值是floatfloat f = atof( s.str().c_str() );GlobalData::GetSingleton()->SetFwqPercentage( f );
}
posted @ 2016-10-14 16:44 HaoK 阅读( ...) 评论( ...) 编辑 收藏