码迷,mamicode.com
首页 > 其他好文 > 详细

【独立开发者er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势

时间:2015-08-01 19:03:31      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:cocos2dx 网络编程实战   jsoncpp   cchttpresponse   cchttprequest   独立开发者er   

本篇文章主要内容: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
<span style="font-size:18px;color:#ff0000;"><strong>#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);
}

最后截图效果:

技术分享


如果没有积分,可以加入群(QQ群号:436689827)索要源码。

版权声明:本文为博主原创文章,未经博主允许不得转载。

【独立开发者er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势

标签:cocos2dx 网络编程实战   jsoncpp   cchttpresponse   cchttprequest   独立开发者er   

原文地址:http://blog.csdn.net/chenqiai0/article/details/47188033

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!