Creating SignedMIDlet

Note: The file in this example application contains the Nokia copyright statement.

The MIDlet has only one class, SignedMIDlet, which sets a textbox and adds two commands for closing the MIDlet and loading a page. The page's URL is set in the application descriptor. The Load command will attempt to open an HTTP connection and count the target page size in bytes. When the page is loaded, the amount of bytes read is displayed on the screen. In case of an error, the error message is displayed. The network operation is done in a separate thread.

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class SignedMIDlet
  extends MIDlet
  implements Runnable, CommandListener

  private final TextField text 
    = new TextField("", "", 256, TextField.ANY);
  private final Form form = new Form("HTTP Result");
  private final Command exitCommand 
    = new Command("Exit", Command.EXIT, 1);
  private final Command okCommand = new Command("Load", Command.OK, 1);

  public SignedMIDlet() {}

  public void startApp()
  {
    if (Display.getDisplay(this).getCurrent()==null)

      form.setCommandListener(this);
      form.addCommand(exitCommand);
      form.addCommand(okCommand);
      form.append(text);
      Display.getDisplay(this).setCurrent(form);
    }
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {}

  public void run()
  {
    // do an action that requires permission, e.g. HTTP connection
    try
    {
      String url = getAppProperty("url");
      HttpConnection connection =
(HttpConnection) Connector.open(url);
      InputStream in = connection.openInputStream();
      int counter = 0;
      int ch;
      while ((ch = in.read()) != -1)
      {
        counter++;
      }
      in.close();
      connection.close();
      text.setString("Bytes read: " + counter);
    }
    catch (IOException e)
    {
      text.setString("IOException: " + e.getMessage());
    }
    catch (SecurityException e)
    {
      text.setString("SecurityException: " + e.getMessage());
    }
  }

  public void commandAction(Command command, Displayable displayable)
  {
    if (command == exitCommand)
    {
      notifyDestroyed();
    }
    else if (command == okCommand)
    {
      new Thread(this).start();
    }
  }