Conexao j2me

Bom dia,

Pessoal to começando com o J2ME e to com alguns problemas, eu to testando conexao com internet, é somente a exibiçao de uma imagem. A parte do codigo que tenta abrir conexao é :

ContentConnection connection = (ContentConnection) Connector.open(url);
InputStream istrm = connection.openDataInputStream();

o problema que gera é :

Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.

Bem eu gostaria de saber que tipo de problema é este, e se eu posso testar conexao no emulador q o netbeans da, ou se tem q ser em um celular mesmo…

Grato pela atençao…grande abraço a todos…

da pra testar conexão no emulador sim…

quanto ao seu erro, poste o código todo, de preferencia dentro das tags [code]

[quote=Pedro Saavedra]Bom dia,

Pessoal to começando com o J2ME e to com alguns problemas, eu to testando conexao com internet, é somente a exibiçao de uma imagem. A parte do codigo que tenta abrir conexao é :

ContentConnection connection = (ContentConnection) Connector.open(url);
InputStream istrm = connection.openDataInputStream();

o problema que gera é :

Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.

Bem eu gostaria de saber que tipo de problema é este, e se eu posso testar conexao no emulador q o netbeans da, ou se tem q ser em um celular mesmo…

Grato pela atençao…grande abraço a todos…[/quote]

A mensagem foi clara: O ambiente recomendou que você use thread para evitar que o usuário perca a responsividade da aplicação.

É um tassunto já discutido aqui inúmeras vezes. Use a função busca. Abaixo o link para te levar a posts com o mesmo problema:
http://www.guj.com.br/jforum.java?module=search&action=search&search_keywords=To+avoid+potential+deadlock&match_type=all&search_forum=14&sort_by=relevance

Pessoal eu estou tentando por multithread e nao estou conseguindo…segue a funcao abaixo se alguem puder me ajudar…

private Image getImage(String url)throws IOException{

    ContentConnection connection = (ContentConnection) Connector.open(url);
    InputStream istrm = connection.openDataInputStream();
    Image im = null;
    
    try{
        byte [] imageData=null;
        int lenght = (int) connection.getLength();
        
        if(lenght != -1){
            imageData = new byte[lenght];
            istrm.read(imageData);
        
        }else {
            ByteArrayOutputStream bstrm = new ByteArrayOutputStream();
            int ch;
            
            while((ch = istrm.read())!= -1){
                bstrm.write(ch);
                
                imageData = bstrm.toByteArray();
                bstrm.close();
            }
            
        }
        im = Image.createImage(imageData, 0, imageData.length);
        
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(istrm != null){
            istrm.close();
        }
        if(connection != null){
            connection.close();
        }
        return (im==null? null: im);
    }
}

[color=red][size=18]Primeiro coloque seu código entre as tags [code]
[/size][/color]

Segundo, cadê a implementação da thread??? :?

Segue todo codigo abaixo… PESSOAL VLU A ATENÇAO !!!

