At first I tried to bring the data that was entered during the registration to a new activity, but change your mind, I just want that in case of having a successful session start, when I hit the "enter" button, send me to another activity I'm connected to a database (mysql) 1) I know that to bring the data from the database to compare them with the data entered in the login activity, that goes in the if but I do not know how to develop the code (I'm new programming)
Note: In my database the user is called "user_name" and the password is "password" The activity in which I find myself is called "login" and to which I would go in case of a true real comparison "Welcome" This is my Login code (where you enter the data to log in):
public class Login extends AppCompatActivity {
TextView tv_registrar;
EditText etapellidos,etusuario, etClave;
Button entrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
/*etnombre = findViewById(R.id.nom);*/
etapellidos = findViewById(R.id.ape);
etusuario = findViewById(R.id.usu);
/*etcertificacion = findViewById(R.id.cert);*/
etClave = findViewById(R.id.pass);
entrar= findViewById(R.id.entrar);
tv_registrar=findViewById(R.id.tv_registrar);
tv_registrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { /*enlace de texto registro con activity Registro*/
Intent intentReg = new Intent(Login.this, Registro.class);
Login.this.startActivity(intentReg);
}
});
entrar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
final String user_name=etusuario.getText().toString();
final String password=etClave.getText().toString();
Response.Listener<String> responListener = new Response.Listener<String>() {
/*Response.Listener<String> responseListener = new Response.Listener<String>() {*/
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success){
Intent intent = new Intent(Login.this,Bienvenido.class);
Login.this.startActivity(intent);
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setMessage("ERROR EN EL INICIO DE SESION")
.setNegativeButton("Retry",null)
.create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
SolicitudDeInicio solicitudDeInicio = new SolicitudDeInicio(user_name,password,responListener);
/*CLASE PARA COMUNICARSE CON VOLLEY*/
RequestQueue queue = Volley.newRequestQueue(Login.this);
queue.add(solicitudDeInicio);
}
});
}
this is my conecion.php file:
<?php
$con = mysqli_connect("localhost", "root", "", "usuarios");
$user_name = $_POST["user_name"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM usuarios WHERE user_name = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $user_name, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$name,$surname, $user_name, $course,$password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["name"] = $name;
$response["surname"] = $surname;
$response["user_name"] = $user_name;
$response["course"] = $course;
$response["password"] = $password;
}
echo json_encode($response);
? >
thanks