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.
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" />
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...