Accessing Flickr's REST API From JavaFX

By Rakesh Menon, November 6, 2008

InterestingPhotos is a simple JavaFX application that displays "interesting" photos retrieved from Flickr using Flickr's REST API.

Information about "interesting" photos is obtained by performing an HTTP GET request on Flickr's flickr.interestingness.getList API call by using the JavaFX asynchronous HTTP API (javafx.io.http.HttpRequest). The response document is parsed by using the JavaFX XML pull parser (javafx.data.pull.PullParser) to extract information about the photos. The photos are then fetched and displayed in Stage as a grid. The user can click the thumb image to load the normal image. Clicking the normal image again returns the UI to thumb image view.

Do not use the api-key used by this sample. Obtain your own key from flickr.com

The response XML document contains a list of information about available photos. The document contains the following information about each photo:

  • id
  • owner
  • secret
  • server
  • farm
  • title

This information is used to construct a URL to retrieve and then display each photo. The ImageView class knows how to retrieve and display an image using a URL. Once the photo is retrieved, it is displayed in Scene of Stage through the use of data binding.

A sequence of photo objects is constructed by parsing the response XML. A grid of ImageView is added to Scene of Stage. If the user clicks this thumb photo, the normal photo is loaded and shown. Clicking the normal photo restores the thumb photo grid. The user can navigate to the next or previous set of photos by clicking << and >> arrow buttons.

Flickr Web Services and Key Requirement

Anyone who wants to use Flickr Web Services must register with Flickr to receive a key. A key is an opaque string that must be included in all communication to Flickr's web servers when using this API.

At the top of the Main.fx file, a constant apiKey is declared that is initialized with value returned by FX argument "flickr_apikey". Go to http://developer.yahoo.com/flickr to register and receive App ID. Use the key value to initialize the constant apiKey before running the program. The App ID can be passed as argument to application.

Source Code
 // TODO: get an apiKey from http://developer.yahoo.com/flickr
 def apiKey = FX.getArgument("flickr_apikey");

You can also directly assign the API-Key string as shown below and rebuild the application

Source Code
def apiKey = "<your flickr api key>";

If you are using proxy-server for connecting to internet, please follow setup as specified in "Java Networking and Proxies".

References