Connect an FMX app to a remote server

Connect an FMX app to a remote server

Within the process of mobile app development, quite often there is a necessity to get data from a remote server and later process them. To solve this task, it is required to fulfill the following steps:

  • To get connected to a remote server via an API
  • To receive data in one of the available formats (usually it will be JSON or XML)
  • To process the received data
  • To present data in a human-readable format

In this series of posts we will show how to perform each step based on the example of a multidevice application written with the help of FMX, the BlockChain.API server and ready-made Embarcadero Delphi components.

Step 1. Connection to a remote server via an API

The connection to the BlockChain.API server will be executed via the REST technology that has become a standard for data exchange between an app and a remote server thanks to the fact that:

  • client apps, regardless of their type (mobile, web, etc.) can use the same API for sending requests and getting information;
  • REST allows standardizing the development of services and facilitates the work with them for developers of apps on a client’s side.

The client app sends a request to the server (usually it is a GET or POST request) and gets a response in a particular format. Quite often it will be JSON or XML.

JSON has a more simplified structure if we compare it with XML which can be seen in the table below.

Thanks to its structure, data sending and parsing for the JSON format are significantly faster than in the case of XML.

In our situation, we will send a GET request to the server.

For getting connected to the remote server and receiving data (via the GET request) we will use a standard non-visual component TNetHttpClient. It doesn’t require any settings for being used. It is necessary just to take it from the Component Palette and place it on the form.

For checking the working capacity and the correctness of formulating API commands, we can make a testing connection and send a request to the BlockChain.Api server in a web browser.

If everything is done correctly and the connection with the server has been successfully established, we can proceed to the next step and get the necessary data.

Step 2. Pull data from the server

The response received from the server will be saved in the TMemoryStream object.

NetHTTPClient1.Get('https://api.blockchain.info/charts/transactions-per-second',LResponse);

The StreamToString function is used for getting text information from the TMemoryStream stream.

Read about Data parsing & visualization in the next post.

At Softacom, we are always ready to provide you with any professional assistance in building innovative software products.
Contact us for more information!