Pessoal, to com um problema com thread...
Minha intensao é fazer uma tela de login. Ao digitar Enviar, deve-se autenticar o usuario. Se forneceu os dados corretamente, prossegue, senao fica onde está.
O que eu fiz:
Tenho uma classe (Login) com os campos pra digitar e um botao enviar. Ao clicar em enviar, instancio uma classe (Autenticacao) que faz conexao com banco de dados.
Na classe Autenticacao eu implemento Runnable para fazer conexao HTTP.
Gostaria que a thread ficasse travada, esperando fazer a conexao. Aí sim voltar pra Login e ver se usuario pode continuar ou nao.
Do jeito que tá, fica travado na tela "Is it OK to use airtime?".
O que eu estou fazendo de errado? Devo mudar a aborgadem do problema?
Alguém tem alguma sugestao?
public class Login implements CommandListener {
.
.
.
public void commandAction(Command command, Displayable disp) {
if (command == exit) {
principal.fecharAplicacao();
} else if (command == send) {
if (verificaDados()) {
Autenticacao autenticacao = new Autenticacao(this, login.getString(), senha.getString());
synchronized (autenticacao) {
retornou = false;
new Thread(autenticacao).start();
while (!retornou) {
try {
autenticacao.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (autenticacao.infoValida()) {
System.out.println("passou");
} else {
System.out.println("nao passou");
}
}
}
}
}
Os metodos da classe Autenticacao invocaServlet() e recebeDados() funcionam. Já testei separadamente.
O problema é na chamada e resposta da thread.
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import main.Login;
/**
* @author Particular
*/
public class Autenticacao implements Runnable {
private String login, senha, infoRecebidas;
HttpConnection http = null;
InputStream inputStream = null;
public Thread thread;
public boolean terminou = false;
Login telaLogin;
//getter and setters
public Autenticacao(Login telaLogin, String login, String senha) {
this.login = login;
this.senha = senha;
this.telaLogin = telaLogin;
thread = new Thread(this);
thread.start();
}
public void run() {
synchronized (this) {
try {
invocaServlet();
infoRecebidas = recebeDados();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
telaLogin.retornou = true;
notify();
}
}
public boolean infoValida(){
if (infoRecebidas == null){
return false;
}
else{
return true;
}
}
public void invocaServlet(){
String url = "http://localhost:8080/teste";
try {
// Cria e abre uma conexao HTTP
http = (HttpConnection) Connector.open(url);
http.setRequestMethod(HttpConnection.GET);
}
catch (Exception e) {
e.printStackTrace();
}
}
public String recebeDados() {
String str = null;
try {
inputStream = http.openInputStream();
if (http.getResponseCode() == HttpConnection.HTTP_OK) {
int length = (int) http.getLength();
if (length != -1) {
byte servletData[] = new byte[length];
inputStream.read(servletData);
str = new String(servletData);
}
// Tamanho nao disponivel
else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch;
while ((ch = inputStream.read()) != -1) {
baos.write(ch);
}
str = new String(baos.toByteArray());
baos.close();
}
} else {
// Se mensagem diferente de HTTP_OK entao mostra o erro
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fechaConexao(http, inputStream);
return str;
}
}
public void fechaConexao(HttpConnection http, InputStream inputStream) {
try {
if (inputStream != null) {
inputStream.close();
}
if (http != null) {
http.close();
}
} catch (Exception e) {
}
}
}
Obrigado desde já.