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. Android 推断SD卡是否存在及容量查询

    首先要在AndroidManifest.xml中添加SD卡訪问权限 <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name= ...

  2. Android官方技术文档翻译——Gradle 插件用户指南(1-3)

    不知道是什么网络问题,上午一直发不了博客,其它页面基本正常,就是在写博客这里,每次打开都是响应超时.刚才用了VPN,顺便试了一下,竟然能够编辑.想是CDN之类的问题吧. 这次翻译的是Gradle 插件 ...

  3. jquery:ajax不接收返回值回

    html页面a加元素的假设href=javasrcipt:void(0)会导致ajax没有收到回后台值. : <p class="chatmsg_load_more"> ...

  4. Mybatis之ResultMap一个简短的引论,关联对象

    基础部分能够查看我的还有一篇博客http://blog.csdn.net/elim168/article/details/40622491 MyBatis中在查询进行select映射的时候.返回类型能 ...

  5. 第四章——SQLServer2008-2012资源及性能监控(3)

    原文:第四章--SQLServer2008-2012资源及性能监控(3) 本文为本系列最后一章,监控内存使用.监控服务器的内存是非常重要的事情,有很多情况会引起内存消耗.所以要经常性地做检查. 本文将 ...

  6. Java高效读取大文件(转)

    1.概述 本教程将演示如何用Java高效地读取大文件.这篇文章是Baeldung(http://www.baeldung.com/) 上“Java——回归基础”系列教程的一部分. 2.在内存中读取 读 ...

  7. iOS开发- 隐藏状态栏(电池栏)

    分为两种情况: 1. 想要隐藏某个视图的状态栏, 比方说, 从界面A, push 到界面B的时候, 界面A原本显示状态栏, 然而我们须要界面B不显示状态栏. 这时候, 能够这样做: 在B中实现: - ...

  8. 使用Visual Studio 2010写Data Url生成工具C#版本

    声明:本文系本人按照真实经历原创.未经许可,谢绝转载. 此文百度经验版本号:怎样用Visual Studio 2010打造Data Url生成工具 源代码下载:用Visual Studio 2010编 ...

  9. Ajax请求访问action推断文件是否存在

    action措辞: public ActionForward fileIsExsit(ActionMapping mapping, ActionForm form, HttpServletReques ...

  10. JavaEE(20) - Web层和EJB的整合(Entity Manager)

    1. 使用容器管理的EntityManager 2. 使用应用程序管理的EntityManager 3. 使用ThreadLocal保证EntityManager的线程安全 4. EAO封装JPA 5 ...