This html parser searches for the string 'Malaysia' from
the website http://www.google.com.my and displays it:
/*
* By Paul Chin
*/
package htmlparser;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HtmlparserMidlet extends MIDlet implements CommandListener, Runnable {
private Display d;
private Form f;
private Command cmExit;
public HtmlparserMidlet() {
cmExit = new Command("Exit", Command.EXIT, 0);
f = new Form("Connecting...");
f.addCommand(cmExit);
f.setCommandListener(this);
}
public void startApp() {
if (d == null) {
d = Display.getDisplay(this);
d.setCurrent(f);
Thread t = new Thread(this);
t.start();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if (c == cmExit) {
destroyApp(false);
notifyDestroyed();
}
}
public void run() {
HttpConnection hc = null;
DataInputStream in = null;
StringBuffer data = new StringBuffer();
String url = "http://www.google.com.my/index.html";
try {
hc = (HttpConnection) Connector.open(url);
in = new DataInputStream(hc.openInputStream());
int ch;
while (true) {
ch = in.read();
if (ch == -1) {
break;
}
data.append((char) ch);
}
//Search for string "Malaysia"
String s = data.toString();
int index = s.indexOf("Malaysia");
String t = s.substring(index, index + 8);
f.append(t);
f.setTitle("Done");
} catch (Exception e) {
}
}
}