In this demonstration, I will show you how to create a cross-platform real-world weather application using WeatherStack API. Delphi FireMonkey applications can target Windows, macOS, and Linux on the desktop. Additionally, they can also target Android and iOS on mobile.
Here is what we create in today’s demonstration:
What is the WeatherStack API?
The WeatherStack API is one of the market-leading weather data services for any application. It has real-time functionality and historical weather information and supports all major programming languages. You can retrieve accurate weather information for any location instantly.
Yes, we have been exploring and learning about WeatherStack API. Here you can see other posts which demonstrate the best features of the WeatherStack API. Moreover, flexibility over other similar web services in the market.
How to start with WeatherStack API?
It is all simple! Just head over to the WeatherStack official web page and sign up for a free subscription plan and get your API Access Key from the dashboard.
And here is a sample API request:
Be sure to check out the official documentation before starting to use the API!
How to create a cross-platform application with the same codebase that runs on Windows, macOS, iOS, Android, and Linux?
With Delphi FireMonkey, you can create a native and cross-platform application from a single code base in no time!
FireMonkey Cross-Platform framework offers you to build native applications with 5x faster productivity. With hundreds of components and libraries, you can design and create fully functional applications swiftly.
Furthermore, with the new FireMonkey App Low Code Wizard, you can build and generate a cross-platform project by selecting available options in the wizard.
FireMonkey App Low Code Wizard can create multiple screens and demonstrated coding best practices. Besides, if you need customization you can easily code it with Delphi!
How to start with Delphi FireMonkey?
Here you can start learning Delphi FireMonkey with the free Bootcamp session which covers most of the essential things you need to start developing cross-platform applications with Delphi FireMonkey!
How to create a cross-platform weather app with Delphi using WeatherStack API?
In this demonstration, I will create this app and show you how to integrate with WeatherStack API.
How to connect to RESTful web services in Delphi?
REST Debugger is a powerful environment to play with RESTful web services. You can start REST Debugger from the Tools menu of the IDE.
Here is how we can establish a connection:
And after successfully connecting to the RESTful web service, we can just copy configured REST Client components by clicking the Copy components button and paste them into our project.
Here is our application user interface structure:
The user interface is responsive and flexible. I have tried to utilize best practices in UI design.
Now, you need to paste the copied components from the REST Debugger and create an OnClick event on the Search button.
And paste this code to the OnClick event.
procedure TFormMain.BtnRequestClick(Sender: TObject);
RESTClient1.Accept := 'application/json';
RESTClient1.AcceptCharset := 'UTF-8, *;q=0.8';
RESTClient1.BaseURL := 'http://api.weatherstack.com/current';
// give city name in the URL query
RESTResponse1.ContentType := 'application/json';
// parse json and open the current object
JSONObject := TJSONObject.ParseJSONValue(RESTResponse1.Content)
JSONValue := JSONObject.Get('current').JSONValue;
LblTemp.Text := JSONValue.GetValue<String>('temperature') + 'º';
LblCity.Text := EdtCityName.Text;
// get the first element from the weather description array
JSONArray := JSONValue.GetValue<TJSONArray>('weather_descriptions');
LblType.Text := JSONArray.Items.Value;
// download weather icon and set to TImage component
var MemoryStream := TMemoryStream.Create;
var HttpClient := TNetHTTPClient.Create(nil);
var HTTPReq := TNetHTTPRequest.Create(nil);
HTTPReq.Client := HttpClient;
// get weather icon URL from weather_icons array
JSONArray := JSONValue.GetValue<TJSONArray>('weather_icons');
var ImgURL := JSONArray.Items;
// download image
// load streamed data to TImage
// little more details on the weather
LstBoxItemHumidity.Text := 'Humidity: ' +
LstBoxItemFeelsLike.Text := 'Feels like: ' +
JSONValue.GetValue<String>('feelslike') + 'º';
LstBoxItemCloudCover.Text := 'Cloud cover: ' +
LstBoxItemIsDay.Text := 'Is day: ' +
As you can see, we are reconfiguring the request URL based on the input by the user.
Then we are parsing JSON and opening the current object. The current object has the main weather data, for instance: temperature, humidity, weather icon, and more.
After that, we are getting the first element from the weather description array which holds a summary of the weather info.
Then we are parsing the weather icon and downloading the image and setting it to the TImage component.
I hope everything in this code is clear. I am pretty sure you can learn so much about JSON manipulation in Delphi and creating cross-platform applications with Delphi FireMonkey.
Here is our application in action:
Head over and check out the full source code here: https://github.com/MuminjonGuru/Mastering-FireMonkey-Delphi/tree/master/WeatherAppFMXWeatherStackAPI
Are you ready to build apps using a quick and cost-effective JSON API for your weather data requirements?