Galera esta dificil… Estou tentando buscar informações via HTTP quando eu uso uma midlet functiona…
/*
* ServiceWeb.java
*
* Created on February 17, 2007, 2:23 PM
*/
package br.com.stefanini.login;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
*
* @author HGLeite
* @version
*/
public class ServiceWeb extends MIDlet {
private Display display;
String url = "http://localhost:9080/WebService/HelloWebService";
public ServiceWeb() {
display = Display.getDisplay(this);
}
public void startApp() {
try {
testGET(url);
} catch (IOException e) {
System.out.println("IOException " + e);
e.printStackTrace();
}
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
void testGET(String url) throws IOException {
HttpConnection connection = null;
InputStream is = null;
OutputStream os = null;
StringBuffer stringBuffer = new StringBuffer();
TextBox textBox = null;
try {
connection = (HttpConnection)Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-CA");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os = connection.openOutputStream();
is = connection.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
stringBuffer.append((char) ch);
}
textBox = new TextBox("Teste simples de GET", stringBuffer.toString(), 1024, 0);
} finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(connection != null) {
connection.close();
}
}
display.setCurrent(textBox);
}
}
mais quando eu faço deste jeito não funciona… Eu preciso atraves de uma Midlet buscar informações via Http em uma Servlet… o problema que só funciona usando a Midlet direto…
public class Teste{
public String testGET(String url) throws IOException {
HttpConnection connection = null;
InputStream is = null;
OutputStream os = null;
StringBuffer stringBuffer = new StringBuffer();
TextBox textBox = null;
try {
connection = (HttpConnection)Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-CA");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os = connection.openOutputStream();
is = connection.openDataInputStream();
int ch;
while ((ch = is.read()) != -1) {
stringBuffer.append((char) ch);
}
retorno = stringBuffer.toString();
} finally {
if(is!= null) {
is.close();
}
if(os != null) {
os.close();
}
if(connection != null) {
connection.close();
}
}
return retorno;
}
}