cocos2d-x 3.1.1 学习笔记[11] http请求 + json解析
//http须要引入的头文件和命名空间
#include <network/HttpClient.h>
using namespace network;
//json须要引入的头文件
#include <json/document.h>
//然后声名两个函数
void http();//用于运行http请求
void onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response);// http请求的回调
void Nice::http()
{
//创建一个get请求
HttpRequest* request = new HttpRequest();
request->setUrl("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=Gk0Bbh9H4iREjesugyEqySN7");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(Nice::onHttpRequestCompleted, this));
request->setTag("json");
HttpClient::getInstance()->send(request);
request->release(); //创建一个post请求
HttpRequest* request_post = new HttpRequest();
request_post->setUrl("http://httpbin.org/post");
request_post->setRequestType(HttpRequest::Type::POST);
std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8");
request_post->setHeaders(headers);
request_post->setResponseCallback(CC_CALLBACK_2(Nice::onHttpRequestCompleted, this));
//post数据
const char* postData = "visitor=cocos2d&TestSuite=Extensions Test/NetworkTest";
request_post->setRequestData(postData, strlen(postData));
request_post->setTag("POST test2");
HttpClient::getInstance()->send(request_post);
request_post->release();
} void Nice::onHttpRequestCompleted(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response)
{
if (!response) {
return; } if (0 != strlen(response->getHttpRequest()->getTag())) {
log("%s completed",response->getHttpRequest()->getTag());
} long statusCode = response->getResponseCode();
char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %ld, tag = %s",statusCode,response->getHttpRequest()->getTag());
log("response code: %ld",statusCode); if (!response->isSucceed()) {
log("response failed");
log("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]);
} //将std::vector(char)* 转换成 std::string的两种方法
std::string backStr = std::string(&(*buffer->begin()), buffer->size());
std::string anotherStr;
anotherStr.insert(anotherStr.begin(), buffer->begin(), buffer->end());
printf("%s\n", backStr.c_str());
printf("%s\n", anotherStr.c_str());
printf("\n"); if (strcmp(response->getHttpRequest()->getTag(), "json") == 0) {
rapidjson::Document doc;
doc.Parse<0>(backStr.c_str());
const rapidjson::Value& v = doc["status"];
printf("status is : %s",v.GetString());
const rapidjson::Value& dir = doc["results"];
if (dir.IsArray()) {
unsigned int i = 0;
const rapidjson::Value& city = dir[i]["currentCity"];
log("city is %s", city.GetString());
//多层測试
const rapidjson::Value& title = doc["results"][(unsigned int)0]["index"][(unsigned int)2]["title"];
log("the title is %s", title.GetString());
}
} }
cocos2d-x 3.1.1 学习笔记[11] http请求 + json解析的更多相关文章
- [XMPP]iOS聊天软件学习笔记[一]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- cocos2d-x 3.1.1学习笔记[23]寻找主循环 mainloop
文章出自于 http://blog.csdn.net/zhouyunxuan cocos2d到底是怎样把场景展示给我们的,我一直非常好奇. 凭个人猜想,引擎内部的结构类似于这样 while(true ...
- cocos2d-x 3.1.1 学习笔记[3]Action 动作
这些动画貌似都非常多的样子,就所有都创建一次. 代码例如以下: /* 动画*/ auto sp = Sprite::create("card_bg_big_26.jpg"); Si ...
- cocos2d-x 3.1.1 学习笔记[2]Sprite 精灵
Sprite应该是用到最多的一个类吧.无法想像一个游戏没有精灵将怎样进行愉快的玩耍. Sprite继承于Node 和 TextureProtocol. Sprite是一个2d的图像. Sprite能够 ...
- cocos2d-x 3.1.1 学习笔记[21]cocos2d-x 创建过程
文章出自于 http://blog.csdn.net/zhouyunxuan RootViewController.h #import <UIKit/UIKit.h> @interfac ...
- cocos2d-x 3.1.1 学习笔记[4]GridActions 网格动画
文章写的 http://blog.csdn.net/zhouyunxuan 老样子.见代码. //GridActions can only used on NodeGrid auto nodeGri ...
- [XMPP]iOS聊天软件学习笔记[四]
昨天完成了聊天界面,基本功能算告一段落 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework git clone https://githu ...
- [XMPP]iOS聊天软件学习笔记[三]
今天做了好友界面,其实xmpp内部已经写好很多扩展模块,所以使用起来还是很方便的 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework gi ...
- [XMPP]iOS聊天软件学习笔记[二]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
随机推荐
- ORACLE中通过DBMS_CRYPTO包对表敏感字段进行加密
http://doc.primeton.com/pages/viewpage.action?pageId=4917998
- 【UOJ UNR #1】火车管理 可持久化线段树
用可持久化线段树维护每个站的第一辆车和每个站的前一次更新的位置即可. #include<iostream> #include<cstring> #include<cstd ...
- 计蒜客 31458.Features Track-滚动数组+STL(map)连续计数 (ACM-ICPC 2018 徐州赛区网络预赛 F)
F. Features Track Morgana is learning computer vision, and he likes cats, too. One day he wants to f ...
- python模块之XlsxWriter
官网Tutorial:http://xlsxwriter.readthedocs.io/tutorial Xlsx是python用来构造xlsx文件的模块,可以向excel2007+中写text,nu ...
- Codeforces 863F - Almost Permutation
863F - Almost Permutation 题意 给出每个位置可以放的数字的范围,定义 \(cost = \sum_{i=1}^{n}(cnt(i))^2\) ,其中 \(cnt(i)\) 为 ...
- [BZOJ 1143] 祭祀river
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1143 Solution: 一道最长反链的模板题 由Dilworth定理可知:最小链覆盖数 ...
- 【计算几何】【斜率】bzoj1610 [Usaco2008 Feb]Line连线游戏
枚举直线,计算斜率,排序,统计答案. #include<cstdio> #include<cmath> #include<algorithm> using name ...
- 什么是EPEL 及 Centos上安装EPEL(转)
什么是EPEL 及 Centos上安装EPEL 转自:http://www.unxmail.com/read.php?67 RHEL以及他的衍生发行版如CentOS.Scientific Linux为 ...
- 【IntellJ IDEA】idea忽略隐藏文件、文件夹的设置操作
左上角setting 如果要忽略文件夹,则直接填写文件夹名字即可,例如:要忽略target文件夹[建议:尽量不要把target忽略,因为可能编译出问题排查,还需要查看target文件夹中的编译结果] ...
- 如何使用ninja编译系统编译我们的程序?
使用ninja 配置自己的环境来使用ninja 构建程序 Android使用ninja Windows使用 调试 不使用VS 技巧 问题 Ninja的原意是忍者,忍者神龟的忍者.这里被google拿来 ...