Creating Source Code

Team-Fly

Writing Java source code is the same as it always was: use your favorite text editor to create a source file with a .java extension. The example we'll build and run is Jargoneer, a MIDlet that looks up words in the Jargon File. The Jargon File is a comprehensive lexicon of hacker slang (find out more by visiting http://www.fwi.uva.nl/~mes/jargon/).

Jargoneer allows you to enter any word, and then connects to an Internet site to find the definition. Running this MIDlet will allow you to appear cool in the company of your hacker friends. When someone uses an unfamiliar word, like "cruft" or "grok," you can surreptitiously key the word into your mobile phone and see a definition in a few seconds.

Jargoneer's source code is provided in Listing 2-1. If you don't want to type it in, you can download all of the code examples in this book from http://www.apress.com.

Listing 2-1: Jargoneer's source code.

start example
 import java.io.*; import javax.microedition.io.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Jargoneer extends MIDlet     implements CommandListener {   private Display mDisplay;   private Command mExitCommand, mFindCommand, mCancelCommand;   private TextBox mSubmitBox;   private Form mProgressForm;   private StringItem mProgressString;   private Gauge mProgressGauge;   public Jargoneer() {     mExitCommand = new Command("Exit", Command.EXIT, 0);     mFindCommand = new Command("Find", Command.SCREEN, 0);     mCancelCommand = new Command("Cancel", Command.CANCEL, 0);     mSubmitBox = new TextBox("Jargoneer", "", 32, 0);     mSubmitBox.addCommand(mExitCommand);     mSubmitBox.addCommand(mFindCommand);     mSubmitBox.setCommandListener(this);     mProgressForm = new Form("Lookup progress");     mProgressGauge = new Gauge(null, false, 10, 0);     mProgressString = new StringItem(null, null);     mProgressForm.append(mProgressGauge);     mProgressForm.append(mProgressString);   }   public void startApp() {     mDisplay = Display.getDisplay(this);     mDisplay.setCurrent(mSubmitBox);   } public void pauseApp() {}   public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) {   if (c == mExitCommand) {     destroyApp(false);     notifyDestroyed();   }   else if (c == mFindCommand) {     // Show the progress form.     mDisplay.setCurrent(mProgressForm);     // Do the query.     String word = mSubmitBox.getString();     Poster p = new Poster(word);     p.start(); // Runs in its own thread.   } } public class Poster extends Thread {   private static final String kURL = "http://www.dict.org/bin/Dict";   private static final String kParameters =       "Form=Dict1&Strategy=*&Database=jargon&Query=";   private String mPostString;   public Poster(String word) {     mPostString = kParameters + word;   }   public void run() {     HttpConnection c = null;     InputStream in = null;     OutputStream out = null;     StringBuffer definition = new StringBuffer();     try {       mProgressString.setText("Sending...");       c = (HttpConnection)Connector.open(kURL);       // Set the request method and headers       c.setRequestMethod(HttpConnection.POST);       c.setRequestProperty("User-Agent",           "Profile/MIDP-1.0 Configuration/CLDC-1.0");       c.setRequestProperty("Content-Language", "en-US");       c.setRequestProperty("Content-Type",           "application/x-www-form-urlencoded");       c.setRequestProperty("Content-Length",           String.valueOf(mPostString.length()));       // Write out the POST parameters.       out = c.openOutputStream();       out.write(mPostString.getBytes());       out.flush();       mProgressString.setText("Waiting...");       in = c.openInputStream();       String line;       int count = 0;       boolean inPre = false;       while ((line = readLine(in)) != null) {         count++;         mProgressGauge.setValue(count % 10);         mProgressString.setText("Receiving");         int preIndex = line.indexOf("<pre>");         int slashpreIndex = line.indexOf("</pre>");         if (preIndex != −1)           inPre = true;         else if (slashpreIndex != −1)           inPre = (preIndex > slashpreIndex);         else if (inPre == true)           trimAndAppend(line, definition);       }     }     catch (IOException ioe) {       Alert error = new Alert("Error", ioe.toString(), null, null);       mDisplay.setCurrent(error, mSubmitBox);     }     finally {       try {         if (in != null)           in.close();         if (out != null)           out.close();         if (c != null)           c.close();       }       catch (IOException ioe) {}     }     Alert results = new Alert("Definition", definition.toString(),         null, null);     results.setTimeout(Alert.FOREVER);     mDisplay.setCurrent(results, mSubmitBox);   }     private byte[] mBuffer = new byte[512];     protected String readLine(InputStream in) throws IOException {       int c, index = 0;       while ((c = in.read()) != −1 && c != '\n')         mBuffer[index++] = (byte)c;       if (c == −1 && index == 0)         return null;       return new String(mBuffer, 0, index);     }     protected void trimAndAppend(String line, StringBuffer amalgam) {       boolean leading = true;       boolean inTag = false;       int c;       for (int i = 0; i < line.length(); i++) {         c = line.charAt(i);         if (c == '<')           inTag = true;         else if (c == '>')           inTag = false;         else if (c == ' ' && leading == true)           ;         else if (inTag == false) {           amalgam.append((char)c);           leading = false;         }       }       if (leading == false) amalgam.append(' ');     }   } } 
end example


Team-Fly


Wireless Java. Developing with J2ME
ColdFusion MX Professional Projects
ISBN: 1590590775
EAN: 2147483647
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net