- #ifndef __LayerPaihan__
- #define __LayerPaihan__
- #include "cocos2d.h"
- #include "extensions/cocos-ext.h"
- #include "network/HttpClient.h"
- USING_NS_CC;
- USING_NS_CC_EXT;
- using namespace network;
- class LayerPaihan : public cocos2d::LayerColor
- {
- public:
- virtual bool init();
- CREATE_FUNC(LayerPaihan);
- //屏蔽下面的触摸事件
- EventListenerTouchOneByOne * listener;
- bool onTouchBegan(Touch* touch, Event* event);
- void menuClose(cocos2d::Ref* psender);
- void menuCloseCallback(cocos2d::Ref* pSender);
- void onMenuGetTestClicked(Ref* sender);
- void onMenuPostBinaryTestClicked(Ref* sender);
- // Http Response Callback
- void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);
- Sprite* bg;
- };
- #endif
#ifndef __LayerPaihan__
#define __LayerPaihan__
#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "network/HttpClient.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace network;
class LayerPaihan : public cocos2d::LayerColor
{
public:
virtual bool init();
CREATE_FUNC(LayerPaihan);
//屏蔽下面的触摸事件
EventListenerTouchOneByOne * listener;
bool onTouchBegan(Touch* touch, Event* event);
void menuClose(cocos2d::Ref* psender);
void menuCloseCallback(cocos2d::Ref* pSender);
void onMenuGetTestClicked(Ref* sender);
void onMenuPostBinaryTestClicked(Ref* sender);
// Http Response Callback
void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);
Sprite* bg;
};
#endif
.cpp里面的代码
- #include "LayerPaihan.h"
- #include "json\rapidjson.h"
- #include "json\document.h"
- #include "json\stringbuffer.h"
- using namespace rapidjson;
- bool LayerPaihan::init()
- {
- if(!LayerColor::initWithColor(Color4B(0,0,0,128))){
- return false;
- }
- Size visibleSize = Director::getInstance()->getVisibleSize();
- listener = EventListenerTouchOneByOne::create();
- listener->setSwallowTouches(true);
- listener->onTouchBegan = CC_CALLBACK_2(LayerPaihan::onTouchBegan, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
- bg = Sprite::create("ph_bg.jpg");
- bg->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
- this->addChild(bg);
- const int MARGIN = 40;
- const int SPACE = 70;
- auto menuRequest = Menu::create();
- menuRequest->setPosition(Point::ZERO);
- bg->addChild(menuRequest);
- HttpRequest* request = new HttpRequest();
- request->setUrl("http://127.0.0.1:8088/game/index.php");
- request->setRequestType(HttpRequest::Type::GET);
- request->setResponseCallback(this, httpresponse_selector(LayerPaihan::onHttpRequestCompleted));
- request->setTag("GET test1");
- HttpClient::getInstance()->send(request);
- request->release();
- auto close =MenuItemSprite::create(Sprite::createWithSpriteFrameName("x.png"),Sprite::createWithSpriteFrameName("x.png"),CC_CALLBACK_1(LayerPaihan::menuClose,this));
- close->setPosition(Point(bg->getContentSize().width,bg->getContentSize().height));
- auto menu = Menu::create(close, NULL);
- menu->setPosition(Vec2::ZERO);
- bg->addChild(menu,6);
- return true;
- }
- bool LayerPaihan::onTouchBegan(Touch* touch, Event* event){
- return true;
- }
- void LayerPaihan::menuClose(cocos2d::Ref* psender){
- this->setVisible(false);
- listener->setSwallowTouches(false);
- }
- void LayerPaihan::onMenuGetTestClicked(Ref* sender)
- {
- HttpRequest* request = new HttpRequest();
- request->setUrl("http://127.0.0.1:8088/game/index.php");
- request->setRequestType(HttpRequest::Type::GET);
- request->setResponseCallback(this, httpresponse_selector(LayerPaihan::onHttpRequestCompleted));
- request->setTag("GET test1");
- HttpClient::getInstance()->send(request);
- request->release();
- }
- void LayerPaihan::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
- {
- HttpRequest* request = new HttpRequest();
- request->setUrl("http://127.0.0.1:8088/game/index.php");
- request->setRequestType(HttpRequest::Type::POST);
- request->setResponseCallback(this, httpresponse_selector(LayerPaihan::onHttpRequestCompleted));
- const char* postData = "params = Value";
- request->setRequestData(postData,strlen(postData) );
- request->setTag("POST test1");
- HttpClient::getInstance()->send(request);
- request->release();
- }
- void LayerPaihan::onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response)
- {
- if (!response) {
- return;
- }
- if (0 != strlen(response->getHttpRequest()->getTag())) {
- log("%s completed",response->getHttpRequest()->getTag());
- }
- long statusCode = response->getResponseCode();
- char statusString[64] = {};
- if (!response->isSucceed()) {
- log("response failed");
- log("error buffer: %s",response->getErrorBuffer());
- return;
- }
- std::vector<char>* buffer = response->getResponseData();
- std::stringstream oss;
- for(unsigned int i=0;i < buffer->size();i++){
- oss<<(*buffer)[i];
- }
- std::string temp=oss.str();
- const char * buf=temp.c_str();
- rapidjson::Document doc;
- doc.Parse<0>(buf);//<通过Parse方法将Json数据解析出来
- if(doc.HasParseError())
- {
- CCLOG("GetParseError%s\n",doc.GetParseError());
- }
- if(doc.IsArray())///<判断是否是字符串
- {
- for (unsigned int i = 0; i< doc.Size(); ++i) {
- const rapidjson::Value &index = doc[i]["index"];
- const rapidjson::Value &name = doc[i]["name"];
- const rapidjson::Value &gold = doc[i]["gold"];
- auto xs = Label::createWithSystemFont(index.GetString(),"Microsoft YaHei",18);
- xs->setAnchorPoint(Point(0,0));
- xs->setPosition(Point(200,244-i*30));
- bg->addChild(xs);
- auto xs_name = Label::createWithSystemFont(name.GetString(),"Microsoft YaHei",18);
- xs_name->setAnchorPoint(Point(0,0));
- xs_name->setPosition(Point(310,244-i*30));
- bg->addChild(xs_name);
- auto xs_gold = Label::createWithSystemFont(gold.GetString(),"Microsoft YaHei",18);
- xs_gold->setAnchorPoint(Point(0,0));
- xs_gold->setPosition(Point(440,244-i*30));
- bg->addChild(xs_gold);
- }
- }
- }
#include "LayerPaihan.h"
#include "json\rapidjson.h"
#include "json\document.h"
#include "json\stringbuffer.h"
using namespace rapidjson;
bool LayerPaihan::init()
{
if(!LayerColor::initWithColor(Color4B(0,0,0,128))){
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(LayerPaihan::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
bg = Sprite::create("ph_bg.jpg");
bg->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
this->addChild(bg);
const int MARGIN = 40;
const int SPACE = 70;
auto menuRequest = Menu::create();
menuRequest->setPosition(Point::ZERO);
bg->addChild(menuRequest);
HttpRequest* request = new HttpRequest();
request->setUrl("http://127.0.0.1:8088/game/index.php");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(this, httpresponse_selector(LayerPaihan::onHttpRequestCompleted));
request->setTag("GET test1");
HttpClient::getInstance()->send(request);
request->release();
auto close =MenuItemSprite::create(Sprite::createWithSpriteFrameName("x.png"),Sprite::createWithSpriteFrameName("x.png"),CC_CALLBACK_1(LayerPaihan::menuClose,this));
close->setPosition(Point(bg->getContentSize().width,bg->getContentSize().height));
auto menu = Menu::create(close, NULL);
menu->setPosition(Vec2::ZERO);
bg->addChild(menu,6);
return true;
}
bool LayerPaihan::onTouchBegan(Touch* touch, Event* event){
return true;
}
void LayerPaihan::menuClose(cocos2d::Ref* psender){
this->setVisible(false);
listener->setSwallowTouches(false);
}
void LayerPaihan::onMenuGetTestClicked(Ref* sender)
{
HttpRequest* request = new HttpRequest();
request->setUrl("http://127.0.0.1:8088/game/index.php");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(this, httpresponse_selector(LayerPaihan::onHttpRequestCompleted));
request->setTag("GET test1");
HttpClient::getInstance()->send(request);
request->release();
}
void LayerPaihan::onMenuPostBinaryTestClicked(cocos2d::Ref *sender)
{
HttpRequest* request = new HttpRequest();
request->setUrl("http://127.0.0.1:8088/game/index.php");
request->setRequestType(HttpRequest::Type::POST);
request->setResponseCallback(this, httpresponse_selector(LayerPaihan::onHttpRequestCompleted));
const char* postData = "params = Value";
request->setRequestData(postData,strlen(postData) );
request->setTag("POST test1");
HttpClient::getInstance()->send(request);
request->release();
}
void LayerPaihan::onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response)
{
if (!response) {
return;
}
if (0 != strlen(response->getHttpRequest()->getTag())) {
log("%s completed",response->getHttpRequest()->getTag());
}
long statusCode = response->getResponseCode();
char statusString[64] = {};
if (!response->isSucceed()) {
log("response failed");
log("error buffer: %s",response->getErrorBuffer());
return;
}
std::vector<char>* buffer = response->getResponseData();
std::stringstream oss;
for(unsigned int i=0;i < buffer->size();i++){
oss<<(*buffer)[i];
}
std::string temp=oss.str();
const char * buf=temp.c_str();
rapidjson::Document doc;
doc.Parse<0>(buf);//<通过Parse方法将Json数据解析出来
if(doc.HasParseError())
{
CCLOG("GetParseError%s\n",doc.GetParseError());
}
if(doc.IsArray())///<判断是否是字符串
{
for (unsigned int i = 0; i< doc.Size(); ++i) {
const rapidjson::Value &index = doc[i]["index"];
const rapidjson::Value &name = doc[i]["name"];
const rapidjson::Value &gold = doc[i]["gold"];
auto xs = Label::createWithSystemFont(index.GetString(),"Microsoft YaHei",18);
xs->setAnchorPoint(Point(0,0));
xs->setPosition(Point(200,244-i*30));
bg->addChild(xs);
auto xs_name = Label::createWithSystemFont(name.GetString(),"Microsoft YaHei",18);
xs_name->setAnchorPoint(Point(0,0));
xs_name->setPosition(Point(310,244-i*30));
bg->addChild(xs_name);
auto xs_gold = Label::createWithSystemFont(gold.GetString(),"Microsoft YaHei",18);
xs_gold->setAnchorPoint(Point(0,0));
xs_gold->setPosition(Point(440,244-i*30));
bg->addChild(xs_gold);
}
}
}
介绍
HttpClient是HTTP客户端的接口。HttpClient封装了各种对象,处理cookies,身份认证,连接管理等。
概念
HttpClient的使用一般包含下面6个步骤:
- 创建 HttpRequest 的实例;
- 设置某种连接方法的类型(GET、POST等),这里通过setUrl传入待连接的地址;
- 设置响应回调函数,读取response;
- 添加请求到HttpClient任务队列;
- 释放连接。无论执行方法是否成功,都必须释放连接;
- 对得到后的内容进行处理。
如何使用
引入头文件和命名空间
#include "network/HttpClient.h"
using namespace cocos2d::network;
HttpRequest 实例
我们将使用HttpRequest无参数的构造函数,它为大多数情况提供了一个很好的默认设置,所以我们使用它。
HttpRequest* request = new HttpRequest();
设置连接方法的类型和待连接的地址
由HTTP规范定义的各种方法对应各种不同的HttpRequest类。
我们将使用Get方法,这是一个简单的方法,它只是简单地取得一个URL,获取URL指向的文档。
request->setRequestType(HttpRequest::Type::GET);
request->setUrl("http://www.httpbin.org/get");
设置回调
无论服务器返回怎样的状态,响应主体response body总是可读的,这至关重要。
request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpComplete,this));
在onHttpComplete里读取响应数据:
std::vector<char> *buffer = response->getResponseData();//Get the request data pointer back
添加请求到HttpClient任务队列
cocos2d::network::HttpClient::getInstance()->send(request);
释放连接
这是一个可以让整个流程变得完整的关键步骤, 我们必须告诉HttpClient,我们已经完成了连接,并且它现在可以重用。如果不这样做的话,HttpClient将无限期地等待一个连接释放,以便它可以重用。
要释放连接,使用:
request->release();
处理响应
现在,我们已经完成了与HttpClient的交互,可以集中精力做我们需要处理的数据。在这个例子中,我们仅仅将它在控制台上输出。
// dump data
std::vector *buffer = response->getResponseData();
printf("Http Test, dump data: ");
for (unsigned int i = 0; i < buffer->size(); i++)
{
printf("%c", (*buffer)[i]);
}
printf("\n");
如果你需要把response作为一个流来读取它里面的信息,上面的步骤将会同如何解析这个连接结合,当你处理完所有的数据后,关闭输入流,并释放该连接。
GET请求示例
下面是一个通过HttpClient的HTTP GET请求的例子。
HttpRequest* request = new HttpRequest();
request->setUrl("http://www.baidu.com");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpRequestCompleted,this));
request->setTag("GET test");
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();
POST请求示例
下面将发送一个POST请求到URL“http://httpbin.org/post”。
HttpRequest* request = new HttpRequest();
request->setUrl("http://httpbin.org/post");
request->setRequestType(HttpRequest::Type::POST);
request->setResponseCallback(CC_CALLBACK_2(HelloWorld::onHttpRequestCompleted,this));
// write the post data
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
request->setRequestData(postData, strlen(postData));
request->setTag("POST test");
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();
处理网络回调函数
void HelloWorld::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response)
{
if (!response)
{
return;
}
// You can get original request type from: response->request->reqType
if (0 != strlen(response->getHttpRequest()->getTag()))
{
log("%s completed", response->getHttpRequest()->getTag());
}
int statusCode = response->getResponseCode();
char statusString[64] = {};
sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
_labelStatusCode->setString(statusString);
log("response code: %d", statusCode);
if (!response->isSucceed())
{
log("response failed");
log("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");
}
Android
需要注意的是,如果你是Android环境,不要忘了在您的应用程序的Manifest 中增加相应的权限:
<uses-permission android:name=”android.permission.INTERNET” />
详细代码可参照..\tests\Cpp\TestCpp\Classes\ExtensionsTest\NetworkTest\HttpClientTest.cpp