0% found this document useful (0 votes)
14 views2 pages

8 - MainActivity (Class)

The document outlines an Android activity that handles user login functionality. It initializes UI components, checks for empty fields, and makes an HTTP POST request to validate user credentials. Depending on the response, it either shows an error message or starts a new activity based on the user's role.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

8 - MainActivity (Class)

The document outlines an Android activity that handles user login functionality. It initializes UI components, checks for empty fields, and makes an HTTP POST request to validate user credentials. Depending on the response, it either shows an error message or starts a new activity based on the user's role.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//------ VARIABLES ------

private EditText etusu, etpas;


private Button btnlog;
private AsyncHttpClient cliente;

//------ INICIALIZACI�N DE VARIABLES ------

etusu = (EditText) findViewById(R.id.etusu);


etpas = (EditText) findViewById(R.id.etpas);
btnlog= (Button) findViewById(R.id.btnlog);
cliente = new AsyncHttpClient();

//------ LLAMADA DE METODOS DENTRO DE ONCREATE ------

botonLogin();

//------ METODOS DE BOTONES ------

private void botonLogin(){


btnlog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(etusu.getText().toString().isEmpty() ||
etpas.getText().toString().isEmpty()){
Toast.makeText(MainActivity.this, "Hay Campos En Blanco!!",
Toast.LENGTH_SHORT).show();
etusu.setText("");
etpas.setText("");
}else{
String usu = etusu.getText().toString().replace(" ","%20");
String pas = etpas.getText().toString().replace(" ","%20");
String url =
"https://app1movilmalj.000webhostapp.com/comprobarUsuario.php?
Usuario="+usu+"&Password="+pas;
cliente.post(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
if(statusCode == 200) {
String respuesta = new String(responseBody);
if (respuesta.equalsIgnoreCase("null")) {
Toast.makeText(MainActivity.this, "Error De
Usuario y/o Contrase�a!!", Toast.LENGTH_SHORT).show();
etusu.setText("");
etpas.setText("");
} else {
try {
JSONObject jsonObj = new
JSONObject(respuesta);
Usuario u = new Usuario();
u.setId(jsonObj.getInt("id_usu"));
u.setNombre(jsonObj.getString("nom_usu"));

u.setContrase�a(jsonObj.getString("pas_usu"));
u.setId_tip(jsonObj.getInt("id_tip"));
Intent i = null;
switch(u.getId_tip()){
case 1:
u.setNom_tipo("Administrador");
i = new
Intent(MainActivity.this,Main2Activity.class);
break;
case 2:
u.setNom_tipo("Operador");
i = new
Intent(MainActivity.this,Main3Activity.class);
break;
case 3:
u.setNom_tipo("Cliente");
i = new
Intent(MainActivity.this,Main4Activity.class);
break;
}
i.putExtra("u",u);
startActivity(i);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(MainActivity.this, "Error Desconocido.
Intentelo De Nuevo!!", Toast.LENGTH_SHORT).show();
etusu.setText("");
etpas.setText("");
}
});
}
}
});
} // Cierra el metodo botonLogin

You might also like