学习cocos2d-x和cocos creator的圈子:cocos2d-x:436689827    cocos creator:124727696

本篇文章主要内容:jsoncpp的使用,Cocos2dx网络编程,聚合数据星座运势接口使用。

1、jsoncpp使用:

jsoncpp的生成请參考博客:Jsoncpp使用具体解释以及链接问题解决

2、聚合数据星座运势接口使用:

我们先登上聚合数据官网。申请相关的数据后,就能够得到APPKEY等信息,调试例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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 网络编程实战之星座运势的更多相关文章

  1. 【独立开发人员er Cocos2d-x实战 008】BMFont生成位图字体工具和Cocos2dx使用载入fnt文件

    1.首先我们须要下载而且安装BMFont工具,下载地址例如以下:http://download.csdn.net/detail/chenqiai0/8899353(里面还有具体的使用文档,假设使用中有 ...

  2. 【独立开发人员er Cocos2d-x实战 001】csb文件导出和载入

    使用cocos studio进行资源文件导出: 然后在cocosproject中进行载入csb文件:  auto myLayout = CSLoader::createNode("/res/ ...

  3. 【独立开发人员er Cocos2d-x实战 011】Cocos2dx 3.x命令行生成APK具体解释

    Cocos2d-x 3.6项目打包生成apk安卓应用文件,搭建安卓环境的步骤有点繁琐.但搭建一次之后,以后就会很快捷! 过程例如以下: 一.下载安卓环境:搭建Android环境须要用到Android ...

  4. 【独立开发人员er Cocos2d-x实战 007】使用Cocos2dx UserDefault.xml

    这篇博客是因为下述问题产生的 -(代码1): std::string str = FileUtils::getInstance()->getWritablePath(); CCLOG(str.c ...

  5. 【独立开发人员er Cocos2d-x实战 009】Cocos2dx 菜单项CCMenu使用

    Cocos2dx中的菜单用CCMenu/Menu类来实现.该类是一个容器.用来装载各种菜单项,用于菜单项能够是图片.系统字体等. 理论就不说了.先上代码: CCMenuItemToggle* item ...

  6. 独立开发人员低成本推广APP的18条技巧

    导语:知道并不等于运行,有些最主要的推广方法往往会被忽略.这些,是自国外开发人员总结出的这18条经验. 如今市面上充满了大牌子大公司和大制作的手机游戏,常常有游戏花300万成本开发,然后再花2000万 ...

  7. 在 Boolan 网开讲《网络编程实战》课程

    <网络编程实战>是一门以讲解实例为主的课程,每一节都讲一两个网络编程的例子程序,课程偏重 Linux 服务端 TCP 网络编程. 本课程要求听课人员已经读过<Unix 网络编程> ...

  8. c++后台开发面试常见知识点总结(二)网络编程

    (1)TCP和UDP有什么区别? TCP是传输控制协议,提供的是面向连接的,可靠地字节流服务.使用三次握手建立连接,四次挥手释放连接.UDP是用户数据报协议,传输的是UDP数据报,是无连接的,而且没有 ...

  9. Cocos2d-X网络编程(4) Cocos2d中的网络通信协议——Socket通信

    Socket,俗称网络套接字,本身并不是协议,而是一个调用接口,是对TCP/IP协议的封装和应用,.提供了一系列方法方便开发者进行网络通讯. TCP/IP协议是使用最早的通讯协议,它是传输层协议,主要 ...

随机推荐

  1. Oracle API Gateway连接WebService服务,攻击保护

    1.启动和连接OAG OAG连接的时候除了不选择analysis,其他都选上,然后启动Gateway实例以及Nodemanager. 命令如下: /$OAG_HOME/apigateway/posix ...

  2. iOS:二维码的生成

    所谓的二维码就是一个图片,只不过在iOS需要借用<CoreImage/CoreImage.h>来实现,  并且二维码图片是通过CIImage来转成UIImage的.具体步骤如下: // 1 ...

  3. Keen Team

    Keen Team (碁震安全研究团队,KeenTeam)是一支由在信息安全理论和技术研究方面全球领先的中国“白帽”安全专家组成的信息安全研究队伍,成员主要来自微软的安全漏洞研究.安全攻击和防御技术研 ...

  4. linux基础-第十九单元_nfs服务

    #服务端部署 介绍: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的 ...

  5. 【微信转载】Google是如何做测试的

    就 目前的软件公司而言,Google无疑是在开放和创新力方面做得最好的.而如何支撑Google这种快速地扩张的研发能力以及迭代速度,并且产品质量总是 一如以往的能给人们很棒的用户体验?这是一个值得我们 ...

  6. Appstore 提交时错误

      ERROR ITMS-9000:This boundle is invalid. New apps and app updates submitted to the App Store must  ...

  7. PHP中单引号双引号使用原则

    PHP中单引号双引号使用原则   1.PHP中尽量用单引号,HTML代码全部用双引号   2.在包含变量的时候,用双引号可以简化操作   3.复杂的情况下用大括号包起来   4 PHP引号还有一个用处 ...

  8. python——list访问问题

    rect.append({'bbox':[(int(rect1), int(rect2)), (int(rect3), int(rect4))]}) 应这样访问 rect[0]['bbox'][0][ ...

  9. Data truncation: Data too long for column

    是字符集问题引起的,用show full fields from + 表名就可以看出你的列的编码格式把它改成GBK或者GB2312.uTF-8.如果还不行的话,把你表的编码格式也改成上面的编码格式,我 ...

  10. MySQL-配置优化技巧

    一.连接请求配置 1.查询当前连接数(show full processlist) show full processlist; 2.最大连接数(max_connections) max_connec ...