/*
 * httpMIDlet.java
 *
 * Created on 22 de Dezembro de 2002, 01:25
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Hashtable;

/**
 * An example MIDlet with simple "Hello" text and an Exit command.
 * Refer to the startApp, pauseApp, and destroyApp
 * methods so see how each handles the requested transition.
 *
 * @author  edgar
 * @version
 */
public class httpMIDlet extends MIDlet implements CommandListener {
    
    private TextBox t = new TextBox("Página:", "http://10.10.0.5/papo3d/acesso.asp?iv=102&senha=casa&Submit=Consultar", 1024, 0);
    private String url_base;

    private Command exitCommand; // The exit command
    private Command AbrirCommand; // The exit command
    private Command BaseCommand; // The exit command
    private Display display;    // The display for this MIDlet

    public httpMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.SCREEN, 2);
        AbrirCommand = new Command("Abrir", Command.SCREEN, 3);
        BaseCommand = new Command("Base", Command.SCREEN, 4);
    }
    
    /**
     * Start up the Hello MIDlet by creating the TextBox and associating
     * the exit command and listener.
     */
    public void startApp() {
        
        t.addCommand(exitCommand);
        t.addCommand(AbrirCommand);
        t.addCommand(BaseCommand);
        
        url_base = t.getString();
        
        t.setCommandListener(this);
        display.setCurrent(t);
    }
    
    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp() {
    }
    
    /**
     * Destroy must cleanup everything not handled by the garbage collector.
     * In this case there is nothing to cleanup.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    /*
     * Respond to commands, including exit
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     */
    public void commandAction(Command c, Displayable s) {
    String retorno;
        
        
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
        if (c == AbrirCommand) {
            try {
                    url_base = t.getString();
                    retorno = AbrirUrl(url_base);
                    t.setString(retorno);
                } catch (Exception e) {
                    t.setString("Erro:");
                    e.printStackTrace();
                }
        }
        if (c == BaseCommand) {
           t.setString(url_base);
        }
    }

    // Pega o código de determinada URL
    public String AbrirUrl(String tURL) 
            throws Exception, IOException {
        int count;
        byte[] b = new byte[32];
        int total = 0;
        StreamConnection c = null;
        InputStream in = null;

        c = (StreamConnection) Connector.open(tURL);
        in = c.openInputStream();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        while ((count = in.read(b, 0, 32)) > 0) {
            bos.write(b, 0, count);
            total += count;
        }
        byte r[] = bos.toByteArray();
        String s = new String(r, 0, r.length);
        //
        return (s);
    }    
}
