WEBINAR:
On-Demand
Building the Right Environment to Support AI, Machine Learning and Deep Learning
Define a Container Data Structure
To begin, in a
Flight.fx file, 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
GET method 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.