package dn;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
* Local Search Midlet
*
* @author Deepak Nadig
*/
public class LocalSearch extends MIDlet implements CommandListener
{
String debug = "none";
Display display;
Form mainForm;
Form aboutForm;
Form resultsForm;
Form rawResultsForm;
String appIDLabel = "AppID";
String queryLabel = "Query";
String resultsLabel = "Results";
String startLabel = "Start";
String zipLabel = "ZIP";
String appID = "YahooDemo";
String query = "pizza";
String results = "2";
String start = "1";
String zip = "95135";
Command searchCommand;
Command rawResultsCommand;
Command backCommand;
Command exitCommand;
Command aboutCommand;
LocalSearchResponseHandler lsrh;
/**
*
*/
public LocalSearch()
{
display = Display.getDisplay(this);
// Commands. Son los botones que se agregan al menu del celular
exitCommand = new Command("Exit", Command.EXIT, 0);
searchCommand = new Command("Search", Command.SCREEN, 2);
backCommand = new Command("Back", Command.BACK, 0);
rawResultsCommand = new Command("Raw", Command.SCREEN, 2);
aboutCommand = new Command("About", Command.HELP, 2);
// Main form
mainForm = new Form("Local Search");
mainForm.append("Local Search");
mainForm.append(new TextField(appIDLabel, appID, 40, TextField.ANY));
mainForm.append(new TextField(queryLabel, query, xTextField.ANY));
mainForm.append(new TextField(resultsLabel, results, 4,
TextField.NUMERIC));
mainForm.append(new TextField(startLabel, start, 4, TextField.NUMERIC));
mainForm.append(new TextField(zipLabel, zip, 5, TextField.NUMERIC));
mainForm.addCommand(exitCommand);
mainForm.addCommand(searchCommand);
mainForm.addCommand(aboutCommand);
mainForm.setCommandListener(this);
// Search results form
resultsForm = new Form("Formatted");
resultsForm.addCommand(rawResultsCommand);
resultsForm.addCommand(exitCommand);
resultsForm.addCommand(backCommand);
resultsForm.setCommandListener(this);
// Raw results form
rawResultsForm = new Form("Raw");
rawResultsForm.addCommand(exitCommand);
rawResultsForm.addCommand(backCommand);
rawResultsForm.setCommandListener(this);
// About form
aboutForm = new Form("About");
aboutForm.addCommand(backCommand);
aboutForm.setCommandListener(this);
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
/**
* Local Search Midlet
*
* @author Deepak Nadig
*/
public class LocalSearch extends MIDlet implements CommandListener
{
String debug = "none";
Display display;
Form mainForm;
Form aboutForm;
Form resultsForm;
Form rawResultsForm;
String appIDLabel = "AppID";
String queryLabel = "Query";
String resultsLabel = "Results";
String startLabel = "Start";
String zipLabel = "ZIP";
String appID = "YahooDemo";
String query = "pizza";
String results = "2";
String start = "1";
String zip = "95135";
Command searchCommand;
Command rawResultsCommand;
Command backCommand;
Command exitCommand;
Command aboutCommand;
LocalSearchResponseHandler lsrh;
/**
*
*/
public LocalSearch()
{
display = Display.getDisplay(this);
// Commands. Son los botones que se agregan al menu del celular
exitCommand = new Command("Exit", Command.EXIT, 0);
searchCommand = new Command("Search", Command.SCREEN, 2);
backCommand = new Command("Back", Command.BACK, 0);
rawResultsCommand = new Command("Raw", Command.SCREEN, 2);
aboutCommand = new Command("About", Command.HELP, 2);
// Main form
mainForm = new Form("Local Search");
mainForm.append("Local Search");
mainForm.append(new TextField(appIDLabel, appID, 40, TextField.ANY));
mainForm.append(new TextField(queryLabel, query, xTextField.ANY));
mainForm.append(new TextField(resultsLabel, results, 4,
TextField.NUMERIC));
mainForm.append(new TextField(startLabel, start, 4, TextField.NUMERIC));
mainForm.append(new TextField(zipLabel, zip, 5, TextField.NUMERIC));
mainForm.addCommand(exitCommand);
mainForm.addCommand(searchCommand);
mainForm.addCommand(aboutCommand);
mainForm.setCommandListener(this);
// Search results form
resultsForm = new Form("Formatted");
resultsForm.addCommand(rawResultsCommand);
resultsForm.addCommand(exitCommand);
resultsForm.addCommand(backCommand);
resultsForm.setCommandListener(this);
// Raw results form
rawResultsForm = new Form("Raw");
rawResultsForm.addCommand(exitCommand);
rawResultsForm.addCommand(backCommand);
rawResultsForm.setCommandListener(this);
// About form
aboutForm = new Form("About");
aboutForm.addCommand(backCommand);
aboutForm.setCommandListener(this);
}
// Primer evento que se ejecuta al inicio del sistema
protected void startApp() throws MIDletStateChangeException
{
display.setCurrent(mainForm);
}
protected void startApp() throws MIDletStateChangeException
{
display.setCurrent(mainForm);
}
// Evento que se ejecuta cuando se para la aplicacion
protected void pauseApp()
{
}
protected void pauseApp()
{
}
// Evento que se ejecuta al cerrar la aplicacion
public void destroyApp(boolean unconditional)
{
}
private void retrieveFormData(Form form)
{
int formSize = form.size();
for (int i = 0; i < formSize; i++)
{
Item item = form.get(i);
String label = item.getLabel();
if ((label != null) && (item.getLabel().equals(appIDLabel)))
{
appID = ((TextField) item).getString();
}
else if ((label != null) && (item.getLabel().equals(queryLabel)))
{
query = ((TextField) item).getString();
}
else if ((label != null) && (item.getLabel().equals(resultsLabel)))
{
results = ((TextField) item).getString();
}
else if ((label != null) && (item.getLabel().equals(startLabel)))
{
start = ((TextField) item).getString();
}
else if ((label != null) && (item.getLabel().equals(zipLabel)))
{
zip = ((TextField) item).getString();
}
}
}
// Evento que escucha los eventos, si preciona exit o search el eligue
public void commandAction(Command c, Displayable s)
{
if ((c == exitCommand))
{
destroyApp(false);
notifyDestroyed();
}
else if (c == searchCommand)
{
String searchResults = null;
try
{
retrieveFormData(mainForm);
searchResults = doYahooLocalSearch();
}
catch (Exception e)
{
searchResults = "Exception: " + e.getMessage();
}
resultsForm.deleteAll();
resultsForm.append(searchResults);
display.setCurrent(resultsForm);
}
else if (c == rawResultsCommand)
{
rawResultsForm.append(lsrh.toRawString());
display.setCurrent(rawResultsForm);
}
else if (c == backCommand)
{
display.setCurrent(mainForm);
}
else if (c == aboutCommand)
{
aboutForm.append("app_id - application id\nquery - search string\nresults - # of results\nstart - first entry of results\nzip - zip code\n\n\nDN(2005)");
display.setCurrent(aboutForm);
}
}
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
{
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
int rc;
try
{
c = (HttpConnection) Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.1");
c.setRequestProperty("Content-Language", "en-US");
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
// Get the ContentType
String type = c.getType();
return parseXML(is);
}
catch (ClassCastException e)
{
throw new IllegalArgumentException("Not an HTTP URL");
}
catch (SAXException e)
{
throw new IOException("SAX exception");
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
}
}
private LocalSearchResponseHandler parseXML(InputStream is) throws SAXException, IOException
{
// Get an instance of the SAX parser factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// Get an instance of the SAX parser
SAXParser saxParser = null;
try
{
saxParser = factory.newSAXParser();
}
catch (ParserConfigurationException pce)
{
throw new IOException(pce.getMessage());
}
// Parse the input XML document stream, using my event handler
lsrh = new LocalSearchResponseHandler();
saxParser.parse(is, lsrh);
return lsrh;
}
}
public void commandAction(Command c, Displayable s)
{
if ((c == exitCommand))
{
destroyApp(false);
notifyDestroyed();
}
else if (c == searchCommand)
{
String searchResults = null;
try
{
retrieveFormData(mainForm);
searchResults = doYahooLocalSearch();
}
catch (Exception e)
{
searchResults = "Exception: " + e.getMessage();
}
resultsForm.deleteAll();
resultsForm.append(searchResults);
display.setCurrent(resultsForm);
}
else if (c == rawResultsCommand)
{
rawResultsForm.append(lsrh.toRawString());
display.setCurrent(rawResultsForm);
}
else if (c == backCommand)
{
display.setCurrent(mainForm);
}
else if (c == aboutCommand)
{
aboutForm.append("app_id - application id\nquery - search string\nresults - # of results\nstart - first entry of results\nzip - zip code\n\n\nDN(2005)");
display.setCurrent(aboutForm);
}
}
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
{
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
int rc;
try
{
c = (HttpConnection) Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.1");
c.setRequestProperty("Content-Language", "en-US");
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
// Get the ContentType
String type = c.getType();
return parseXML(is);
}
catch (ClassCastException e)
{
throw new IllegalArgumentException("Not an HTTP URL");
}
catch (SAXException e)
{
throw new IOException("SAX exception");
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();
if (c != null)
c.close();
}
}
private LocalSearchResponseHandler parseXML(InputStream is) throws SAXException, IOException
{
// Get an instance of the SAX parser factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// Get an instance of the SAX parser
SAXParser saxParser = null;
try
{
saxParser = factory.newSAXParser();
}
catch (ParserConfigurationException pce)
{
throw new IOException(pce.getMessage());
}
// Parse the input XML document stream, using my event handler
lsrh = new LocalSearchResponseHandler();
saxParser.parse(is, lsrh);
return lsrh;
}
}
No hay comentarios:
Publicar un comentario