Académique Documents
Professionnel Documents
Culture Documents
PUBLISHED By : Torry.net
All the development will be carried out on Windows platform as a 32bit
application, only the result will be adapted for target device or platform. Our
application allows to enter ID and returns business record for given company
(business name, address and more) in JSON format result will be processed
and displayed within an application.
Prerequisities :
In first step, create a new application, i.e. select Multi Device Application and Header/Footer
layout. As soon as first step is done, save basic app anywhere you like.
Several last versions allow Firemonkey to use View technology, where user can define exact
look of the app on various resolutions (we define Master and customize it then). Working with
Master only will be enough for us because application adapts itself and detailed resolution
settings won't be necessary.
View options Master Enoughs
Now is pretty important to set Layout it means assure app to customize target device in case if
layout for various resolutions is not set. We can choose among many Layout options (you can
imagine as a panel with certain behavior), but let's use TListBox component (another reasonable
option is MultiView or TListView).
To be frank, do forget VCL based TListBox, this is strongly different solution. Each Item within the list can be a
component with type at pleasure, with different style, and can contain further miscellaneous components.
ListBox in action
Choose ListBox and place it onto the Form with setting Align=alClient.
Titles
Now we will insert particular items which will represent a cornerstone of our user interface. Starting with title
(I've already inserted it there).
In the next step add an Item on ListBox through context menu:
Please observe tree structure regarding TEdit. FireMonkey allows to combine components
as you like and as you need. Styles what are able to "do miracles" are additional value
too.
Well, we've created basic user interface. This UI will be adapted according to target
platform while compiling.
Main Programme
At this moment, we can compile application for target platform, i.e. for Windows, iOS, Mac OSX or
Android. I strictly recommend to develop completely using Windows target and tune up e.g. for
Android it's more comfortable regarding speed of compiling and deployment.
uses
// Windows,
System.Net.httpclient,
System.JSON;
{$R *.fmx}
procedure THeaderFooterForm.Button1Click(Sender:
TObject);
var
oClient: THTTPClient;
oStream: TStringStream;
iStatus: Integer;
sResponse: string;
oJsonValue: TJSONValue;
s: string;
o: TObject;
const
ciLen = length('ico_back('); // odpoved obsahuje kus JS
begin
oClient := THTTPClient.Create;
oStream := TStringStream.Create;
try
Next page
// pro ladeni
iStatus := oClient.Get('http://data.fin.cz/json/subject/45274649.js', oStream, nil).StatusCode;
// iStatus := oClient.Get('http://data.fin.cz/json/subject/'+edtICO.Text+'.js', oStream,
nil).StatusCode;
iStatus := iStatus div 200;
if iStatus <> 1 then
begin
edtName.Text := 'Error:'+iStatus.ToString;
Exit;
end;
sResponse := oStream.DataString.Substring(ciLen);
sResponse := sResponse.Remove(Length(sResponse) - 1);
finally
oStream.DisposeOf; // ignoruj ARC
oClient.DisposeOf;
end;
(*
'ico_back({id:''25247484'',count:1,name:''AB - AKCIMA, s.r.o.'',
url:''http://rejstrik-firem.kurzy.cz/25247484/ab-akcima-sro/'',
addr_main:{city:''Cheb'',citypart:''Cheb'',street:''Havl\u00ED\u010Dkova 1803/2'',
zip:''35002'',no:''1803/2'',code:{city:''554481'',district:''CZ0411''}},
vat:{id:''CZ25247484''},date:{start:''2000-08-03'',last:''2014-09-01'',end:null}})'
*)
sResponse := NormaliseJSON(sResponse);
// Windows.MessageBox(0, Pchar(sResponse), nil, 0);
oJsonValue := TJSONObject.parseJSONValue(sResponse);
if oJsonValue is TJSONObject then
begin
s := TJSONObject(oJsonValue).Values['id'].Value;
edtName.Text := TJSONObject(oJsonValue).Values['name'].Value;
o := TJSONObject(oJsonValue).Values['addr_main'];
if o is TJSONObject then
begin
edtAdr1.Text := TJSONObject(o).Values['street'].Value;
edtAdr2.Text := TJSONObject(o).Values['citypart'].Value;
edtPost.Text := TJSONObject(o).Values['zip'].Value; Next page
end;
end;
First part of code downloads JSON, second one goes through and returns requested
information. It's straightforward except NormaliseJSON which partially edits returned JSON.
function THeaderFooterForm.NormaliseJSON(const s: string): string;
var
c: char;
bInQuote: Boolean;
begin
Result := '';
bInQuote := False;
for c in s do
begin
if c = '''' then
begin
bInQuote := not bInQuote;
Result := Result + '"';
continue;
end;
if bInQuote then
Result := Result + c
else
if CharInSet(c, ['{', ',']) then
Result := Result + c+ '"'
else
if CharInSet(c, [':']) then
Result := Result + '"'+c
else
Result := Result + c
end;
Result := Result + '';
end;
Try it out for Windows, translate for Android and we've done.
Notes
Be aware of zero-terminated strings in mobile applications if you do not want to use switch which
turns it off, simply remember that all the functions called by String.xxxx start from zero.
Size of Application
Remove all unnecessary parts from your app via the Project Manager. Final application
compile as a Release, package size is in my case 6 119 782 bytes.
Address : Prostjoviky 79
79803, PLUMLOV
(District Prostjov, Olomouc region)
Email:yamaco@yamaco.cz
Web: Torry.net
Thank You