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)的更多相关文章

  1. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  2. Android 手机卫士--构建服务端json、请求网络数据

    本文地址:http://www.cnblogs.com/wuyudong/p/5900384.html,转载请注明源地址. 数据的传递 客户端:发送http请求 http://www.oxx.com/ ...

  3. 【Swift】 GET&POST请求 网络缓存的简单处理

     GET & POST 的对比 源码:https://github.com/SpongeBob-GitHub/Get-Post.git 1. URL - GET 所有的参数都包含在 URL 中 ...

  4. Android - 使用Volley请求网络数据

    Android - 使用Volley请求网络数据 Android L : Android Studio 14 个人使用volley的小记,简述使用方法,不涉及volley源码 准备工作 导入Volle ...

  5. 解决React Native使用Fetch API请求网络报Network request failed

    问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...

  6. 安卓请求网络错误 直接在main Thread 进行网络操作出现maintreamexception

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites ...

  7. android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法

    MONO 调用一个线程操作UI 然后报Only the original thread that created a view hierarchy can touch its views.错误 goo ...

  8. js 获取请求网络协议、IP、端口号、项目名称

      js 获取请求网络协议.IP.端口号.项目名称 CreationTime--2018年6月19日15点54分 Author:Marydon /** * 获取url请求前缀 * @return ht ...

  9. react-native 项目实战 -- 新闻客户端(4) -- 请求网络数据

    1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...

随机推荐

  1. linux 下修改 apache 启动的所属用户和组

    apache默认启动的用户和组是www-data,所以有些时候,就会涉及到权限问题,没有权限在执行目录下创建或者读写文件.改变用户和组的方法其实很简单: 1.进入到apache默认安装路径/etc/a ...

  2. jQuery EasyUI实现全部关闭tabs

    有时,当我们打开很多tabs当标签,要关闭一个接一个,它只能被关停 显然太麻烦,能够在选项卡的最右边加入一个button 实现关闭所有. 代码例如以下: <!DOCTYPE HTML PUBLI ...

  3. Unicode字段也有collation

    原文:Unicode字段也有collation 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/01/11/unicode-collation.aspx ...

  4. 动态规划,而已! CodeForces 433B - Kuriyama Mirai&#39;s Stones

    Kuriyama Mirai has killed many monsters and got many (namely n) stones. She numbers the stones from  ...

  5. .NET反编译之Reflector基础示例

    这几日由于公司需要, 看了些.NET反编译技巧,特地和大家分享下 .NET反编译工具很多,Reflector是其中一个很优秀的工具,所以就用它来进行反编译工作了.今天我们就用"繁星代码生成器 ...

  6. Android用户界面设计:框架布局(转)

    摘要:框架布局是Android开发者组织视图控件最简单和最有效的布局之一.通过本文,你将学到所有关于框架布局的知识,它们主要用来在屏幕上组织特别的或重叠的视图控件.使用得当的话,很多有趣的Androi ...

  7. Codeforces 383C . Propagating tree【树阵,dfs】

    标题效果: 有一棵树,有两种操作模式对本树:1:表示为(1 x val),在NOx加在节点上val,然后x每个节点加上儿子- val.给每个儿子一个儿子在一起-(- val),加到没有儿子为止.2:表 ...

  8. poj3417 Network 树形Dp+LCA

    题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...

  9. ZOJ-3652-Maze(BFS)

    Celica is a brave person and believer of a God in the bright side. He always fights against the mons ...

  10. oracle之sql语句优化

    oracle之sql语句优化 sql语句的优化 1.在where子句中使用 is null 或 is not null 时,oracle优化器就不能使用索引了. 2.对于有连接的列,即使最有一个是静态 ...