Java Reference
In-Depth Information
bis.close();
}
else {
println("failure = {rtd.failureText}");
}
rtd = null;
}
};
}
When the
requestData
function is called, the act of creating a new
RemoteText-
Document
instance with a valid URL starts the request process. When a document
has been received successfully, the data is read in as a
ByteArrayInputStream
as it would with Java, and this step is complete. At this point, it doesn't matter
what the response type is; the job of the
RemoteTextDocument
class is to get the
document to you.
However, if the returned document is XML or JSON, you'll need to parse it to
make use of it. This is where the
PullParser
class (see Figure 10.3) comes in,
which is called in Listing 10.4, highlighted in bold type. There's some more code
behind this that you need to provide, such as the code in Listing 10.5. It begins
with a custom class,
Location
, since this code illustrates parsing location data as
received from Yahoo! Web Services.
javafx.data.pull.PullParser
+forward()
+forward(integer)
+parse()
+seek(object)
+seek(object,integer)
+toString()
Figure 10.3
The PullParser Class
The real work begins with the object,
parser
, which is an instance of the
Pull-
Parser
class. It begins parsing as soon as its
input
object is set and the
parse
method is called. Because it's bound to the
ByteInputStream
object
bis
from
Listing 10.4, it has access to the data as soon as the
RemoteTextData
object pop-
ulates it. It's this bound variable that allows the code in Listing 10.4 (which
requests data from a remote server) to cooperate with the code in Listing 10.5
(which parses the received JSON document).
Search WWH ::
Custom Search