我的问题是我想在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() {
}
}
}