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. MMTool制作Ozmosis引导BIOS完美引导OS X系统

    Ozmosis引导是德国黑苹果爱好者制作的一个引导程序,目前仍处于测试版,有了它,你可以不用再使用四叶草.变色龙之类引导工具,相对而言它更象白苹果.Ozmosis是基于AMI公司bios的硬件引导驱动 ...

  2. java提高篇(十四)-----关键字final

    在程序设计中,我们有时可能希望某些数据是不能够改变的,这个时候final就有用武之地了.final是java的关键字,它所表示的是"这部分是无法修改的".不想被改变的原因有两个:效 ...

  3. CreateEvent、SetEvent、ResetEvent和WaitForSingleObject

    事件对象就像一个开关:它仅仅有两种状态---开和关.当一个事件处于"开"状态.我们称其为"有信号".否则称为"无信号". 能够在一个线程的运 ...

  4. 原代码,反码,解释和具体的补充 Java在&gt;&gt;和&gt;&gt;&gt;差异

    前两天分析 HashMap 的 hash 算法的时间,会见 >> 和 >>> 这两个符号.然后检查以下信息,我脑子里在某一时刻.今天遇到,我没想到居然忘  0-0.... ...

  5. NYOJ 1068 ST(段树 为段更新+间隔总和)

    ST 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描写叙述 "麻雀"lengdan用随机数生成了后台数据.可是笨笨的他被妹纸的问题给难住了. .. 已知 ...

  6. 2014 ACM湖南匹配10会议省赛

    2014湖南游戏..... 1:牡丹江Regional有些球队没来的冲突 2:题目比較水 3:队友神勇发挥 最终在开局不利的情况下完毕了翻盘,拿到了第二名.....没有抓住机会顺势夺冠还是非常遗憾的. ...

  7. Linux以下银行乱码

    更改 /etc/sysconfig/i18n 档,例如 LANG="en_US.UTF-8",xwindow它会显示英文界面. LANG="zh_CN.GB18030&q ...

  8. SICP 习题(1.1,1.2,1.3,1.4)解题总结。

    近来在重读SICP,以前读过一次,读了第一二章就没有坚持下去,时间一长就基本忘记了,脑海里什么都不剩,就隐约记得自己曾经读过一本很牛B的书. 这次读希望能够扎实一点,不管能读到哪里,希望可以理解一些东 ...

  9. VS路宏 vc++于OutDir、ProjectDir、SolutionDir不同的路径

    说明 $(RemoteMachine) 设置为"调试"属性页上"远程计算机"属性的值.有关很多其它信息,请參见更改用于 C/C++ 调试配置的项目设置. $(R ...

  10. 深入浅出学习Hibernate框架(一):从实例入手初识Hibernate框架

    这篇博客是hibernate学习的第一篇,主要简介hibernate框架,之后简单说一下hibernate的文件夹结构,最后写一个简单的hibernate实例.通过这三步来简单的认识一下hiberna ...