转自:http://blog.csdn.net/wangbin_jxust/article/details/9632771,http://blog.csdn.net/wangbin_jxust/article/details/9707873
联网游戏开发中,经常遇到http编程,接下来就分三次对cocos2dx的网络编程进阶。
本博文中先介绍建立基本的http通信并得到返回信息。
1.创建cocos2dx工程。
2.项目引用外部库。
如果要使用cocos2dx的CCHttpClient来进行网络访问,则需要引入cocos2dx的相关库,详细步骤如下:
右键单击项目->属性->c/c++->常规,在右边的附件包含目录中添加cocos2dx的extensions目录对应的路径。
然后,右键单击项目->属性->链接器->输入,在右边的附件依赖项中添加libcurl_imp.lib和libExtensions.lib两个库,用分号隔开。
如果不引入extensions文件夹,会出现找不到CCHttpClient的错误;
如果不引入libcurl_imp.lib和libExtensions.lib两个库,编译项目时也会出现报错。
另外注意引入头文件:
#include "cocos-ext.h"
3.添加下载按钮和回调函数。
添加下载按钮:
CCMenuItemImage *pDownloadItem = CCMenuItemImage::create( "bt_blue_light.png", "bt_blue_light.png", this, menu_selector(HelloWorld::menuDownloadCallback) ); CC_BREAK_IF(!pDownloadItem); CCSize pWinSize = CCDirector::sharedDirector()->getWinSize(); CCMenu* pDownloadMenu = CCMenu::create(pDownloadItem, NULL); pDownloadMenu->setPosition(ccp(50 ,50)); CC_BREAK_IF(! pDownloadMenu); this->addChild(pDownloadMenu, 1);
添加按钮的回调函数:
void HelloWorld::menuDownloadCallback(CCObject* pSender) { cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest(); request->setUrl("http://www.oschina.net/action/api/news_list"); request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost); std::vector<std::string> headers; headers.push_back("Content-Type: application/json; charset=utf-8"); request->setHeaders(headers); const char* postData = "catalog=2&pageIndex=1&pageSize=5"; request->setRequestData(postData ,strlen(postData)); request->setResponseCallback(this, callfuncND_selector(HelloWorld::onHttpRequestCompleted)); request->setTag("Post_My_Data"); cocos2d::extension::CCHttpClient::getInstance()->send(request); request->release(); }
按钮的回调函数里向服务器发起http请求了,request->setResponseCallback(this, callfuncND_selector(HelloWorld::onHttpRequestCompleted))一行代码,向请求结束时添加了onHttpRequestCompleted回调函数
4.为http request 结束增加回调函数并读取网络数据
void HelloWorld::onHttpRequestCompleted(cocos2d::CCNode *sender ,void *data) { AllocConsole(); freopen("CONIN$", "r", stdin); freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); cocos2d::extension::CCHttpResponse *response = (cocos2d::extension::CCHttpResponse*)data; if (!response) { return; } if (0 != strlen(response->getHttpRequest()->getTag())) { CCLog("%s completed", response->getHttpRequest()->getTag()); } int statusCode = response->getResponseCode(); char statusString[64] = {}; sprintf(statusString ,"Http status code:%d ,tag = %s" ,statusCode ,response->getHttpRequest()->getTag()); CCLog("response code:%d" ,statusCode); if (!response->isSucceed()) { CCLog("response failed"); CCLog("error buffer:%s" ,response->getErrorBuffer()); } std::vector<char> *buffer = response->getResponseData(); printf("Http response,dump data:"); std::string result = ""; for (unsigned int i = 0; i < buffer->size(); i ++) { printf("%c" ,(*buffer)[i]); } }
debug时就能看到reponse中服务器返回的数据了。
打印到窗体中:
转自:http://blog.csdn.net/sakana87320/article/details/8595242
概述
使用CCHttpClient一般有6步:
- 创建CCHttpRequest实例。
- 设置Url和请求类型。
- 使用CCHttpClient向Url发出请求(连接Url)。
- 读取响应。
- 释放连接。
- 响应处理。
如何使用
获取CCHttpRequest实例
使用一个无参构造方法创建一个CCHttpRequest的实例。CCHttpRequest无参构造方法可以获取一个默认设置的CCHttpRequest实例,该实例可以满足大多数需求。
cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest();
request->setUrl("blog.csdn.net/sakana87320/article/details/8595242");
//设置为GET请求:kHttpGet request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpGet); //设置处理响应回调函数 request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setTag("GET test");
//设置为POST请求:kHttpPost request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpPost); //设置处理响应回调函数 request->setResponseCallback(this, callfuncND_selector(HttpClientTest::onHttpRequestCompleted)); request->setTag("POST test");
cocos2d::extension::CCHttpClient::getInstance()->send(request);
void HttpClientTest::onHttpRequestCompleted(cocos2d::CCNode *sender, void *data) { CCHttpResponse *response = (CCHttpResponse*)data; if (!response) { return; } // 你可以使用: response->request->reqType获取请求类型 if (0 != strlen(response->getHttpRequest()->getTag())) { CCLog("%s completed", response->getHttpRequest()->getTag()); } //获取状态码 int statusCode = response->getResponseCode(); char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); m_labelStatusCode->setString(statusString); CCLog("response code: %d", statusCode); if (!response->isSucceed()) { //访问失败获取错误信息 CCLog("response failed"); CCLog("error buffer: %s", response->getErrorBuffer()); return; } // 获取返回数据。 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(" "); }
request->release();
注意
Android平台下不要忘了添加访问网络的权限。