[RESOLVIDO] HTTP Via GET funciona no emulador no celular não

Ae galera,

Ve se alguem pode me dar um help.

Fiz um midlet que conecta a uma pagina de teste, que só devolve ok.
No emulador foi perfeito… mas no celular ele mostra que conecta a internet, mas devolve o valor null.

Alguem tem idéia do que seja?

Segue código:

package CONEXGET;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Conex extends MIDlet implements CommandListener {
	private Command exit = new Command("Sair", Command.EXIT, 1);
	private static Display display;
	private List menu = null;
	public Conex() {
		display = Display.getDisplay(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		menu = new List("Menu Inicial", List.IMPLICIT);
		menu.append("Enviar", null);
		menu.addCommand(exit);
		menu.setCommandListener(this);
		display.setCurrent(menu);
	}

	public void commandAction(Command command, Displayable arg1) {
		if (command == exit) {  
            try {
				destroyApp(false);
			} catch (MIDletStateChangeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
            notifyDestroyed();  
        }else{
        	enviarGet();
        }
		
	}
	public void enviarGet(){
		try {
			
			conexaoGET.setDefaultURL("http://www.rodolforamos.com.br/testeM/testeComunicacao.php");
			conexaoGET.setUrlstring("?teste=teste");
			System.out.println("testeURL * "+conexaoGET.getDefaultURL());
			new Thread(new conexaoGET()).start();
			final String resposta = conexaoGET.getResposta();
			System.out.println("resp = "+resposta);
			menu = new List("Menu Inicial", List.IMPLICIT);
			menu.append("resp = "+resposta, null);
			menu.addCommand(exit);
			menu.setCommandListener(this);
			display.setCurrent(menu);
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
package CONEXGET;

import javax.microedition.io.*;
import java.io.*;

public class conexaoGET implements Runnable {

	private static String defaultURL = "";

	public static String urlstring = "";

	public static String resposta = null;

	public static String errorMsg = null;

	public conexaoGET() throws IOException {

		HttpConnection http = null;

		OutputStream oStrm = null;

		InputStream iStrm = null;

		boolean ret = false;

		String url = getDefaultURL() + getUrlstring();
		System.out.println("Conexao URL - " + url);
		try {
			System.out.println("teste 8 ");
			http = (HttpConnection)Connector.open(url); 
			System.out.println("teste 9 ");
			http.setRequestMethod(HttpConnection.GET);

			http.setRequestProperty("User-Agent",
					"Profile/MIDP-2.0, Configuration/CLDC-1.0");

			http.setRequestProperty("Content-type", "binary/x-java-serialized");

			http.setRequestProperty("Content-Language", "pt-BR");

			http.setRequestProperty("Content-Length", Integer
					.toString(getUrlstring().length()));

			iStrm = http.openInputStream();

			System.out.println("Conexao - HTTP OK - " + HttpConnection.HTTP_OK);

			if (http.getResponseCode() == HttpConnection.HTTP_OK) {

				int length = (int) http.getLength();

				String str;

				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();
				}
				System.out.println("Conexao STR - " + str);
				setResposta(str);
				ret = true;
			} else
				setErrorMsg(http.getResponseMessage());
		} catch (IOException e) {
			setErrorMsg("Erro ");
		} finally {
			if (iStrm != null)
				try {
					iStrm.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (oStrm != null)
				try {
					oStrm.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (http != null)
				try {
					http.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		if (ret == false) {
			System.out.println("erro - " + errorMsg);
		}
	}

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

	public static void setDefaultURL(String defaultURL) {
		conexaoGET.defaultURL = defaultURL;
	}

	public static String getDefaultURL() {
		return defaultURL;
	}

	public static void setErrorMsg(String erro) {
		conexaoGET.errorMsg = erro;
	}

	public static String getErrorMsg() {
		return errorMsg;
	}

	public static void setResposta(String resp) {
		conexaoGET.resposta = resp;
	}

	public static String getResposta() {
		return resposta;
	}

	public static void setUrlstring(String url) {
		conexaoGET.urlstring = url;
	}

	public static String getUrlstring() {
		return urlstring;
	}
}

Valew povo

Acho que vc faltou às aulas de Thread…
Isso não te diz nada?

new Thread(new conexaoGET()).start();  
final String resposta = conexaoGET.getResposta();

Sorry não entendi o que quiz dizer

Uma Thread dispara um processo concorrente.
Ou seja, quando vc dá um start nela, é criado um novo processo, e concorrentemente vc já tenta pegar seu resultado, sem a thread ter terminado…

Caraca…Pode cre…tinha esquecido disso.

Só uma pergunta, o que seria melhor: Fazer um synchronized/wait ou um while?

Valew

Obrigado

Melhor ainda, faz um listener :lol:

Verdade, Valew em…