midlet abre conexão http mas não se conecta ao banco........

05/09/2007

0

pessoal tenho o seguinte código:

private void conectarHTTP() throws IOException{
        Thread t = new Thread(new Runnable() {
            public void run() {
                HttpConnection http = null;
                InputStream istrm = null;
                OutputStream ostrm = null;
                boolean ret = false;
                final String url = "http://localhost:8084/TesteMidlet/TesteMidlet";
                try
                {
                       http = (HttpConnection)Connector.open(url);
                       http.setRequestMethod(HttpConnection.POST);
                       http.setRequestProperty("Conexão", "close");
                       ostrm = http.openOutputStream();
                       byte data[] = ("login=" + textFieldLogin.getString()).getBytes();
                       ostrm.write(data);
                       data = ("&senha=" + textFieldSenha.getString()).getBytes();
                       ostrm.write(data);
                       ostrm.flush();
                       istrm = http.openInputStream();
                       ret = processServerResponse(http, istrm);
                }
                finally
                {
                    if(istrm != null)
                        
                        try {
                            istrm.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    if(ostrm != null)
                        
                        try {
                            ostrm.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    if(http != null)
                        
                        try {
                            http.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                }     
                if( ret == false)
                    get_alert2(msg);
        }
    }); t.start();
    }
            
            private boolean processServerResponse(HttpConnection http, InputStream istrm) throws IOException{
                String errorMsg;
                errorMsg = null;
                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();
                    }
                    return true;
                } else
                    errorMsg = new String(http.getResponseMessage());
                return false;
            }
            

[u:0c7a279b49]
este é o código da midlet.. ela abre a conexão http, até consegue se comunicar com o post (vi isso pelo monitor HTTP do netbeans) porém ñ está se comunicando com o banco de dados [/u:0c7a279b49]

[b:0c7a279b49]aki está a servlet[/b:0c7a279b49]

public class TesteMidlet extends HttpServlet {
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        String login_ = request.getParameter("login");
        String senha_ = request.getParameter("senha");
String logar = loginLookup(login_, senha_);
 if (logar == null) {
            response.sendError(response.SC_BAD_REQUEST, "Não foi possível encontrar login/senha");
            return;
        }
 response.setContentType("text/plain");
 PrintWriter out = response.getWriter();
        out.print(logar);
      out.close();
        
    }
    
private String loginLookup(String login_, String senha_) {
        String banco = "jdbc:mysql://localhost:3306/cissa";
        String usuario = "root";
        String pass = "123456";
        Connection con = null;
        StringBuffer msgb = new StringBuffer("");
        
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            con = DriverManager.getConnection(banco, usuario, pass);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("Select (login, senha) FROM usuario WHERE login= " +login_ + "and senha = ´ " + senha_ + " ´ ");
            if(rs.next()) {
                return rs.getString(1);
            }
        } catch(Exception e) {
            return e.toString();
        }
        return loginLookup(login_, senha_);
    }

   public String getServletInfo() {
        return "Short description";
    }


[b:0c7a279b49]eu não sei mais o q fazer
eu preciso q a midlet se conecte ao banco para verificar se o usuario ja existe no banco ou nao... então pelos parametros login/senha q serão enviados pela midlet a servlet o método do post deveria tratar isso com o banco de dados... mas algo ñ está certo em algum lugar
eu estou aprendendo agora a trabalhar com tudo isso ai, estou tendo dificuldades em programar por falta de um material mais objetivo e claro... preciso desesperadamente entender oq está acontecendo, resolver e aprender... é pro meu tcc...
grata[/b:0c7a279b49]


Cissagatto

Cissagatto

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar