Ae galera… Seguinte tenho uma classe que envia via get algumas informações para meu servidor.
Até ai normal… Funciona no Emulador para o KP500 e no emulador S40 5th edition.
No celular KP500 funciona tb, mas no celular Nokia 2730 ( S40 5th edition) não funciona…Alguem tem alguma idéia do que pode ser?
Vlw
Classe
import javax.microedition.io.*;
import java.io.*;
public class conexaoGET implements Runnable {
private static String defaultURL = "";
public static String urlstring = "";
private String str = null;
public static String resposta = null;
public static String errorMsg = null;
public conexaoGET() throws IOException {
}
public void run() {
HttpConnection http = null;
OutputStream oStrm = null;
InputStream iStrm = null;
boolean ret = false;
String url = getDefaultURL() + getUrlstring();
System.out.println("Conexao URL - " + url);
try {
http = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
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();
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();
}
setResposta(str);
ret = true;
} else
setErrorMsg(http.getResponseMessage());
} catch (IOException e) {
System.out.println("Erro - " + e);
} catch (Exception e) {
System.out.println("Erro - " + e);
} 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();
}
}
/*
* synchronized(Conex.resposta){
* System.out.println("Status is notifying"); Conex.resposta = str;
* Conex.resposta.notify(); }
*/
if (ret == false) {
System.out.println("erro - " + errorMsg);
}
}
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;
}
}
Midlet
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.Item;
//import javax.microedition.lcdui.ItemStateListener;
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 static String resposta = 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();
while (resposta == null) {
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();
}
}
}