JavaFX, Retrieving non XML/JSON data from clouds

tango weather overcast

Usuually on JavaFX we grab data using HttpRequest from external resources on formats like JSON or XML. I showed how to get it on the post Reading Twitter with JavaFX and how to parse it using PullParser on the post Parsing a XML sandwich with JavaFX.

Another day I need to grab and interpret some plain results, not in XML nor JSON, while consuming a REST service. In this case we don’t have a well structure data so the PullParser won’t help us.

Example 1: Reading Raw Data

In this example we’ll load a plain text file served in a remote location.

var planetsRequest = HttpRequest {
    location: "http://silveiraneto.net/downloads/planets";
    onInput: function(stream: InputStream) {
        var buff = new BufferedReader(new InputStreamReader(stream));
        var line = "";
        while((line = buff.readLine())!=null){
            println(line);
        }
    }
}
planetsRequest.enqueue();

This will produce the output:

Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

Example 2: Discovering your IP Address

In this example we’ll examine how to integrate a request of a remote data in a running graphical program.

The best way to know your real IP address is asking for a remote server to look which IP made that request. It’s like calling for a friend and asking him which number appeared in his mobile. =) This server side Python script prints the IP address of who requested the page.

#!/usr/bin/env python
import os
 
print "Content-type: text/html"
print
print os.environ['REMOTE_ADDR']

In the client side, with JavaFX, we’ll load the remote value into a local variable. The ip is assigned with the value “…” and later the ipRequest will replace it with a String with the IP. The bind feature will automatically fix the GUI String text.

For the user he will see the ellipsis for a few seconds and so their IP.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.io.http.HttpRequest;
import java.io.*;
 
var ip = "...";
 
Stage {
    title: "What is my IP?" width: 250 height: 80
    scene: Scene {
        content: Text {
            x: 10, y: 30
            content: bind "My IP is {ip}"
        }
    }
}
 
var ipRequest = HttpRequest {
    location: "http://silveiraneto.net/scripts/myip.py";
    onInput: function(stream: InputStream) {
        var buff = new BufferedReader(new InputStreamReader(stream));
        ip = buff.readLine();
    }
}
ipRequest.enqueue();

You can try this JavaFX applet here.

Example 3: Reading Integer values

Until now we handled just plain Strings. But in some cases you want to get number as non structured data. In this case you need to know previously which type the data is. In the case of a web service this probably will be described in a WSDL file.

Here I’m writing a very simple service script at Zembly, a great platform for cloud computing. It’s called aplusb, it justs add the first parameter A to the second B.

if ((Parameters.a != null) && (Parameters.b!= 0)) {
return Parameters.a+Parameters.b;
}

The service is published at Zembly here where you can see more details on how to invoke it.

A simple way to invoke it on JavaFX and than getting the value as an Integer:

import java.io.*;
import javafx.io.http.HttpRequest;
 
var a = 100;
var b = 200;
var result = 0 on replace {
    println(result);
}
 
var zemblyRequest = HttpRequest {
    location: "http://zembly.net/things/1827f696529d4e6f940c36e8e79bea1c;exec?a={a}&b={b}";
    onInput: function(stream: InputStream) {
        var buff = new BufferedReader(new InputStreamReader(stream));
        result = Integer.valueOf(buff.readLine());
    }
}
zemblyRequest.enqueue();

The output will be:

0
300

The first 0 is from the first assignment on the var result. The 300 is from the webservice itself.

The same approach can be used to convert the ASCII/Unicode result from the stream to the suitable type on a variable.

7 thoughts on “JavaFX, Retrieving non XML/JSON data from clouds

  1. Good article, Silveiro. By the way, we just released a new, easier way to call services at zembly. You can read about it at http://zembly.com/ui/callAPI

    Basically, we now have a JavaFX client library, called ZCL, that makes calling zembly services almost as easy as calling them from inside zembly itself, with just a single line of code. (We also have a library for Java.) You can download the library over at Kenai: http://kenai.com/projects/zcl/pages/Home

    One nice thing about using ZCL, beyond just the simplicity, is that it uses OAuth to authenticate to zembly so that you can use your zembly keychain to call services like Twitter, Google, Yahoo, etc. without distributing all those API access keys with your application.

  2. Hi, Silveira. I’m new with JavaFX scripts (but I use to develop in Java), and I’m having some fun with some tutorials. Yours is quite good, but I got an error when trying to run your examples. I use Eclipse, and it doesn’t let me run your raw data reader because it doesn’t find the method enqueue(). I’m sure that’s a newbie mistake, easy to solve, but I haven’t found anything helpful in Internet.
    Any help from you would be really appreciated.
    And congratulations for your site, is quite interesting and educative.

  3. Hi @Daniel. Probably the problem is that some of my codes here in the blog are no longer consistent with the current JavaFX’s syntax and API. Probably minor adjustments should be done in the code in order to make it work properly.

  4. Thanx for your quick reply. I’m working on finding a solution, or a clue to adapt it to the new specifications.

    Even if your scripts could be outdated, I still think your scripts are really good to begin to learn, so I’ll visit your blog regularly :-)

  5. You’re wellcome @Daniel. Please, if you modify a source in order to make it work in newest versions of JavaFX, feel free post them in the commentaries and I’ll update the post.

  6. Hi,

    The enqueue() command was deprecated in version 1.2.
    Now you must use the start() command.

    Here are the corrections:

    planetsRequest.enqueue(); -> planetsRequest.start();
    ipRequest.enqueue(); -> ipRequest.start();
    zemblyRequest.enqueue(); -> zemblyRequest.start();

    ;)

    1. This does look appropriate, but i’m not seenig any changes to the admin CSS enqueues.. i’ve even tried modifying the admin css functions directly(temporarily for testing) and i’m still not seenig changes, which leads me to suspect the function is firing an enqueue instead of printing the CSS links where the provided filter gets called(see the comments above the wp_admin_css function in general-template.php). I have a fresh head this morning though, so i’ll keep digging and testing.. Dec 5 ’10 at 10:01

Leave a Reply

Your email address will not be published. Required fields are marked *