RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Invoke RESTful Web Services from JavaFX : Page 2

Learn how to access RESTful web services from a JavaFX application.


Define a Container Data Structure

To begin, in a Flight.fxfile, define a JavaFX class that corresponds to the structure of the flight element:

public class Flight{

 public var number: String;
 public var time: String;
 public var direction: String;
 public var carrier: String;
 public var destination: String;
 public var origin: String;


Accessing the Service

In a main JavaFX application (Main.fx) or in a location of your design, invoke a RESTful web service from a function loadFlightsSchedules using the JavaFX object HttpRequest (see Listing 1). All the pertinent properties and functions are declared within the declarative scope of HttpRequest.

Notice that the URL of the web service is specified in a location variable and the method GET (although JavaFX supports PUT and POST as well) is specified in the variable of the same name. You execute the call to the web service itself with request.enqueue();. If successful, the results are processed by the method onInput: function(input: java.io.InputStream).

Parsing the Results

In the function onInput: function(input: java.io.InputStream), Listing 1 handled the XML results from a web service by invoking the FlightsParser, which is a custom extension of the JavaFX object javafx.data.pull.PullParser. PullParsers supports direct query- and event-based XML parsing (similar to the SAX parser).

To load the elements from the parsed XML into a Flight object, you can use the (abbreviated) code in Listing 2.

You're Done

This quick 10-Minute Solution has demonstrated how to use the GETmethod to invoke a RESTful web service and how to parse its results into a JavaFX object with minimal error handling.

For more details on web services processing and parsing of other data types, review the JavaFX objects javafx.io.http, javafx.data.pull, and javafx.data.xml.

Edmon Begoli a software architect with 14 years of professional experience on large commercial and federal software projects. He is a member of the R&D staff at Oak Ridge National Laboratory.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date