cocos2d-x3.0之请求网络(phpserver)
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
#include "network\HttpClient.h"
#include "cocos-ext.h" class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // a selector callback
void menuCloseCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually
CREATE_FUNC(HelloWorld); void onHttpRequestComplete(cocos2d::network::HttpClient *pSender, cocos2d::network::HttpResponse *pResponse);
void onHttpPostComplete(cocos2d::network::HttpClient *pSender, cocos2d::network::HttpResponse *pResponse);
}; #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
using namespace network; Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create(); // 'layer' is an autorelease object
auto 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 ( !Layer::init() )
{
return false;
} Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->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
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
/*auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
[](Object *sender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
});*/ closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1); /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); // position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer
this->addChild(label, 1); // add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer
this->addChild(sprite, 0); return true;
} void HelloWorld::menuCloseCallback(Ref* pSender)
{
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
// MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
// return;
//#endif
//
// Director::getInstance()->end();
//
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// exit(0);
//#endif HttpRequest *request = new HttpRequest;
request->setUrl("http://127.0.0.1/test2.php");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestComplete));
request->setTag("GET1");
HttpClient::getInstance()->send(request);
request->release(); //HttpRequest* request = new HttpRequest();
// request->setUrl("http://127.0.0.1/test.php");
// request->setRequestType(HttpRequest::Type::POST);
// request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpPostComplete)); //const char* postData = "username=zwcwu&password=123456";
// request->setRequestData(postData,strlen(postData) );
//
//
// request->setTag("POST1");
// HttpClient::getInstance()->send(request);
// request->release();
} void HelloWorld::onHttpRequestComplete(cocos2d::network::HttpClient *pSender, cocos2d::network::HttpResponse *pResponse)
{
if(!pResponse)
{
log("response is null", pResponse);
return;
}
if(!pResponse->isSucceed())
{
log("response failed, %s", pResponse->getErrorBuffer());
return;
} long statusCode = pResponse->getResponseCode();
log("responseCode:%ld", statusCode); std::vector<char> *buffer = pResponse->getResponseData();
std::string buf(buffer->begin(), buffer->end());
log("get requestData:%d,%s", buf.length(), buf.c_str());
} void HelloWorld::onHttpPostComplete(cocos2d::network::HttpClient *pSender, cocos2d::network::HttpResponse *pResponse)
{
if(!pResponse)
{
log("response is null", pResponse);
return;
}
if(!pResponse->isSucceed())
{
log("response failed, %s", pResponse->getErrorBuffer());
return;
} long statusCode = pResponse->getResponseCode();
log("responseCode:%ld", statusCode); std::vector<char> *buffer = pResponse->getResponseData();
std::string buf(buffer->begin(), buffer->end());
log("get requestData:%s", buf.c_str());
}
test.php
<html>
<body>
<?php if(isset($_POST["username"]) && isset($_POST["password"]))
{
echo $_POST["username"];
if($_POST["username"]=="zwcwu" && $_POST["password"]=="123456")
{
echo "Login Success"; //return to client
}
else
{
echo "Login Failed"; //return to client
}
}
else
{
echo "No Username or Password"; //return to client
}
?>
</body>
</html>
test2.php
<?php
echo "WC"
?>
cocos2d-x3.0之请求网络(phpserver)的更多相关文章
- Android请求网络共通类——Hi_博客 Android App 开发笔记
今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
- Android 手机卫士--构建服务端json、请求网络数据
本文地址:http://www.cnblogs.com/wuyudong/p/5900384.html,转载请注明源地址. 数据的传递 客户端:发送http请求 http://www.oxx.com/ ...
- 【Swift】 GET&POST请求 网络缓存的简单处理
GET & POST 的对比 源码:https://github.com/SpongeBob-GitHub/Get-Post.git 1. URL - GET 所有的参数都包含在 URL 中 ...
- Android - 使用Volley请求网络数据
Android - 使用Volley请求网络数据 Android L : Android Studio 14 个人使用volley的小记,简述使用方法,不涉及volley源码 准备工作 导入Volle ...
- 解决React Native使用Fetch API请求网络报Network request failed
问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...
- 安卓请求网络错误 直接在main Thread 进行网络操作出现maintreamexception
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites ...
- android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法
MONO 调用一个线程操作UI 然后报Only the original thread that created a view hierarchy can touch its views.错误 goo ...
- js 获取请求网络协议、IP、端口号、项目名称
js 获取请求网络协议.IP.端口号.项目名称 CreationTime--2018年6月19日15点54分 Author:Marydon /** * 获取url请求前缀 * @return ht ...
- react-native 项目实战 -- 新闻客户端(4) -- 请求网络数据
1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...
随机推荐
- python django模型内部类meta详细解释
Django 模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.下面对此作一总结: abstract 这个属性是定义当前的模型类是不是一个抽象类.所谓抽象类是不会相应 ...
- C++学习笔记25,析构函数总是会宣布virtual
为了永远记住析构函数声明virtual----><<effective c++>> 为这句话不一定对,但无需质疑的是这句话是非常实用的. 查看以下的样例: #includ ...
- apache tomcat 集群! (转)
公司需要一个内部测试局域网, 要求可以支持3000并发访问!以前也没做过服务器这方面.临时抱佛脚,查看了N多文档,他人经验,布置好之后,又遇到了N多问题,功夫不负有心人.终于还是完成了要求!观他人的布 ...
- 【转】Android内存机制分析1——了解Android堆和栈
昨天用Gallery做了一个图片浏览选择开机画面的功能,当我加载的图片多了就出现OOM问题.以前也出现过这个问题,那时候并没有深究.这次打算好好分析一下Android的内存机制. 因为我以前是做VC+ ...
- Android 应用程序启动过程源代码分析
本文转自:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程序的Activity的启动过程.在And ...
- table在 点击线条颜色
效果图: <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 3
DDL Setup Steps SQL> grant execute on utl_file to ggs; Grant succeeded. Create GLOBALS file [orac ...
- 【MySQL案件】ERROR 1418
1.1.1. ERROR 1418 [环境的叙述性说明] mysql5.0.67 [问题叙述性说明] 当它来到创建存储过程ERROR 1418一个错误. # 创建函数SQL声明 CREATE FUNC ...
- NGUI 3.5课程(五岁以下儿童)button-图片切换
然后,我们去了一个样本,做一个button画面切换. 特征,像球员"开始"和"暂停". 写TestButton.cs脚本: using UnityEngine; ...
- Scrum总结
Scrum总结一个轻量级的软件开发方法 Scrum是一个敏捷开发框架,是一个增量迭代的开发过程..在这个框架整个开发周期由若干个小的跌代周期,每个小的的跌代周期称为一个Sprint,每个Sprint的 ...