Location-Based Coffee Shop Search With Yahoo Webservices
LocalSearch is a simple JavaFX application that searches Yahoo! Local Search Web Services for local restaurants. Because the service can provide results in either XML or JSON, examples for parsing both are provided. The results are displayed in a simple GUI, which shows the details of shop such as, name, address, phone, average rating and a review excerpt. The user can scroll through the list of shops using left and right arrow button.
Understanding the Code
Results are shown to the user using the javafx.stage and javafx.scene classes. The query is processed and a list of coffee shops is obtained. The list of shops can be navigated by using << and >> arrow buttons. The details of selected shop are displayed. To initiate a new search, the user types the zip code in the text box and clicks the Search button or presses the Enter key.
The information is retrieved by performing an HTTP GET request using the JavaFX asynchronous HTTP API (javafx.io.http.HttpRequest). The document in the response is parsed by using the JavaFX XML or JSON pull parser (javafx.data.pull.PullParser).
Constraints provided in the requests for local restaurant information are:
- Type of restaurant, for example coffee, pizza etc.
- Location by zip code
- The number sets of restaurant information expected in the response
The response contains an XML or JSON document that has a list of restaurants including the following information about each one:
- Name
- Address
- Phone number
- GPS coordinates
- Distance from the given location, indicated by zip code
- Categories of food that the restaurant serves
- Customer ratings
The JSONPullParser.fx file contains the specifics of requesting and parsing the JSON format response document. The XMLPullParser.fx file has the specifics for requesting and parsing the XML format response document. Both files contain extensions of javafx.data.pull.PullParser that are used to extract the location, category and rating information about each restaurant from the response documents.
At the top of the Main.fx file, a constant appid is declared that is initialized with value returned by FX argument "yahoo_appid". Go to http://developer.yahoo.com/search/ to register and receive the App ID. Use the App ID to initialize the constant appid before running the program. The App ID can be passed as argument to application.
def dataType = "xml"; // select data type -> xml or json def appid = FX.getArgument("yahoo_appid"); // get app-id from http://developer.yahoo.com/search/
You can also directly assign the app-id string as shown below and rebuild the application
def appid = "<your yahoo app id>";
The Main.fx file has the Scene configuration and defines the Result, Category and Rating classes. These classes exist to contain parsed information and are used by the parser extension classes in JSONPullParser.fx and XMLPullParser.fx. The RequestHandler class extends javafx.io.http.HttpRequest and provides a variable processResults which is initialized with different functions depending on the type of response document to be parsed.
If you are using proxy-server for connecting to internet, please follow setup as specified in "Java Networking and Proxies".
Rakesh Menon
