
One of the most common techniques used by MIDlets is scraping web pages. The situation arises when a MIDlet wants some piece of information from a website that was designed for humans, not computers. Common examples are retrieving stock quotes, weather forecasts, yellow pages, and news stories.
The following simple example demonstrates how a web page can be retrieved. You can add your own algorithms for scraping. In other words, process the data and extract just the information you want.
Scrape Source Code
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class scrape extends MIDlet implements CommandListener
{
/**
* Constants
*/
private static final String _urlBase = "www..com";
private static final int _urlLength = 200;
private static final int _lowPriority = 1;
private static final int _highPriority = 0;
private static final Command _exitCommand
= new Command("Exit", Command.EXIT, _lowPriority);
private static final Command _fetchCommand
= new Command("Fetch", Command.OK, _highPriority);
/**
* Globals
*/
private Display _display = null;
private Form _form = null;
private TextField _url = new TextField("URL", _urlBase , _urlLength,
javax.microedition.lcdui.TextField.URL);
/**
* All MIDlets require a startApp, destroyApp, and pauseApp method.
*/
protected void startApp()
{
_form = new Form("Scape");
_form.setCommandListener(this);
_form.addCommand(_exitCommand);
_form.addCommand(_fetchCommand);
_form.append(_url);
_form.append("Enter a URL and press Fetch");
_display = Display.getDisplay(this);
_display.setCurrent(_form);
}
protected void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
/* Not used but must be declared */
protected void pauseApp() {}
/**
* Implement declared CommandListener interface
*/
public void commandAction(Command c, Displayable d)
{
if (c == _exitCommand)
{
destroyApp(true);
}
else if (c == _fetchCommand)
{
_form.delete(_form.size()-1);
dumpUriToForm("http://"+_url.getString(), _form);
}
}
/**
* Retrieve a page and dump it to the display
*
* This simple example just displays the raw page retrieved
* from the website. Alternatively, the results of the
* getViaStreamConnection() function could be stored in
* a string. The contents could then be searched using
* methods like indexOf()
*/
private void dumpUriToForm(String theUri, Form theForm)
{
try
{
theForm.append(getViaStreamConnection(theUri));
}
catch (java.io.IOException e)
{
theForm.append("Error! ");
}
}
/**
* Retrieve a URI and put the results into a string
*/
String getViaStreamConnection(String theUri) throws IOException
{
final int EOF = -1;
int length = 0;
StreamConnection connection = null;
InputStream stream = null;
byte buffer[] = new byte[200]; /* arbitrary buffer size */
String page = new String();
try
{
connection = (StreamConnection) Connector.open(theUri);
stream = connection.openInputStream();
/* Slurp the entire page */
while ((length = stream.read(buffer)) != EOF)
{
page = page + new String(buffer, 0, length);
}
}
finally
{
if (stream != null) stream.close();
if (connection != null) connection.close();
}
return(page);
}
}