Ajuda com j2me - HttpConnection

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;
    }   

}

[quote=Licuri]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;
    }   

}

[/quote]

E sempre que eu tento conectar ele da esta mensagem no browser

Eu chamo desta form…
Tenho uma Midlet de uma tela de Login e eu tenho que validar o usuario e senha na minha Servlet…

A mensagem ocorrre quando entra nesta linha

String retorno = teste.testGET("http://localhost:8080/Servlet")
Teste teste = new Teste();
String retorno = teste.testGET("http://localhost:8080/Servlet")
form.append(retorno)

Ninguém…será que não entederam a pergunta?

não me lembro dessa mensagem especificamente, mas resolvi um problema parecido colocando minha “conexão” numa thread…

Isso não é um erro, é apenas um aviso de que algumas operações, como uma conexão de rede por exemplo, você deve executar em uma thread diferente.

Tenta fazer a conexão por POST assim ó:

conn = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.1");
            // Para permitir que o servlet use o método getParameter deve usar o comando a baixo
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            encoded = "email="+email+"&password="+password+"&accId="+accId;
            conn.setRequestProperty("Content-Length", Integer.toString(encoded.length()));
 // Mostra informações do servidor!
getConnectionInformation(conn);
os = conn.openOutputStream();
// encode data
System.out.println("encoded: "+encoded);
            
byte postmsg[] = encoded.getBytes();
 for(int i=0;i<postmsg.length;i++) {
      os.write(postmsg[i]);
 } 

...

Você pode usar esse método aqui pra ver as informações da conexão também:

    private void getConnectionInformation(HttpConnection hc) {
        
        System.out.println("Request Method for this connection is " + hc.getRequestMethod());
        System.out.println("URL in this connection is " + hc.getURL());
        // It better be HTTP:)
        System.out.println("Protocol for this connection is " + hc.getProtocol());
        System.out.println("This object is connected to " + hc.getHost() + " host");
        System.out.println("HTTP Port in use is " + hc.getPort());
        System.out.println("Query parameter in this request are  " + hc.getQuery());
        
    }

E como o srogerio disse, coloca tudo que for de conexão numa outra Thread (SuaClass implements Runnable) pq se não vai travar tudo no seu celular.

=)

Senhores,

ACHO que o problema pode ser resolvido de outra forma, que não numa thread. Vai no properties da aplicação->signing e seleciona sign distribution, depois em Alias põe trusted…só isso =)

amigo,

vc conseguiu resolver o problemas, pois acho que estou enfrentando a mesma situação.

Estou tendo problemas em acessar via post o ws, passando uma string para validação.

caso tenha conseguido, entre em contato comigo pelo email anteromendes@yahoo.com.br

att,