I have a php script in my server that is very simple because he has a single task: store in a sql database two data.
http://mydomain.it/test.php?id=285&numero=504;
If you open this url in the browser, you are going to add a row in an sql table that looks like this
I want to be able to store the data in the database using firemonkey too, and I have placed in my app two TEdits and a TButton. Clicking on the button this code executes:
var url: string;
H: TIdHttp;
SS: TStringStream;
begin
url := 'http://mydomain.it/test.php?id='+IntToStr(a)+'&numero='+IntToStr(b);
H := TIdHttp.Create(nil);
try
SS := TStringStream.Create;
try
try
H.Post(url, SS);
except
on E:Exception do
ShowMessage(e.Message);
end;
finally
SS.Free;
end;
finally
H.Free;
end;
The code above works very well because it takes only 1 second to run and I am trapping the exceptions (just for a debug purpose). I have two main questions:
This is the first time that I use indy and from what I have understood this is a good way to do what I need. I am not sure if I have placed the
try-except
in the correct place; is it?What about if internet connection is slow? Should I use something like the code below?
code
var process: array of ITask;
begin
SetLength(process, 1);
process[0] := TTask.Create(
procedure
var url: string;
H: TIdHttp;
SS: TStringStream;
begin
url := 'http://mydomain.it/test.php?id='+IntToStr(a)+'&numero='+IntToStr(b);
H := TIdHttp.Create(nil);
try
SS := TStringStream.Create;
try
try
H.Post(url, SS);
except
on E:Exception do
ShowMessage(e.Message);
end;
finally
SS.Free;
end;
finally H.Free;
end;
end
);
process[0].Start;
TTask.WaitForAll(process);
Label1.Text := 'done!!';
end;
The code in section 2 has some issue and it doesn't work. Any idea?