Problema com botão em formulario

2 respostas
ocanema

Bom primeira pergunta que faço aqui no forum...
tenho uma aplicação teste pra fazer uma consulta a um banco por uma servlet..

tá funcionando direitinho.. o unico problema é que no "fmResultado" o command "cmBack" não retorna para o "fmMain" porque isso acontece?? alguem pode me dar um palpite?

segue o codigo:

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

public class ServerResponse extends MIDlet implements CommandListener {
    private Display display;
    private Form fmMain;
    private Form fmDownload;
    private Form fmResultados;
    private Command cmExit;
    private Command cmSearch;
    private StringItem siNome;
    private StringItem siAtt;
    private StringItem siEndereco;
    private TextField tfNome;
    private String errorMsg = null;
    private Alert alError;
    private Command cmBack;
    
    public ServerResponse(){
        display = Display.getDisplay(this);
        cmBack = new Command("Voltar", Command.SCREEN, 1);
        cmExit = new Command("Sair", Command.EXIT, 1);
        cmSearch = new Command("Buscar", Command.SCREEN, 1);
        fmMain = new Form("Teste de servidor");
        tfNome = new TextField("Nome:", "", 10, TextField.ANY);
        siNome = new StringItem("Nome: ", "");
        siAtt = new StringItem("Baixando Informações", "Aguarde");
        siEndereco = new StringItem("Endereco: ", "");
        
        fmMain.addCommand(cmExit);
        fmMain.addCommand(cmSearch);
        fmMain.append(tfNome);
        fmMain.setCommandListener(this);
        
        fmResultados = new Form("Resultados");
        fmResultados.append(siNome);
        fmResultados.append(siEndereco);
        fmResultados.addCommand(cmBack);
        
        fmDownload = new Form("");
        fmDownload.append(siAtt);
       
    }
    
    public void startApp() {
        display.setCurrent(fmMain);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable displayable) {
        if(c == cmExit){
            destroyApp(false);
            notifyDestroyed();
        }
        else if(c == cmSearch){
            try{
                goConnection();
            }
            catch(Exception e){
                
            }
        }
        else if (c == cmBack){
            display.setCurrent(fmMain);
        }
    }

    private void goConnection() throws IOException{
        Thread t = new Thread(new Runnable(){
           public void run(){
                HttpConnection http = null;
                InputStream istm = null;
                boolean ret = false;
        
                String url = "http://localhost:8080/teste/RequestServlet" + "?nome=" + tfNome.getString();
        
                try{
                        http = (HttpConnection) Connector.open(url);
                        http.setRequestMethod(HttpConnection.GET);
                        istm = http.openInputStream();
                        ret = processServerResponse(http, istm);
                    }
                catch(IOException e){
                    e.printStackTrace();
                }
                    finally{
                        if(istm != null)
                            try{
                             istm.close();   
                            }
                            catch(IOException e){
                                e.printStackTrace();
                            }
                        if(http != null)
                            try{
                                http.close();
                            }
                            catch(IOException e){
                                e.printStackTrace();
                            }
                    }
                    if (ret == false)
                    showAlert(errorMsg);
               
           }
        });
        t.start();
        display.setCurrent(fmDownload);
           
               
    }

    private boolean processServerResponse(HttpConnection http, InputStream istm)throws IOException {
        if(http.getResponseCode() == HttpConnection.HTTP_OK){
            int length = (int) http.getLength();
            String str;
            if(length != -1){
                byte servletData[] = new byte[length];
                istm.read(servletData);
                str = new String(servletData);
            }
            else{
                ByteArrayOutputStream bStm = new ByteArrayOutputStream();
                int ch;
                while((ch = istm.read()) != -1){
                    bStm.write(ch);
                }
                str = new String(bStm.toByteArray());
                bStm.close();
            }
            display.setCurrent(fmResultados);
            siNome.setText(str);
            return true;
        }
        else{
            errorMsg = new String( http.getResponseMessage());
            return false;
        }
    }

    private void showAlert(String msg) {
        alError = new Alert("Error", msg, null, AlertType.ERROR);
        alError.setTimeout(Alert.FOREVER);
        display.setCurrent(alError, fmMain);  
    }
}

Agradeço desde ja a possivel ajuda ^^

2 Respostas

R

Olá, bem vindo !

Antes de tudo, deixo claro aqui que nunca programei em JME e posso estar falando besteira.

No trecho de código:

# fmMain.addCommand(cmExit);  
#         fmMain.addCommand(cmSearch);  
#         fmMain.append(tfNome);  
#         fmMain.setCommandListener(this);  
#           
#         fmResultados = new Form("Resultados");  
#         fmResultados.append(siNome);  
#         fmResultados.append(siEndereco);  
#         fmResultados.addCommand(cmBack);

Não está faltando setar o listener no fmResultados, para a classe “ouvir” o cmBack também ? Tipo:

fmResultados.setCommandListener(this);

flw.

Roger Leite

ocanema

Woooww!!

Puts como eu fui idiota de não ver isso^^ :oops:
Valew cara… brigadão! :smiley:

Criado 14 de setembro de 2007
Ultima resposta 14 de set. de 2007
Respostas 2
Participantes 2