public List < GetLocationResult > search ( String q , int maxRows , Locale locale ) throws Exception { List < GetLocationResult > searchResult = new ArrayList < GetLocationResult > ( ) ; String url = URLEncoder . encode ( q , "UTF8" ) ; url = "q=select%20*%20from%20geo.placefinder%20where%20text%3D%22" + url + "%22" ; if ( maxRows > 0 ) { url = url + "&count=" + maxRows ; } url = url + "&flags=GX" ; if ( null != locale ) { url = url + "&locale=" + locale ; } if ( appId != null ) { url = url + "&appid=" + appId ; } InputStream inputStream = connect ( url ) ; if ( null != inputStream ) { SAXBuilder parser = new SAXBuilder ( ) ; Document doc = parser . build ( inputStream ) ; Element root = doc . getRootElement ( ) ; // check code for exception String message = root . getChildText ( "Error" ) ; // Integer errorCode = Integer.parseInt(message); if ( message != null && Integer . parseInt ( message ) != 0 ) { throw new Exception ( root . getChildText ( "ErrorMessage" ) ) ; } Element results = root . getChild ( "results" ) ; for ( Object obj : results . getChildren ( "Result" ) ) { Element toponymElement = ( Element ) obj ; GetLocationResult location = getLocationFromElement ( toponymElement ) ; searchResult . add ( location ) ; } } return searchResult ; }
Call the Yahoo! PlaceFinder service for a result .