Ola, preciso colocar pra rodar um programa que se conecta a um servlet. O problema é quando coloco no emulador WTK pra rodar tá dando erro de:
Is it Ok to use airtime?
eu coloco yes no emulador mas nao acontece nada.
Li em foruns que é pra fazer em threads, já coloquei meu código em threads mas continua com o mesmo erro.
Me ajudem por favor.
POsta o código ai amigo fica mais facil pra ajudar
Meu código do Midlet
[code]package controle;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class MeuMidlet extends MIDlet implements CommandListener
{
private Display tela;
private Form formulario;
private Command btSair;
private Command btTestar;
private TextField tfSenha;
private TextField tfConta;
public MeuMidlet()
{
// inicializacao dos componentes
this.formulario = new Form("Midlet/Servlet");
//this.formulario.append("Testando....");
this.tfConta = new TextField("Conta: ", "", 20, TextField.ANY);
this.formulario.append(this.tfConta);
this.tfSenha = new TextField("Senha: ", "", 20, TextField.ANY);
this.formulario.append(this.tfSenha);
this.btSair = new Command("Sair", Command.EXIT, 0);
this.btTestar = new Command("Testar", Command.SCREEN,1);
// adiciona o command ao formulario
this.formulario.addCommand(this.btSair);
this.formulario.addCommand(this.btTestar);
// configura esta instancia como listener dos eventos de command
this.formulario.setCommandListener(this);
}
public void startApp()
{
// obtem referencia a tela do dispositivo
this.tela = Display.getDisplay(this);
// configura o formulario como o Displayable corrente
this.tela.setCurrent(this.formulario);
}
public void pauseApp()
{
}
public void destroyApp(boolean pode)
{
}
public void commandAction(Command comando, Displayable d)
{
if (comando == this.btSair)
{
// se o command sair for acionado
this.destroyApp(true);
this.notifyDestroyed();
}
if (comando == this.btTestar)
{
boolean estahOk = false;
Alert alerta = new Alert("Resposta");
try
{
String url = "http://127.0.0.1:8080/prjServlet/servidor?cta=" + this.tfConta.getString() + "&pw=" + this.tfSenha.getString();
// String url = "http://32.101.215.139:8080/servidor/servidor?cta=" + this.tfConta.getString() + "&pw=" + this.tfSenha.getString();
System.out.println(url);
HttpConnection http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
http.setRequestProperty("User-Agent", "Profile/MIDP-1.0");
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
InputStream resposta = http.openInputStream();
int tamanho = (int)http.getLength();
if(tamanho != -1)
{
byte[] conteudo = new byte [tamanho];
resposta.read(conteudo);
if (conteudo [0] == (byte) '1')
estahOk = true;
else
estahOk = false;
}
if(estahOk)
alerta.setString("Conexão bem sucedida e autenticação Ok!!!");
else
alerta.setString("Conexão bem sucedida e autenticação falhou!!!");
}
else
alerta.setString("Conexão falhou! " + http.getResponseMessage());
// se o command msg for acionado
}
catch(Throwable t)
{
alerta.setString("Exceção!!!");
}
this.tela.setCurrent(alerta);
}
}
}
[/code]
Meu servlet:
package controle;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servidor extends HttpServlet
{
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException
{
System.out.println("recebeu requisição!");
String conta = request.getParameter ("cta");
String senha = request.getParameter ("pw");
response.setContentType ("text/plain");
PrintWriter out = response.getWriter();
if (conta != null && conta.equals("java"))
{
if (senha != null && senha.equals("j2me"))
{
out.print ("1");
out.close ( );
return;
}
}
out.print ("0");
out.close ();
}
}
Qd coloco pra rodar no WTK e coloco usuário e senha da o erro airtime.
Pelo que vi vc não esta usando thread para a conexão… Tenta fazer com thread e ve se funciona…
Voce debugou e ele para a onde ???
Código com as thread
package controle;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class MeuMidlet extends MIDlet implements CommandListener
{
private Display tela;
private Form formulario;
private Command btSair;
private Command btTestar;
private TextField tfSenha;
private TextField tfConta;
public MeuMidlet()
{
// inicializacao dos componentes
this.formulario = new Form("Midlet/Servlet");
//this.formulario.append("Testando....");
this.tfConta = new TextField("Conta: ", "", 20, TextField.ANY);
this.formulario.append(this.tfConta);
this.tfSenha = new TextField("Senha: ", "", 20, TextField.ANY);
this.formulario.append(this.tfSenha);
this.btSair = new Command("Sair", Command.EXIT, 0);
this.btTestar = new Command("Testar", Command.SCREEN,1);
// adiciona o command ao formulario
this.formulario.addCommand(this.btSair);
this.formulario.addCommand(this.btTestar);
// configura esta instancia como listener dos eventos de command
this.formulario.setCommandListener(this);
}
public void startApp()
{
// obtem referencia a tela do dispositivo
this.tela = Display.getDisplay(this);
// configura o formulario como o Displayable corrente
this.tela.setCurrent(this.formulario);
}
public void pauseApp()
{
}
public void destroyApp(boolean pode)
{
}
public void commandAction(Command comando, Displayable d)
{
if (comando == this.btSair)
{
// se o command sair for acionado
this.destroyApp(true);
this.notifyDestroyed();
}
if (comando == this.btTestar)
{
boolean estahOk = false;
EnviaRequisicao minhaThread = new EnviaRequisicao(this.tfConta.getString(), this.tfSenha.getString());
while(minhaThread.isAlive())
; // Está trabalhando
Alert alerta = new Alert("Resposta");
alerta.setString(minhaThread.getResultado());
}
}
public class EnviaRequisicao extends Thread
{
private String conta;
private String senha;
private String resultado = "processando...";
public EnviaRequisicao(String conta, String senha)
{
this.conta = conta;
this.senha = senha;
this.start();
}
public void run()
{
try
{
String url = "http://127.0.0.1:8080/prjServlet/servidor?cta=" + this.conta +
"&pw=" + this.senha;
// String url = "http://32.101.215.139:8080/servidor/servidor?cta=" + this.tfConta.getString() + "&pw=" + this.tfSenha.getString();
System.out.println("Executando -->" + url);
HttpConnection http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
http.setRequestProperty("User-Agent", "Profile/MIDP-1.0");
if (http.getResponseCode() == HttpConnection.HTTP_OK)
{
InputStream resposta = http.openInputStream();
int tamanho = (int)http.getLength();
if(tamanho != -1)
{
byte[] conteudo = new byte [tamanho];
resposta.read(conteudo);
if (conteudo [0] == (byte) '1')
this.resultado = "Ok!!! Validou corretamente";
else
this.resultado = "Ok!!! Mas a conta/senha está errada";
}
}
else
this.resultado = "Conexão falhou! " + http.getResponseMessage();
// se o command msg for acionado
}
catch(Throwable t)
{
this.resultado = t.getMessage();
}
}
public String getResultado()
{
return this.resultado;
}
}
}
Ao colocar pra rodar aparece
Running with locale: Portuguese_Brazil.1252
Running in the identified_third_party security domain
http://127.0.0.1:8080/prjServlet/servidor?cta=java&pw=j2me
Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
E no emulador aparece “Is it OK to use airtime? yes / no”
Mas se eu apertar em yes nao acontece nada
Me ajudem.
Obrigada
Ao colocar pra executar, eu digito o usuário e a senha e envio.
ai da a mensagem de erro e nao acontece mais nada.
percebi que o código pára de executar ao iniciar a conexão “HttpConnection http = (HttpConnection) Connector.open(url);”, pois antes e depois da conexão coloquei para imprimir uma mensagem e ele mostra somente a mensagem que vem antes da conexão.