Delphi FMX: HTTPClient asynchronous Post works in Windows, Fails in Android

  

I’m using THTTPClient in Delphi 10.3 to perform a POST operation and bring data from a server. Parameters are JSON. Problem is:

When compiled as a Win32 application, it works perfectly for both when performing a asynchronous call or not.
When compiled as an Android app, it fails in the async call and works fine in the normal way.

The failure error indicates that somehow the request is not passing the json parameters (that only happens in async mode). For example: if the remote server requires to pass two parameters (say, name and age) I’ll get the remote error that “name is a mandatory field”.

My code is based on a Delphi download sample.
Is there something I should change for this to work in Android? Thanks!

Here is the relevant code:

//the content of mmoParams.Text is a JSON string:
//{“name”:”somebody”,”salary”:”1000″,”age”:”51″}

Params := TStringStream.Create(mmoParams.Text, TEncoding.UTF8);
Params.Position := 0;
// prepare the request
HTTPClient.ContentType := ‘application/json’;
HTTPClient.Accept := ‘application/json’;
if chkAsync.IsChecked then begin
// prepare the request
HTTPClient.ContentType := ‘application/json’;
HTTPClient.Accept := ‘application/json’;
// make the request and handle in the callback
HTTPResult:= HTTPClient.BeginPost(DoEndPost,edtURL.Text,Params);
end
else begin
// make the request
HTTPResponse := HTTPClient.Post(edtURL.Text,Params);
// handle response
lblStatusCode.Text := HTTPResponse.StatusCode.ToString;
mmoResult.Text := HTTPResponse.ContentAsString(TEncoding.UTF8);
end;

and here’s the callback procedure for when the async call (BeginPost) is made.

procedure TMainForm.DoEndPost(const AsyncResult: IAsyncResult);

begin
try
HTTPResponse := THTTPClient.EndAsyncHTTP(AsyncResult);
TThread.Synchronize(nil,
procedure
begin
// handle result
lblStatusCode.Text := HTTPResponse.StatusCode.ToString;
mmoResult.Text := HTTPResponse.ContentAsString(TEncoding.UTF8);
end);
finally
end;
end;

Comments are closed.