Pessoal, estou tentando fazer um midlet conversar com um servlet via http connection. porem na hora de conectar ao servlet da uma mensagem de erro (Status 404). Alguem poderia me ajudar? sou novo no assunto e preciso disso para acabar meu tcc…
segue o codigo do midlet
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class teste extends MIDlet implements CommandListener
{
private Display display;
private Form fmMain;
private Alert alError;
private Command cmConectar;
private Command cmSair;
private TextField tfLogin;
private StringItem siNome;
private String errorMsg = null;
public teste()
{
display = Display.getDisplay(this);
cmConectar = new Command("Conectar", Command.SCREEN, 1);
cmSair = new Command("Sair", Command.EXIT, 2);
tfLogin = new TextField("Login", "", 3, TextField.NUMERIC);
siNome = new StringItem("Nome: ", "");
fmMain = new Form("Login");
fmMain.addCommand(cmConectar);
fmMain.addCommand(cmSair);
fmMain.append(tfLogin);
fmMain.append(siNome);
fmMain.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable s)
{
if(c == cmSair)
{
destroyApp(false);
notifyDestroyed();
}
if(c == cmConectar)
{
String url = "http://localhost:8084/testeServlet/teste.servlet";//teste.servlet?codigo=123";
System.out.println("iniciar thread");
Download dl = new Download(url, this);
dl.start();
}
}
class Download implements Runnable
{
private String url;
private teste MIDlet;
private boolean downloadSuccess = false;
public Download(String url, teste MIDlet)
{
this.url = url;
this.MIDlet = MIDlet;
}
public void run()
{
try
{
System.out.println("iniciar conexao com bd");
conectar(url);
}
catch(Exception e)
{
System.err.println("Msg: "+ e.toString());
}
}
public void start()
{
Thread thread = new Thread(this);
try
{
System.out.println("thread ativada");
thread.start();
}
catch(Exception e)
{
}
}
}
private void conectar(String url) throws IOException
{
HttpConnection http = null;
InputStream iStrm = null;
boolean ret = false;
//String url = "http://localhost:8084/testeServlet/servlet?login=123";
try
{
//String url1 = "http://localhost:8084/testeServlet/teste.servlet";//teste.servlet?codigo=123";
//String url1 = "http://localhost:8084/projetoFinal/editarCliente.jsp";
System.out.println("abrindo conexao");
http = (HttpConnection) Connector.open(url);
System.out.println("conexao aberta");
http.setRequestMethod(HttpConnection.GET);
iStrm = http.openInputStream();
ret = processServerResponse(http, iStrm);
}
finally
{
if(iStrm != null)
iStrm.close();
if(http != null)
http.close();
}
if(ret == false)
showAlert(errorMsg);
}
private boolean processServerResponse(HttpConnection http, InputStream iStrm) throws IOException
{
errorMsg = null;
System.out.println("abrindo dados");
if(http.getResponseCode() == HttpConnection.HTTP_OK)
{
System.out.println("lendo dados");
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();
}
siNome.setText(str);
System.out.println("enviando dados");
return true;
}
else
errorMsg = new String(http.getResponseCode() + http.getResponseMessage());
return false;
}
private void showAlert(String msg)
{
alError = new Alert("Error", msg, null, AlertType.ERROR);
alError.setTimeout(Alert.FOREVER);
display.setCurrent(alError, fmMain);
}
}
e o codigo do servlet
package teste;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class servlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
System.out.println("recebendo dados");
String login = req.getParameter("codigo");
String nome = consulta(login);
if(nome == null)
{
res.sendError(res.SC_BAD_REQUEST, "login errado");
return;
}
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print(nome);
out.close();
}
private String consulta(String login)
{
System.out.println("abrindo consulta de dados");
Connection con = null;
Statement st = null;
StringBuffer msgb = new StringBuffer("");
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tcc","root","");
Statement stmt = con.createStatement();
System.out.println("executar consulta ao bd");
ResultSet rs = stmt.executeQuery("SELECT * FROM usuario WHERE codigo = 123");// + login + "'" );
if(rs.next())
return rs.getString(1);
else
return null;
}
catch (Exception e)
{
return e.toString();
}
}
public String getServletInfo()
{
return "Fim";
}
}
valew