コード:
#include <cpprest/http_client.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include "DxLib.h"
char *str;
pplx::task<void> Get()
{
return pplx::create_task([]
{
// proxyあり
// auto config = http_client_config();
// config.set_proxy(web_proxy(utility::conversions::to_string_t("http://127.0.0.1:8080")));
// http_client client(L"https://jsonplaceholder.typicode.com/posts/1", config);
http_client client("https://jsonplaceholder.typicode.com/posts/1");
return client.request(methods::GET);
}).then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
// レスポンスを文字列として取得後、標準出力する
// auto body = response.extract_string();
// std::wcout << body.get().c_str() << std::endl;
// レスポンスをJSONとして解析する
return response.extract_json();
}
}).then([](json::value json)
{
// タイトルだけ取得する
// この関数内でprintfDx,DrawFormatStringを使うと以降それらの関数が正常に使えなくなるため文字列の挿入のみとする(Get().wait()関数が原因?)
sprintf(str, "%s", json["title"].as_string().c_str());
//printfDx("\n\n\ntest3 %s", json["title"].as_string().c_str());
//DrawFormatString(0, 0, GetColor(255, 255, 255), "test3 %s", json["title"].as_string().c_str());
//DrawFormatString(0, 0, GetColor(128, 255, 255), "test ");
});
}
// プログラムは android_main から始まります
int android_main(void)
{
const ANativeActivity *NativeActivity;
NativeActivity = GetNativeActivity();
cpprest_init(NativeActivity->vm);
if (DxLib_Init() == -1) // DXライブラリ初期化処理
{
return -1; // エラーが起きたら直ちに終了
}
str = new char[1024]();
SetDrawScreen(DX_SCREEN_BACK);
int x = 220, y = 140;
try
{
Get().wait();
}
catch (const std::exception &e)
{
std::cout << "Error " << e.what() << std::endl;
}
while (ProcessMessage() == 0) {
ClearDrawScreen();
if (GetTouchInputNum() > 0) {
GetTouchInput(0, &x, &y);
}
DrawBox(x, y, x + 200, y + 200, GetColor(255, 255, 255), TRUE); // 四角形を描画する
clsDx();
printfDx("test");
DrawFormatString(0, 25, GetColor(255, 255, 255), "test2");
DrawFormatString(0, 50, GetColor(255, 255, 255), "test3 %s",str);
ScreenFlip();
WaitKey(); // キー入力待ち
}
//DrawBox(220, 140, 420, 340, GetColor(255, 255, 255), TRUE); // 四角形を描画する
delete str;
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}