I'm currently trying to create a single unified unit for the purposes of threading. I've developed a web API which I intend to use with multiple applications. My intent with the thread is to get the data from the API without using the main application thread. (i.e. offloading into a thread for the purposes of good UX). The output will be JSON retrieved using a POST request to the API, and parsing will be performed from there.
I've got the unit all written up but I'm struggling to figure out how I retrieve the output in my actual application/transfer the JSON from the thread to the application.
I think what I need is to add an 'output variable' somewhere, but I'm unsure how I do that when this unit is standalone - Normally I'd write it all in one unit and of course I could output it to a global variable there.
Here's my thread unit;
unit TCAPI_ThreadLib;
interface
uses
FMX.Types, FMX.Objects, {FMX.Dialogs,} IdHTTP, System.Classes;
type
TTCAPI_GetJson_Thread = class(TThread)
private
APIHTTP : TIdHTTP;
FApiEmail, FApiId, FApiPassword, FCommand, FCommandParams : String;
protected
procedure Execute; override;
procedure Finished;
public
constructor Create(SEmail, SApiID, SAPIPassword : String);
destructor Destroy; override;
var
FDevGUID, FDevFN, FDevPlatform, FDevModel : String;
FApiDataResult : String;
Const
APIBase : String = 'http://my-web-api-url.com/api.php';
end;
implementation
{ TTCAPI_GetJson_Thread }
constructor TTCAPI_GetJson_Thread.Create(SEmail, SApiID, SAPIPassword: String);
begin
inherited Create(True);
APIHTTP := TIdHTTP.Create(nil);
FApiEmail := SEmail;
FApiID := SApiID;
FApiPassword := SApiPassword;
FreeOnTerminate := True;
end;
destructor TTCAPI_GetJson_Thread.Destroy;
begin
APIHttp.Free;
inherited;
end;
procedure TTCAPI_GetJson_Thread.Execute;
var
RcvStrm : TStringStream;
TmpClass : String;
ApiCommand : String;
_Params : TStringList;
begin
inherited;
RcvStrm := TStringStream.Create;
_Params := TStringList.Create;
_Params.Add('API_ID='+FApiID);
_Params.Add('API_EMAIL='+FApiEmail);
_Params.Add('API_PASSWORD='+FApiPassword);
APICommand := APIBase;
try
APIHTTP.Post(APICommand, _Params, RcvStrm);
FApiDataResult := RcvStrm.DataString;
finally
RcvStrm.Free;
_Params.Free;
end;
Synchronize(Finished);
end;
procedure TTCAPI_GetJson_Thread.Finished;
begin
// ShowMessage(FApiDataResult);
end;
end.
If I add FMX.Dialogs
and use a ShowMessage(FApiDataResult)
call in the thread unit, it shows the JSON in a message box - I need to figure out how to get that JSON into a variable within the scope of my applications main unit/form and how to actually tell the application that the thread has finished so that I can begin parsing.
I'm sure it's simple to do, but I'd appreciate any help!