MyHttp.cpp
//
// MyHttp.cpp
// TestCocos2dx
//
// Created by zhang peng on 13-5-1.
// Copyright 2013年 __MyCompanyName__. All rights reserved.
//
#include "MyHttp.h"
#include "cocos-ext.h"
using namespace cocos2d;
USING_NS_CC_EXT;
USING_NS_CC;
MyHttp::MyHttp()
{
CCLog("初始化");
}
MyHttp::~MyHttp()
{
}
bool MyHttp::init()
{
if ( !CCNode::init() )
{
return false;
}
CCLog("************开始加载*************");
cocos2d::extension::CCHttpRequest* req = new cocos2d::extension::CCHttpRequest();
req->setRequestType(CCHttpRequest::kHttpGet);
/*请求的类型
kHttpGet,get请求
kHttpPost,post请求
kHttpUnkown 呵呵
*/
req->setRequestData("HelloWorld",strlen("HelloWorld"));//请求的数据
req->setTag("myTag");//tag
req->setUrl("http://www.baidu.com");//设置请求地址
//设置回调函数
req->setResponseCallback(this, callfuncND_selector(MyHttp::onSendCompleted));
//发送请求
cocos2d::extension::CCHttpClient::getInstance()->send(req);
req->release();
return true;
}
void MyHttp::onSendCompleted(CCNode* node,void* data)
{
CCHttpResponse *response = (CCHttpResponse* ) data;
std::vector<char> *buffer = response->getResponseData();
printf("Http Test, dump data: ");
std::string str = "";
for (unsigned int i = 0; i < buffer->size(); i++)
{
str += (*buffer)[i];
//CCLog("%c", (*buffer)[i]);//这里打印从服务器返回的数据
}
str += '\0';
CCLog("%s", str.c_str());
}
主类调用
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
MyHttp* http = new MyHttp();
http->init();
}