The Code
The code for this hack is organized into three files:
- LocalSearch.java
MIDlet described in more detail below
- LocalSearchResponseHandler.java
SAX event handler class used to parse the response from Yahoo! Local Search
- URLUTF8Encoder.java
Utility class used to encode special characters in the URL
TIP
You can download the complete code for this hack at http://mobile-j2me.blogspot.com.
The code in LocalSearch.java handles most of the work. This file implements the MIDlet interface requirements, describes the application interface, and handles any user interaction. Here's a look at the key methods in LocalSearch.java:
// package/import statements
public class LocalSearch extends MIDlet implements CommandListener
{
// Data members
public LocalSearch()
{
// Create the UI forms and the buttons/commands
}
protected void startApp() throws MIDletStateChangeException
{
// Set the display to the main form
}
protected void pauseApp()
{
// Not used
}
protected void destroyApp(boolean unconditional)
{
// Not used
}
public void commandAction(Command c, Displayable s)
{
// Handle commands from the UI
// Retrieve data entered by the user
// Invoke Yahoo! Local Search (see doYahooLocalSearch())
// Parse the response
// Display the results or error to the user
}
// Invoke Yahoo Local Search and get response
private String doYahooLocalSearch() throws IOException
{
// Invoke Yahoo Local Search
String url =
"http://api.local.yahoo.com/LocalSearchService/V1/localSearch?"
+ "appid="
+ appID
+ "&query="
+ URLUTF8Encoder.encode(query)
+ "&results="
+ results
+ "&start="
+ start
+ "&zip=" + zip;
return(postViaHttpConnection(url).toFormattedString());
}
private LocalSearchResponseHandler postViaHttpConnection(String url)
throws IOException
{
// POST a HTTP request with the URL
// Retrieve the response (XML document)
// Invoke parseXML()
}
// Parse the response XML document using SAX and retrieve
// the needed information
private LocalSearchResponseHandler parseXML(InputStream is) throws
SAXException, IOException
{
}
}
The following code is a SAX handler that retrieves the title, phone number, and address from each of the businesses that Yahoo! returns. Here are the key methods in LocalSearchResponseHandler.java:
// package/import statements
class LocalSearchResponseHandler extends DefaultHandler
{
// Data members
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
// Keep track of QNames
qNameStack.push(qName);
// Get the # of results available and the # of results
// returned in the response
if (qName.equals(ELEM_RS))
{
totalResultsAvailable=attributes.getValue(ATTR_TRA);
totalResultsReturned = attributes.getValue(ATTR_TRR);
}
currentElementContent = "";
}
public void characters(char[] ch, int start, int length)
throws SAXException
{
currentElementContent = currentElementContent + new String(ch, start,
length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException
{
// Get current QName
qName = (String) qNameStack.peek();
// Get the title, phone, address of the listing
if (ELEM_TITLE.equals(qName))
{
titles.addElement(currentElementContent);
}
else if (ELEM_PHONE.equals(qName))
{
phones.addElement(currentElementContent);
}
else if (ELEM_ADDRESS.equals(qName))
{
addresses.addElement(currentElementContent);
}
// Pop QName, since we are done with it
qNameStack.pop();
}
}