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. 从苹果系统InstallESD.dmg里提取IOS

    右键下载的Mac OS X Mountain Lion镜像:InstallESD.dmg,选择7-zip------打开压缩包 2.双击InstallMacOSX.pkg 3.选中InstallESD ...

  2. Android - 直线(line)画法

    Android - 直线(line)画法 本文地址: http://blog.csdn.net/caroline_wendy 横线(horizontal line) <View android: ...

  3. C指针决心 ------ 指针表达式

    本文是自己学习所做笔记.欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 所谓的指针表达式是指一个表达式.其结果是一个指针. 例1. int  a,b; ...

  4. Docker系列之(一):10分钟玩转Docker(转)

    1.前言 进入云计算的时代,各大云提供商AWS,阿里云纷纷推出针对Docker的服务,现在Docker是十分火爆,那么Docker到底是什麽,让我们来体验一下. 2.Docker是什麽 Docker是 ...

  5. 使用Java中间MessageDigest该文本MD5加密(Java中间MD5样品加密算法演示)

    原文地址:http://www.wenboxz.com 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  6. Spring面试问答Top 25

    欢迎大家向我推荐你在面试过程中遇到关于Spring的问题. 我会把大家推荐的问题加入到以下的Spring经常使用面试题清单中供大家參考. 问题清单: 什么是Spring框架?Spring框架有哪些主要 ...

  7. UVA 193 Graph Coloring 图染色 DFS 数据

    题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色. 很水的一题,用dfs回溯即可.先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递归 ...

  8. Linux解析内核源代码——传输控制块诞生

    原创文章是freas_1990,转载请注明出处:http://blog.csdn.net/freas_1990/article/details/23795587 在Linux 2.6一旦(不包含2.6 ...

  9. k8s google sample - guestbook

    Redis读写分离作为存储 PHP网页作为前端 github地址 https://github.com/kubernetes/kubernetes/blob/release-1.1/examples/ ...

  10. urlrewrite使用地址重写

    地址重写: 主要是为了站点的安全. 比如我们平时的地址请求 地址重写前,訪问路径是: /read.egov?action=read&bid=2 地址重写后,訪问路径是:/read-read-2 ...