O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Yahoo! Hacks
By Paul Bausch
October 2005
More Info

HACK
#80
Create a Yahoo! Local MIDlet
Use Java for mobile devices to access the Yahoo! Local API on a mobile device
The Code
[Discuss (0) | Link to this hack]

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();
	}

}


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.