2
I have a PHP API and I’m developing a Mobile App with Delphi Firemonkey, and I need to consume the JSON data that my API returns.
I would like to know how I address the client part to access this API, as I am using (for testing) a string that contains the path I want to test. Something like URL := 'http://192.168. 1.3/api/v1/attendant/1'.
So far, I’ve managed to get the JSON that my API returns, and I’m doing it this way:
1 - I created a UNIT to handle the REST request, and the code is like this:
unit uRestConnect;
interface
uses
REST.Client, REST.Json, JSON;
type
TRest = class
private
FClient : TRESTClient;
FRequest : TRESTRequest;
FResponse : TRESTResponse;
FURL : String;
public
constructor Create;
destructor Destroy;
function Result(Recurso: String; Parametros: array of string): String;
end;
implementation
{ TRest }
constructor TRest.Create;
begin
Self.FURL := 'http://192.168.1.3/api/v1/';
Self.FClient := TRESTClient.Create(nil);
Self.FRequest := TRESTRequest.Create(nil);
Self.FResponse := TRESTResponse.Create(nil);
Self.FClient.BaseURL := Self.FURL;
Self.FClient.AutoCreateParams := True;
Self.FRequest.AutoCreateParams := True;
Self.FRequest.HandleRedirects := True;
Self.FRequest.Client := Self.FClient;
Self.FRequest.Response := Self.FResponse;
end;
destructor TRest.Destroy;
begin
Self.FClient.Free;
Self.FRequest.Free;
Self.FResponse.Free;
end;
function TRest.Result(Recurso: string; Parametros: array of string): String;
var
I: Integer;
begin
for I := 0 to Length(Parametros)-1 do
begin
Recurso := Recurso + '/' + Parametros[i];
end;
Self.FRequest.Resource := Recurso;
Self.FRequest.Execute;
Result := Self.FResponse.JSONText;
end;
end.
And I have a basic form with a button and a Memo where the JSON content is being displayed that I recover from the API. The button event is that way:
procedure TForm1.btnBuscarClick(Sender: TObject);
var
Rest : TRest;
Cliente : TCliente;
Response : String;
begin
Rest := TRest.Create;
Cliente := TCliente.Create;
Response := Rest.Result('atendimentoscliente', EdIdCliente.Text);
MemoAtendimentos.Text := Response;
end;
Now, I would like to know how to get this JSON to be converted into an object in a class of my model.
What version of your Delphi?... to clarify some questions, you already have a PHP Webservice running in the address 'http://192.168. 1.3/api/v1/attendant/1' and you want to access this domain through the Mobile App?
– Jefferson Rudolf
Exactly. I’m using Delphi Berlin Update 2, and the real problem is that I don’t know how to put together a correct/Units class structure to do this, or what methods will be required. I can take the JSON that the API in PHP returns to me, but I still don’t necessarily know what to do with it, or turn it into an object of a Tcliente class, for example. This is still very confusing for me. If you have anything that can provide me with a way to proceed, I would thank you very much!
– user46439
I advise you to do a search on REST Debugger, you put the address and parameters that should be accessed on the client side, it is very simple to understand.
– Jefferson Rudolf
But I can use REST Debugger, I’m just not succeeding (or knowing) in my project to transform the JSON I take from the API into objects from my model classes.
– user46439