学习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. ASP.NET Core 1.0基础之静态文件处理

    来源 这些HTML , CSS files, image files, 和JavaScript这些静态文件,是ASP.NET能够直接响应给客户端的.本文详述下ASP.NET和静态文件的关系. Serv ...

  2. Flex4以后Menubar背景设置问题

    flex4的 backgroundColor失效,需要使用 contentBackgroundColor menubar.setStyle("contentBackgroundColor&q ...

  3. OOP几大原则【转】

    设计模式遵循的一般原则: 1.开-闭原则(Open-Closed Principle, OCP):一个软件实体应当对扩展开发,对修改关闭.说的是,再设计一个模块的时候,应当使这个模块可以在不被修改的前 ...

  4. solr6.6 导入 pdf/doc/txt/json/csv/xml文件

    文本主要介绍通过solr界面dataimport工具导入文件,包括pdf.doc.txt .json.csv.xml等文件,看索引结果有什么不同.其实关键是managed-schema.solrcon ...

  5. Android架构分析之Android消息处理机制(一)

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在这个系列文章中我们将来分析Android消息处理机制. 本文介绍了一个使用Han ...

  6. EffectiveJava(25)泛型和数组的使用取舍及规范

    泛型和数组 泛型:1.泛型是不可变的.对于任意两个不同类型Type1,type2;List既不是List的子类型,也不是List的超类型 2.泛型是通过擦除来实现的.故泛型只在编译时强化它们的信息,并 ...

  7. 爪哇国新游记之十九----使用Stack检查数字表达式中括号的匹配性

    /** * 辅助类 * 用于记载字符和位置 * */ class CharPos{ char c; int pos; public CharPos(char c,int pos){ this.c=c; ...

  8. 微信小程序 - 上传图片纯前端(多张、单张)

    演示如下 可能有些命名不太规范,到时改一下即可 点击从github拉取:图片上传示例

  9. JS的同步加载、异步加载

    在使用js展开式菜单时,发现只有加载完页面包含的js文件时,展开菜单才能折叠起来. 查找了一下原因:是因为js页面加载使用的是同步模式,又称阻塞模式,会阻止浏览器的后续处理,停止后续的解析,只有当当前 ...

  10. 什么时候使用PHP设计模式和为什么要使用?

    有大量的文章解释什么是设计模式,如何实现设计模式,网络上不需要再写一篇这样的文章.相反,在本文中我们更多的讨论什么时候用和为什么要用,而不是用哪一个和如何使用. 我将会为这些设计模式描绘不同的场景和案 ...