线程连接Postgresql db不起作用

时间:2016-05-07 12:29:43

标签: android database multithreading postgresql android-asynctask

我的问题是我想在android中连接到Postgresql的db,我看到我需要一个线程,我在主类的私有类中创建这个线程,但是不工作,“Toast”总是显示“Vacío”。我做得不好? :/

public class MainActivity extends AppCompatActivity {
    Button btnEntrar;
    EditText etUsuario;
    EditText etPass;

    public static String login = "Vacío";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnEntrar = (Button) findViewById(R.id.btnEntrar);
        etUsuario = (EditText) findViewById(R.id.etUsuario);
        etPass = (EditText) findViewById(R.id.etPass);

        btnEntrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usuario = etUsuario.getText().toString();
                String pass = etPass.getText().toString();
                new ConnUsers(usuario, pass).execute();


                Toast.makeText(MainActivity.this, login, Toast.LENGTH_SHORT).show();

                /*if(login){
                    Intent intent = new Intent(MainActivity.this, IncidenciasActivity.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "Acceso es TRUE", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(MainActivity.this, "Acceso es FALSE", Toast.LENGTH_SHORT).show();
                } */
            }
        });
    }

    private class ConnUsers extends AsyncTask <String, String, String>{

        private static final String DB_DRIVER = "org.postgresql.Driver";
        private static final String url = "jdbc:postgresql://xxx/xxx";
        private static final String user = "xxx";
        private static final String password = "xxx";

        private String usuario;
        private String pass;

        public ConnUsers(String usuario, String pass){
            this.usuario = usuario;
            this.pass = pass;
        }

        @Override
        protected String doInBackground(String... params) {
            String acceso = "doInBackground";

            try{
                Class.forName(DB_DRIVER);

                Connection connection = DriverManager.getConnection(url, user, password);
                Statement st = connection.createStatement();
                ResultSet rs = st.executeQuery("SELECT * FROM usuarios WHERE usuario = '" + usuario +"'");

                if(rs.next()!=false){
                    if(Funciones.md5(pass).equalsIgnoreCase(rs.getString("password"))){

                        int id = rs.getInt("id");

                        acceso = "Todo correcto";
                    }
                    else{
                        //Toast.makeText(context, "Password incorrecto", Toast.LENGTH_LONG).show();
                        acceso = "Falla la pass";
                    }
                }
                else{
                    //Toast.makeText(context, "Usuario incorrecto", Toast.LENGTH_LONG).show();
                    acceso = "Falla el usuario";
                }

                rs.close();
                st.close();
                connection.close();

            }catch(SQLException e){
                cancel(true);
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            return acceso;
        }

        @Override
        protected void onPostExecute(String s) {
            MainActivity.login = s;
        }

        @Override
        protected void onCancelled() {

        }
    }


}

1 个答案:

答案 0 :(得分:0)

也许你的连接无法访问PostgreSQL服务器以获得安全性。这意味着您的数据库仅用于 Localhost。因为某些服务器通常会直接阻止连接。 如果你的问题是,你应该使用 WebService。就像那样

enter image description here