como posso fazer para passar o valor da variavel str da classe Conexao para o componente sibalance da classe princial. fiz mas nao funcionou
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class Principal extends MIDlet implements CommandListener {
private Display display;
public Form frmPrincipal;
Alert alError;
private Command cmPOST;
private Command cmExit;
private TextField tfAcct;
private TextField tfPwd;
private StringItem siBalance;
public Principal(){
display = Display .getDisplay(this);
cmPOST = new Command("Obter dados", Command.SCREEN, 3);
cmExit = new Command("Sair", Command.EXIT, 1);
tfAcct = new TextField("Conta : ", "", 5, TextField.NUMERIC);
tfPwd = new TextField("Password: ", "", 10, TextField.ANY | TextField.PASSWORD);
siBalance = new StringItem("Saldo: ", "");
//siBalance = new StringItem("Saldo: ", "");
frmPrincipal = new Form("Informações da conta");
frmPrincipal.addCommand(cmPOST);
frmPrincipal.addCommand(cmExit);
frmPrincipal.append(tfAcct);
frmPrincipal.append(tfPwd);
frmPrincipal.append(siBalance);
frmPrincipal.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(frmPrincipal);
//throw new UnsupportedOperationException("Not supported yet.");
}
protected void pauseApp() {
//throw new UnsupportedOperationException("Not supported yet.");
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
destroyApp(arg0);
//throw new UnsupportedOperationException("Not supported yet.");
}
public void commandAction(Command c, Displayable d) {
if (c == cmPOST){
try {
Conexao con = new Conexao(this.tfAcct.getString(), this.tfPwd.getString());
siBalance.setText(con.getDados());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
}else if(c == cmExit){
notifyDestroyed();
}
//throw new UnsupportedOperationException("Not supported yet.");
}
}
import javax.microedition.io.*;
import java.io.*;
public class Conexao extends Thread{
HttpConnection httpCon;
OutputStream Ostrm;
InputStream Istrm;
//private StringItem siBalance;
private String conta;
private String senha, str;
private boolean ok = false;
public Conexao(String conta, String senha){
this.conta = conta;
this.senha = senha;
this.start();
}
public void run(){
try{
String url = "http://localhost:8080/Servlets/GetNpostServlet"
+ "?" +
"account="+this.conta+"&password="+this.senha;
httpCon = (HttpConnection) Connector.open(url);
Ostrm = httpCon.openOutputStream();
httpCon.setRequestMethod(HttpConnection.POST);
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
byte data[] = ("account="+this.conta).getBytes();
Ostrm.write(data);
data = ("&password="+this.senha).getBytes();
Ostrm.write(data);
Ostrm.flush();
Istrm = httpCon.openInputStream();
boolean ret = ProcessaRespostaServidor(httpCon, Istrm);
} catch (IOException ex) {
ex.printStackTrace();
}finally{
if (ok == true){
getDados();
}
if (Ostrm != null)
try {
Ostrm.close();
} catch (IOException ex) {
ex.printStackTrace();
}
if (Istrm != null)
try {
Istrm.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public boolean ProcessaRespostaServidor(HttpConnection http, InputStream Istrm)
throws IOException{
String errorMsg = null;
if (http.getResponseCode() == HttpConnection.HTTP_OK) {
int length = (int) http.getLength();
if (length !=-1){
byte servletData[] = new byte[length];
Istrm.read(servletData);
str =new String(servletData);
}else {
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = Istrm.read()) != -1)
bStrm.write(ch);
str = new String(bStrm.toByteArray());
bStrm.close();
}
ok = true;
return true;
}else
errorMsg = new String(http.getResponseMessage());
ok = false;
return false;
}
public String getDados() {
return(this.str);
}
public void setDados(String str){
this.str=str;
}
}
