NetTest.java



import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class NetTest extends MIDlet
                     implements CommandListener {

    public Form form;
    public Ticker ticker;
    public Command command;

    public void startApp() {
        form = new Form( "Network Test" );
        ticker = new Ticker( "" );
        form.setTicker( ticker );
        command = new Command( "receive", Command.SCREEN, 1 );
        form.addCommand( command );
        form.setCommandListener( this );
        Display.getDisplay( this ).setCurrent( form );
    }

    public void pauseApp() {
    }

    public void destroyApp( boolean flag ) {
    }

    public void commandAction( Command source, Displayable d ) {

        if( source == command ) {
            String message = getMessage();
            ticker.setString( message );
        }
    }

    public String getMessage() {

        String message = null;
        try {
            HttpConnection connection
               = (HttpConnection)Connector.open(
                   "http://www.wakhok.ac.jp/~tatsuo/i/j/test.txt",
                  // "http://www.wakhok.ac.jp/cgi-bin/test.cgi",
                  // "http://www.wakhok.ac.jp/cgi-bin/time.cgi",
                    Connector.READ, true );
            connection.setRequestMethod( HttpConnection.GET );
            connection.setRequestProperty( "Content-Type", "text/plain" );
      //      connection.connect();
            InputStream in = connection.openInputStream();
            InputStreamReader reader = new InputStreamReader( in );
            StringBuffer sb = new StringBuffer();
            int i=0;
            while( ( i = reader.read() ) != -1 ) {
                 char c = ( char )i;
                 sb.append( c );
            }
            reader.close();
            in.close();
            connection.close();
            message = sb.toString();
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
        return message;
    }
}