I’m trying to make HTTP-request on Android build.
void UHttpAdapter::SendHttpRequest(const FString& URL, EHttpMethodType Method, const FString& Content, FOnHttpRequestSuccess Success, FOnHttpRequestFailed Failed)
{
auto MethodName = UCommonUtils::GetEnumValueAsString(TEXT("EHttpMethodType"), Method);
auto OnResponseReceived = [Success, Failed](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
UE_LOG(LogHttpAdapter, Log, TEXT("Response from %s : %s"), *Request->GetURL(), bWasSuccessful ? TEXT("success") : TEXT("failed"));
if (bWasSuccessful)
Success.ExecuteIfBound(Response->GetContentAsString());
else
Failed.ExecuteIfBound();
};
auto Http = &FHttpModule::Get();
auto Request = Http->CreateRequest();
Request->SetURL(*URL);
Request->SetVerb(*MethodName);
Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
if (Method == EHttpMethodType::HM_POST)
Request->SetContentAsString(*Content);
Request->OnProcessRequestComplete().BindLambda(OnResponseReceived);
if (!Request->ProcessRequest())
Failed.ExecuteIfBound();
//auto Response = Request->GetResponse();
UE_LOG(LogHttpAdapter, Log, TEXT("Request to %s by %s"), *Request->GetURL(), *MethodName);
}
But app will crashed on Request->ProcessRequest() call on Android.
Windows build and in Editor works correct.
I thinks about problem with libcurl … but all links are correct
Someone can hepl me with this?
Maybe I need use some another way to call http-request?
Also… Socket connection by FTcpSocketBuilder().Build()->Connect(IpAddr) works correct. This is not a problem with network.
Oh… it… was…
I found error - for some reason MethodName in Request->SetVerb(*MethodName) was incorrect on Android builds, but everything Ok in Editor.
Lord7even
(Lord7even)
December 15, 2018, 6:10pm
3
Could you tell me a bit more how you set this up? Somehow this is not working on my end and I’m searching for houers for a solution. It works well on Windows and the Mobile Preview Window but on the actual Android it seams that the http module is not working. I get absolute no answer from the request. Could this be a problem with the signature of the app?
header
#pragma once
#include "CoreMinimal.h"
#include "HttpAdapter.generated.h"
DECLARE_LOG_CATEGORY_EXTERN(LogHttpAdapter, Log, All);
UENUM(BlueprintType)
enum class EHttpMethodType : uint8
{
GET UMETA(DisplayName = "GET"),
POST UMETA(DisplayName = "POST")
};
DECLARE_DYNAMIC_DELEGATE_OneParam(FOnHttpRequestSuccess, FString, Response);
DECLARE_DYNAMIC_DELEGATE(FOnHttpRequestFailed);
UCLASS()
class MOBAGAME_API UHttpAdapter : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "HttpAdapter")
static void SendHttpRequest(const FString& URL, EHttpMethodType Method, const FString& Content, FOnHttpRequestSuccess Success, FOnHttpRequestFailed Failed);
};
source
#include "HttpAdapter.h"
#include "HttpModule.h"
#include "IHttpResponse.h"
#include "Utils/CommonUtils.h"
DEFINE_LOG_CATEGORY(LogHttpAdapter);
void UHttpAdapter::SendHttpRequest(const FString& URL, EHttpMethodType Method, const FString& Content, FOnHttpRequestSuccess Success, FOnHttpRequestFailed Failed)
{
auto MethodName = UCommonUtils::GetEnumValueAsString(TEXT("EHttpMethodType"), Method);
auto OnResponseReceived = [Success, Failed](FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful)
Success.ExecuteIfBound(Response->GetContentAsString());
else
Failed.ExecuteIfBound();
};
auto Http = &FHttpModule::Get();
auto Request = Http->CreateRequest();
Request->SetURL(*URL);
Request->SetVerb(*MethodName);
Request->SetHeader(TEXT("User-Agent"), TEXT("X-UnrealEngine-Agent"));
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
if (Method == EHttpMethodType::POST)
Request->SetContentAsString(*Content);
Request->OnProcessRequestComplete().BindLambda(OnResponseReceived);
if (!Request->ProcessRequest())
Failed.ExecuteIfBound();
}
Lord7even
(Lord7even)
December 16, 2018, 5:05pm
7
wow, thank you very much! Very kind of you to share your code. mine looks not that different from yours. I’ll try to use your code directly in the project I’m working on. Right now I have the code in a plugin which might be a problem