[code]public class ConexaoFoto extends MIDlet implements CommandListener{

private Display display;
private Form form;
private TextBox tbox;
private Command cmdExit;
private Command cmdView;
private Command cmdBack;

public ConexaoFoto(){
    display = Display.getDisplay(this);
    form = new Form("Formulario foto");
            
    cmdExit = new Command("Exit", Command.EXIT, 1);
    cmdView = new Command("View", Command.SCREEN, 1);
    cmdBack = new Command("Back", Command.BACK, 1);
    
    tbox = new TextBox("Digite a URL", 
            "http://www1.folha.uol.com.br/folha/especial/2007/"+
            "campeonatobrasileiro/images/escudo-flamengo-80x80.gif",150,0);
    
    tbox.addCommand(cmdExit);
    tbox.addCommand(cmdView);
    tbox.setCommandListener(this);
    
    form.addCommand(cmdBack);
    form.setCommandListener(this);
}
public void startApp() {
    display.setCurrent(tbox);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
    
}

public void commandAction(Command c, Displayable d) {
    if(c == cmdExit){
        destroyApp(true);
        notifyDestroyed();
    }else if(c == cmdView){
        
        try{
            Image im;
            if((im = getImage(tbox.getString()))!= null){
                ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);
                
                if(form.size() != 0){
                    form.set(0, ii);
                }else{
                    form.append(ii);
                }
            }else{
                display.setCurrent(form);
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
}

private Image getImage(String url)throws IOException{
    
    ContentConnection connection = (ContentConnection) Connector.open(url);
    InputStream istrm = connection.openDataInputStream();
    Image im = null;
    
    try{
        byte [] imageData=null;
        int lenght = (int) connection.getLength();
        
        if(lenght != -1){
            imageData = new byte[lenght];
            istrm.read(imageData);
        
        }else {
            ByteArrayOutputStream bstrm = new ByteArrayOutputStream();
            int ch;
            
            while((ch = istrm.read())!= -1){
                bstrm.write(ch);
                
                imageData = bstrm.toByteArray();
                bstrm.close();
            }
            
        }
        im = Image.createImage(imageData, 0, imageData.length);
        
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(istrm != null){
            istrm.close();
        }
        if(connection != null){
            connection.close();
        }
        return (im==null? null: im);
    }
}

}[/code]

é como eu e o boone falamos, falta vc implementar thread!

da uma procurada sobre isso no fórum ta cheio de coisa…

Galera eu nao tenho muita intimidade com thread eu fiz de uma forma aqui e continua dando o mesmo problema:

[code]public class ConexaoFoto extends MIDlet implements CommandListener{

private Display display;
private Form form;
private TextBox tbox;
private Command cmdExit;
private Command cmdView;
private Command cmdBack;

public ConexaoFoto(){
    display = Display.getDisplay(this);
    form = new Form("Formulario foto");
            
    cmdExit = new Command("Exit", Command.EXIT, 1);
    cmdView = new Command("View", Command.SCREEN, 1);
    cmdBack = new Command("Back", Command.BACK, 1);
    
    tbox = new TextBox("Digite a URL", 
            "http://www1.folha.uol.com.br/folha/especial/2007/"+
            "campeonatobrasileiro/images/escudo-flamengo-80x80.gif",150,0);
    
    tbox.addCommand(cmdExit);
    tbox.addCommand(cmdView);
    tbox.setCommandListener(this);
    
    form.addCommand(cmdBack);
    form.setCommandListener(this);
}
public void startApp() {
    display.setCurrent(tbox);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
    
}

public void commandAction(Command c, Displayable d) {
    if(c == cmdExit){
        destroyApp(true);
        notifyDestroyed();
    }else if(c == cmdView){
        
        try{
            RetornaConexao r = new RetornaConexao();
            r.run();
            Image im;
            if(r.im != null){
                im = r.im;
                ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);
                
                if(form.size() != 0){
                    form.set(0, ii);
                }else{
                    form.append(ii);
                }
            }else{
                display.setCurrent(form);
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
}

}[/code]

A outra classe é :

[code]public class RetornaConexao implements Runnable{
Image im = null;

private void getImage()throws IOException{
    ContentConnection connection = (ContentConnection) Connector.open("http://www1.folha.uol.com.br/folha/especial/2007/"+
            "campeonatobrasileiro/images/escudo-flamengo-80x80.gif");
    
    InputStream istrm = connection.openDataInputStream();
            
    try{
        byte [] imageData=null;
        int lenght = (int) connection.getLength();
        
        if(lenght != -1){
            imageData = new byte[lenght];
            istrm.read(imageData);
        
        }else {
            ByteArrayOutputStream bstrm = new ByteArrayOutputStream();
            int ch;
            
            while((ch = istrm.read())!= -1){
                bstrm.write(ch);
                
                imageData = bstrm.toByteArray();
                bstrm.close();
            }
            
        }
        im = Image.createImage(imageData, 0, imageData.length);
        
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(istrm != null){
            istrm.close();
        }
        if(connection != null){
            connection.close();
        }
       
    }
}

public void run() {
    try{
        getImage();
    }catch(IOException e){
        e.printStackTrace();
    }
}

}[/code]

A aplicaçao para nesta linda :

ContentConnection connection = (ContentConnection) Connector.open("http://www1.folha.uol.com.br/folha/especial/2007/"+ "campeonatobrasileiro/images/escudo-flamengo-80x80.gif");

Sei que to pertubando com isso, mas é que tenho que aprender e fazendo uma faço varias…

heheheheh
amigo
eu ja postei um monte d coisa aki relativo a isso
pra facilitar pra vc
www.elainececiliagatto.blogspot.com
pega minha monografia la emeus arquivos
vai lhe dar uma espairecida
thread são fáceis até não fique com medo
hehehehehe

ah sim outra coisa
o q extamente vc ta querendo fazer???
tem uma string conexao enorme
vc poderia dizer?
obrigada

Tipo, implementa pra vc ae:

if (command == cmdBuscar) {                       
         new Thread(new Runnable() {   
                   public void run(){   
                      url = "http://localhost/teste.php?Vid_cliente="+txtCliente.getString();   
                                try{   
                           buscaDados(url);   
                        }catch (IOException e) {
...

Ai eu uso o Thread