【UE4学习】16_http访问PHP获得Json测试

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Szu_IT_Man/article/details/56682708

简介

本文主要通过http访问php获取json包进行小测试,该源码是以插件形式编写,此处记录主要部分的源码。

通过 json php sql,可以实现登录,数值交互等功能。


环境:

win10 64bit
UE4.15



————————————————————————

1.   .cs 加入需要调用的模块HTTP、Json


PublicDependencyModuleNames.AddRange(
				new string[]
				{
					"Core",
					"CoreUObject",
					"Engine",
                                        "HTTP",
                                        "Json"
// ... add other public dependencies that you statically link with here ...});


2. .h头文件类声明,主要暴露给蓝图用,记得包行Http.h 和 json.h


UCLASS(BlueprintType, Blueprintable)
class JSONPLUGIN_API UJsonFunction : public UObject
{
	GENERATED_UCLASS_BODY()

	UFUNCTION(BlueprintCallable, Category = "MyJson2")
		virtual void MyHttpCall(FString URL);


	UFUNCTION(BlueprintCallable, Category = "MyJson2")
		static UJsonFunction* ConstructJsonObject();

	void OnRequestReceived(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful);
};


3.  .cpp实现


UJsonFunction* UJsonFunction::ConstructJsonObject()
{
	return NewObject<UJsonFunction>();
}

void UJsonFunction::MyHttpCall(FString URL)
{
	TSharedRef<IHttpRequest> request = FHttpModule::Get().CreateRequest();
	GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, FString::Printf(TEXT("%s"), *URL));

	request->SetVerb("GET");
	request->SetURL(URL);
	//request->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
	request->OnProcessRequestComplete().BindUObject(this, &UJsonFunction::OnRequestReceived);
	request->ProcessRequest();
}

void UJsonFunction::OnRequestReceived(FHttpRequestPtr request, FHttpResponsePtr response, bool bWasSuccessful)
{
	TSharedPtr<FJsonObject> jsonObj;
	TSharedRef<TJsonReader<>> jsonReader = TJsonReaderFactory<>::Create(response->GetContentAsString());

	if (!bWasSuccessful) {
		UE_LOG(LogJson, Error, TEXT("Response was invalid! Please check the URL."));

		// Broadcast the failed event
		return;
	}

	FString result = response->GetContentAsString();
	GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, FString::Printf(TEXT("%s"), *result));
	UE_LOG(LogTemp, Warning, TEXT("Json:   %s"), *result);

	if (FJsonSerializer::Deserialize(jsonReader, jsonObj))
	{
		int32 recievedInt = jsonObj->GetIntegerField("myRank");
		GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, FString::FromInt(recievedInt));
		UE_LOG(LogTemp, Warning, TEXT("myRank:   %d"), recievedInt);

		FString name = jsonObj->GetStringField("name2");
		GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Green, name);
		UE_LOG(LogTemp, Warning, TEXT("myRank:   %s"), *name);
	}
}


4. php部分的代码


<?php
//get_userinfo.php
if(date_default_timezone_get() != "Asia/Shanghai") 
{
	date_default_timezone_set("Asia/Shanghai");
}

$func=$_GET['fun'];
if ('search'==$func) {
	$arrayName = array(
						'myRank' =>12 ,
						'name2'=>"PHP Json Test."
						);
	echo json_encode($arrayName);

?>

5.效果



给自己的源码(点击打开链接

参考:

展开阅读全文

没有更多推荐了,返回首页