J2ME - Cliente para Consumir Web Services

2 respostas
B

Estou com o seguinte código abaixo que ao acessar ao efetuar a chamada para o evento do Command buscar emite a seguinte mensagem:

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

o aplicativo fica parado na tela com a mensagem:

wants to send and receive data using
the network. This will use airtime and may result in charges…

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

import org.ksoap.SoapObject;
import org.ksoap.transport.HttpTransport;

public class ClienteJME extends MIDlet implements CommandListener {
    private Display display;
    private Form frmPrincipal, frmResultado;    
    private TextField tfCodigo;
    private TextField tfQtde;
    private Command buscar, sair;
    private StringItem msg;
    StringBuffer stringBuffer = new StringBuffer();
           
    public ClienteJME (){
       //inicializar componentes
       this.sair   = new Command("Sair", Command.EXIT, 0);
       this.buscar = new Command("Buscar",Command.OK,1 );
       //form principal
       this.frmPrincipal = new Form("Web Services JME");
             
       this.tfCodigo = new TextField("Código do Produto:", "1", 30, TextField.ANY);
       //this.tfQtde   = new TextField("Retorna estoque atual","", 30, TextField.NUMERIC);
       
       frmPrincipal.append(this.tfCodigo);
       //frmPrincipal.append(this.tfQtde);
       
       //adiciona o command para o formulário
       this.frmPrincipal.addCommand(this.sair);
       this.frmPrincipal.addCommand(this.buscar);
       
       this.frmPrincipal.setCommandListener(this);
       //form de resultado
       this.frmResultado = new Form("Resultado");
       this.msg = new StringItem("","");
       //adiciona os componentes no form resultado
       this.frmResultado.append(this.msg);
       this.frmResultado.addCommand(this.sair);
       this.frmResultado.setCommandListener(this);       
    }
    
    public void startApp() {
       display = Display.getDisplay(this);
       this.display.setCurrent(frmPrincipal);        
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void testWebService(String codigo ) throws Exception {       
        // Chama o WebService
        if (codigo != ""){
            String url = "http://localhost:8080/axis/service/ServiceProduto.jws?wsdl"; 
            SoapObject client = new  SoapObject(url,"getQuantidade");
            client.addProperty("codigo", codigo);       
            HttpTransport ht = new HttpTransport(url,"getQuantidade");
            
            this.stringBuffer.append(ht.call(client));                    
            this.msg.setLabel("Quantidade: ");
            this.msg.setText(stringBuffer.toString());            
            this.display.setCurrent(this.frmResultado);
        }
    }
    
    //método chamado assim que um command for adicionado.
    public void commandAction(Command c, Displayable d)
    {      
       if (c == this.sair){
           this.destroyApp(true);
           this.notifyDestroyed();           
       }
       else 
       if (d == frmPrincipal){
           if (c == this.buscar ){
              try {   
                this.testWebService(tfCodigo.getString());             
              } catch (Exception ex) {
                 System.out.println(ex);
              }    
           } 
       }
    }
}

Se eu chamar direto o método this.testWebService(tfCodigo.getString());
no evento startApp não tem problema o código funciona normalmente o grande problema é quando chamo pelo command Buscar, como faço para resolver este problema?

2 Respostas

R

pq o StartApp implementa Thread intenamente…
vc precisa colocar sua chama em uma Thread:

. . .

Thread th = new Thread();
th.start

public void run () {

seuMetodo();

}

creio q deva resolver

B

Obrigado roberto.teixeira a sua sugestão resolveu o meu problema.

Criado 21 de dezembro de 2006
Ultima resposta 22 de dez. de 2006
Respostas 2
Participantes 2