import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class Conn extends MIDlet implements CommandListener
{
  private String url = "http://localhost:8080/docuX.txt";
  private Display display;
  private TextBox text;
  private Command cmConnect;
  private Command cmExit;

   public Conn()
   {
     display = Display.getDisplay(this);
     text = new TextBox("Network connection", "", 100, TextField.ANY);
     cmConnect = new Command("Connect", Command.SCREEN, 1);
     cmExit = new Command("Exit", Command.EXIT, 1);
   }
   public void startApp()
   {
     text.addCommand(cmConnect);
     text.addCommand(cmExit);
     text.setCommandListener(this);
     display.setCurrent(text);
   }
   public void commandAction(Command c, Displayable s)
   {
      if(c == cmConnect)
      {
         Thread t = new Thread(){
            public void run(){
               connect();
            }
         };
         t.start();
      }
      else if(c == cmExit)
      {
         destroyApp(false);
         notifyDestroyed();
      }
   }
   private void connect()
   {
      HttpConnection httpConn = null;
      InputStream in = null;
      try
      {
          httpConn = (HttpConnection) Connector.open(url);
          in = httpConn.openInputStream();
          int length = (int) httpConn.getLength();
          byte data[] = new byte[length];
          in.read(data);
          String str = new String(data);
          text.setString(str);
          System.out.println("The content of docuX:\n" + str);
          in.close();
          httpConn.close();
      }catch(IOException ex){
          text.setString("** Error, connecting... : " + ex.getMessage());
      }
  }
   public void pauseApp(){}
   public void destroyApp(boolean b){}
}


