[UE5/C++] ๐ŸŒ Unreal์—์„œ Http ์š”์ฒญํ•˜๊ธฐ (2)

Singery00ยท2024๋…„ 9์›” 25์ผ
0

UE5 C++

๋ชฉ๋ก ๋ณด๊ธฐ
20/20
post-thumbnail

๊ฐœ์š”

๐ŸŒ ์–ธ๋ฆฌ์–ผ ์—”์ง„์—์„œ Http ์š”์ฒญ์œผ๋กœ ํ†ต์‹ ํ•ด๋ณด์ž.

๋ฐฑ์—”๋“œ ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•˜์—ฌ API ํ†ต์‹ ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ํ•ด๋ณด๊ณ  ์‹ถ์–ด์กŒ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์–ธ๋ฆฌ์–ผ ์—”์ง„์—์„œ Http ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๊ฐ’์„ ๋ฐ›์•„
JSON์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์‹œ๋ฆฌ์ฆˆ๋กœ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ  ์˜์ƒ


๋ณธ๋ก 

์ด๋ฒˆ ์‹ค์Šต์—์„œ ์‚ฌ์šฉํ•  ํ…Œ์ŠคํŠธ URL์ž…๋‹ˆ๋‹ค.
https://jsonplaceholder.typicode.com/posts/1

์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์„ ๊ฐ€์ง€๋Š”๊ฑธ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

Response {
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

์˜์กด์„ฑ ์ถ”๊ฐ€

Json ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์˜์กด์„ฑ์€ ์ด๋ฏธ HTTP์™€ ๊ฐ™์ด ์ถ”๊ฐ€ ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.


ํ—ค๋” ์ถ”๊ฐ€

Json.h ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”.

์ด๊ฑฐ๋งŒ ํ•˜๋ฉด ํ—ค๋” ํŒŒ์ผ์€ ์ˆ˜์ •ํ• ๊ฒŒ ์—†์Šต๋‹ˆ๋‹ค.

#include "Json.h"

์†Œ์Šค ํŒŒ์ผ ์ˆ˜์ •

์šฐ๋ฆฌ๋Š” ์‘๋‹ต๋ฐ›์€ Response๋ฅผ ํŒŒ์‹ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด OnResponseReceived ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ Json์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์ฃ ?

์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ถ€๋ถ„ ๋’ค๋ถ€ํ„ฐ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด๋ด…์‹œ๋‹ค.

FJsonObject ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด๋Š” Json ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ฒŒ ๋  ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

// Json ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๊ฐ์ฒด
TSharedPtr<FJsonObject> RefObj;
    

์ด์ œ Json์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ๋ฆฌ๋” ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด ๊ฐ์ฒด๋Š” JSON ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ์ฝ๋Š” ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์„ FJsonObject์™€ ๊ฐ™์€ Unreal ์—”์ง„์˜ ๊ตฌ์กฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.

// Json ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฆฌ๋” ๊ฐ์ฒด
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());

Deserialize๋Š” ์—ญ์ง๋ ฌํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

// Reader๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑํ•˜๊ณ , ์ด๋ฅผ FJsonObject๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
FJsonSerializer::Deserialize(Reader, RefObj);

์ „์ฒด ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

void AGM_API_Test::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{

	// ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฌธ

	// Json ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๊ฐ์ฒด
	TSharedPtr<FJsonObject> RefObj;

	// Json ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฆฌ๋” ๊ฐ์ฒด
	TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());

	// Reader๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์„ ํŒŒ์‹ฑํ•˜๊ณ , ์ด๋ฅผ FJsonObject๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
	FJsonSerializer::Deserialize(Reader, RefObj);

	// ์š”์ฒญ์ด ์™„๋ฃŒ๋˜์—ˆ์Œ์„ ์ถœ๋ ฅ
	UE_LOG(LogTemp, Warning, TEXT("Response : %s"), *Response->GetContentAsString());
	UE_LOG(LogTemp, Warning, TEXT("title : %s"), *RefObj->GetStringField("title"));
	UE_LOG(LogTemp, Warning, TEXT("body : %s"), *RefObj->GetStringField("body"));

}

์‹คํ–‰ํ•ด๋ณด๋ฉด ๊ฐ ํ•„๋“œ์— ๋งž๊ฒŒ ์ถ”์ถœ๋˜์–ด ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.


๋งˆ๋ฌด๋ฆฌ

๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.
์ง€๊ธˆ์€ ์ž„์‹œ๋กœ ๊ฐ ํ‚ค๋ฅผ ์ง์ ‘ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ง์ ‘ API๋ฅผ ์„ค๊ณ„ํ•˜์—ฌ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค๊ณ  ํ•˜๋ฉด ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ํฌ์ŠคํŒ…์—๋Š” ์„œ๋ฒ„์—๊ฒŒ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ ์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ์‹ค์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

profile
๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์–ด๋ณด์ž

0๊ฐœ์˜ ๋Œ“๊ธ€