Iniciante J2ME

4 respostas
kelvis

E ai Galera...

Seguinte, sou iniciante na programação J2ME e tô fazendo alguns pequenos testes aqui...
Estou tentando me conectar com um site via HTTP para abrir a página no emulador, porém
está caindo na 1ª Exceção e mostrando a mensagem que designei "Erro! URL não pode ser aberta, tente novamente"

Alguém pode dar uma olhada no meu código e me dizer onde estou errando?!

package teste;

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

/**
 * @author kelvis.almeida
 */
public class TesteHTTP extends MIDlet {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
    //</editor-fold>
    /**
     * The TesteHTTP constructor.
     */
    public TesteHTTP() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {
        // write pre-initialize user code here
 
        // write post-initialize user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {
        // write pre-action user code here
 
        // write post-action user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {
        // write pre-action user code here
 
        // write post-action user code here
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
    // write post-switch user code here
    }
    //</editor-fold>

    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    public void pauseApp() {
        midletPaused = true;
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void exitMIDlet() {
//        switchDisplayable(null, null);
//        destroyApp(true);
//        notifyDestroyed();
    }

    public void startApp() {
//        if (midletPaused) {
//            resumeMIDlet();
//        } else {
//            initialize();
//            startMIDlet();
//        }


        try {
            String URL = "http://www.google.com.br";
            HttpConnection conexao = (HttpConnection) Connector.open(URL);
            InputStream inpStream = conexao.openInputStream();

            System.out.println("Host= " + conexao.getHost());

            System.out.println("Porta= " + conexao.getPort());

            System.out.println("Protocolo= " + conexao.getProtocol());

            conexao.close();
            inpStream.close();

        } catch (ConnectionNotFoundException cnfex) {
            System.out.println("Erro! URL não pode ser aberta, tente novamente");
        } catch (IOException IOex) {
            System.out.println(IOex.toString());
        }

        midletPaused = false;
        destroyApp(true);

    }
}

4 Respostas

Norxs

hehehehe … justo o google cara? … auhauauh …

analisa o erro ai … cnfex.printStackTrace();

kelvis

Então parceiro... Dei um sysOut no cnfex e ele deu a seguinte saída: TCP OPEN
Também mudei a URL porque saquei que o google tem altos redirecionamentos... hauhauahuah
Mas tá o mesmo erro... Alguma idéia pra me ajudar?

try {
            String URL = "http://www.guj.com.br";
            HttpConnection conexao = (HttpConnection) Connector.open(URL);
            InputStream inpStream = conexao.openInputStream();

            System.out.println("Host= " + conexao.getHost());

            System.out.println("Porta= " + conexao.getPort());

            System.out.println("Protocolo= " + conexao.getProtocol());

            conexao.close();
            inpStream.close();

        } catch (ConnectionNotFoundException cnfex) {
            System.out.println("Erro! URL não pode ser aberta, tente novamente");
            System.out.println(cnfex.getMessage());
            
        } catch (IOException IOex) {
            System.out.println(IOex.toString());
        }

        midletPaused = false;
        destroyApp(true);

    }
}
kelvis

ops… dei um cnfex.printStackTrace(); e vi o danado do erro!

kelvis

put'z galera... O erro persiste... Será que alguém pode dar um Help?

O código com as devidas alterações segue:

package teste;

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

/**
 * @author kelvis.almeida
 */
public class TesteHTTP extends MIDlet {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">                      
    //</editor-fold>                    
    /**
     * The TesteHTTP constructor.
     */
    public TesteHTTP() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">                       
    //</editor-fold>                     

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">                                           
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {                                         
        // write pre-initialize user code here
                                           
        // write post-initialize user code here
    }                            
    //</editor-fold>                          

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">                                        
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {                                      
        // write pre-action user code here
                                        
        // write post-action user code here
    }                             
    //</editor-fold>                           

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">                                         
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {                                       
        // write pre-action user code here
                                         
        // write post-action user code here
    }                              
    //</editor-fold>                            

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">                                              
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {                                            
        // write pre-switch user code here
        Display display = getDisplay();                                               
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }                                             
    // write post-switch user code here
    }                                   
    //</editor-fold>                                 

    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    public void pauseApp() {
        midletPaused = true;
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }

        TextBox tbox = new TextBox(".::Módulo de conexão::.", "Conectando...", 25, 0);
        Display.getDisplay(this).setCurrent(tbox);

        try {
            String URL = "http://www.cts.org.br/icts/";
            HttpConnection conexao = (HttpConnection) Connector.open(URL);
            InputStream inpStream = conexao.openInputStream();

            System.out.println("Host= " + conexao.getHost());

            System.out.println("Porta= " + conexao.getPort());

            System.out.println("Protocolo= " + conexao.getProtocol());

            conexao.close();
            inpStream.close();

        } catch (ConnectionNotFoundException cnfex) {
            System.out.println("Erro! URL não pode ser aberta!");
            cnfex.printStackTrace();
            
        } catch (IOException IOex) {
            System.out.println(IOex.toString());
        }

        midletPaused = false;
        destroyApp(true);

    }
}
Criado 19 de fevereiro de 2009
Ultima resposta 20 de fev. de 2009
Respostas 4
Participantes 2