首页 > 代码库 > 【独立开发人员er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势
【独立开发人员er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势
学习cocos2d-x和cocos creator的圈子:cocos2d-x:436689827 cocos creator:124727696
本篇文章主要内容:jsoncpp的使用,Cocos2dx网络编程,聚合数据星座运势接口使用。
1、jsoncpp使用:
jsoncpp的生成请參考博客:Jsoncpp使用具体解释以及链接问题解决
2、聚合数据星座运势接口使用:
我们先登上聚合数据官网。申请相关的数据后,就能够得到APPKEY等信息,调试例如以下:
3、Cocos2dx网络编程:源代码下载请点击(源代码中包含jsoncpp文件)
#include "HelloWorldScene.h" #include "CocoStudio/GUI/UIWidgets/UIButton.h" #include "GUI/CCControlExtension/CCControlButton.h" #include "network/HttpResponse.h" #include "network/HttpClient.h" #include "cocos-ext.h" #pragma comment(lib, "json_vc71_libmt.lib") #include "../jsoncpp/include/json.h" #include<fstream> #include <cassert> using namespace std; USING_NS_CC; USING_NS_CC_EXT; CCScene* HelloWorld::scene() { // ‘scene‘ is an autorelease object CCScene *scene = CCScene::create(); // ‘layer‘ is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it‘s an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::Http)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // create menu, it‘s an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer this->addChild(pLabel, 1); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(pSprite, 0); return true; } void HelloWorld::menuCloseCallback(CCObject* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); #else CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif #endif } #define URLFORMAT "http://web.juhe.cn:8080/constellation/getAll?consName=%s&type=today&key=%s" //格式化URL #define APPKEY "" //APPKEY 填写你自己的APPKEY就能够使用</strong></span> #define BAIYANGZUO "%E7%99%BD%E7%BE%8A%E5%BA%A7" //白羊座 void HelloWorld::Http(CCObject* pSender) { cocos2d::extension::CCHttpRequest* request = new cocos2d::extension::CCHttpRequest(); char szUrl[256] = {0}; sprintf_s(szUrl, 255, URLFORMAT, BAIYANGZUO, APPKEY); request->setUrl(szUrl); request->setRequestType(cocos2d::extension::CCHttpRequest::kHttpGet); request->setResponseCallback(this, callfuncND_selector(HelloWorld::onHttpRequestCompleted)); cocos2d::extension::CCHttpClient::getInstance()->send(request); request->release(); return; } void HelloWorld::onHttpRequestCompleted(cocos2d::CCNode *sender ,void *data) { 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 ++) { result += (*buffer)[i]; } Json::Reader reader; Json::Value root; if ((!reader.parse(result, root, false))) { return; } std::string name = root["name"].asString(); std::string asString = root["summary"].asString(); CCLOG("name:%s, age:%d", name.c_str(), asString); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCLabelTTF* pLabel = CCLabelTTF::create(name.c_str(), "Arial", 24); // position the label on the center of the screen pLabel->setPosition(ccp(visibleSize.width/2, visibleSize.height/2)); // add the label as a child to this layer this->addChild(pLabel, 1); }
最后截图效果:
温馨提示:出现中文乱码。解决方式例如以下:
extension::CCHttpRequest post
加上这段代码
/*std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8");
request->setHeaders(headers);*/
假设没有积分。能够增加群(QQ群号:436689827)索要源代码。
參考博客:
Cocos2dx 网络编程实战之星座运势
HTTP深入浅出 http请求
cocos2dx 网络编程(CCHttpRequest和CURL两个方式)
【独立开发人员er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。