Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Networking with HTTP


In recent smartphone games, we normally use an Internet network to update data, download resources, and so on. There aren't any games developed without networking. In this recipe, you will learn how to use networking to download resources.

Getting ready

You have to include the header file of network/HttpClient to use networking.

 #include "network/HttpClient.h"

If you run it on Android devices, you need to edit proj.android/AndroidManifest.xml.

<user-permission android:name="android.permission.INTERNET" />

How to do it...

In the following code, we will get the response from http://google.com/ and then, print the response data as a log.

auto request = new network::HttpRequest();
request->setUrl("http://google.com/ ");
request->setRequestType(network::HttpRequest::Type::GET);
request->setResponseCallback([](network::HttpClient* sender, network::HttpResponse* response){
    if (!response->isSucceed()) {
        CCLOG("error");
        return;
    }

    std::vector...