Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. package com.example.hp.cesfam;
  2.  
  3.  
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.os.Build;
  7. import android.support.annotation.RequiresApi;
  8. import android.support.v7.app.AlertDialog;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. import static com.example.hp.cesfam.R.id.txtPass2;
  22.  
  23.  
  24. public class Login extends AppCompatActivity {
  25. private EditText rut,pass;
  26. TextView recoverpass;
  27. String txtRut, txtPass;
  28. Button registro, Login;
  29. UserSessionManager sesion;
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. try{
  35. sesion=new UserSessionManager(getApplicationContext());
  36.  
  37. ControllerActivity.actividadActual=this;
  38. setContentView(R.layout.activity_login);
  39.  
  40. rut= findViewById(R.id.txtRut2);
  41. pass=findViewById(txtPass2);
  42. recoverpass=findViewById(R.id.olvidar);
  43.  
  44. registro= findViewById(R.id.btnRegistro);
  45. registro.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. Intent startIntent = new Intent(getApplicationContext(), Registro.class);
  49. startActivity(startIntent);
  50. }
  51. });
  52. Login= findViewById(R.id.btnLogin);
  53. Login.setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View v) {
  56.  
  57. Login();
  58. }
  59. });
  60. recoverpass.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View view) {
  63. Intent i=new Intent(getApplicationContext(),RestablecerPass.class);
  64. startActivity(i);
  65. }
  66. });
  67.  
  68. }
  69. catch (Exception e)
  70. {
  71. String m = e.getMessage();
  72. Log.d("Error","Error: "+e.getMessage());
  73. }
  74.  
  75. }
  76. public void dialogo(){
  77. AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
  78. alertDialog.setCancelable(true);
  79. alertDialog.setMessage("Datos Incorrectos");
  80. alertDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
  81. @Override
  82. public void onClick(DialogInterface dialog, int which) {
  83. dialog.dismiss();
  84. pass.setText("");
  85. rut.setText("");
  86. }
  87. });
  88. AlertDialog alertDialog1=alertDialog.create();
  89. alertDialog1.show();
  90. }
  91. @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
  92. public void Login(){
  93. inicializar(); //hace la llamada cuando el boton guardar es pinchado para validar los datos ingresados
  94. if(!validar()){
  95. Toast.makeText(this,"El registro ha fallado", Toast.LENGTH_SHORT).show();
  96. }
  97. else{
  98.  
  99. JSONObject parametros = new JSONObject();
  100. try {
  101. parametros.put("rut", txtRut);
  102. parametros.put("password", txtPass);
  103. } catch (JSONException e) {
  104. e.printStackTrace();
  105. }
  106. new LoginClass.ValidarSesion().execute(parametros.toString());
  107.  
  108. }
  109.  
  110. }
  111.  
  112.  
  113. public boolean validar(){
  114. boolean valido=true;
  115. if(txtRut.isEmpty()){
  116. rut.setError("Rut invalido");
  117. valido=false;
  118. }
  119. if(txtRut.length()<9 || txtRut.length()>10){
  120. rut.setError("Debe ingresar un Rut valido");
  121. valido=false;
  122. }
  123. if(txtPass.isEmpty()){
  124. pass.setError("Ingrese contraseña");
  125. valido=false;
  126. }
  127. return valido;
  128. }
  129. public void inicializar(){
  130. txtRut=rut.getText().toString();
  131. txtPass= pass.getText().toString();
  132.  
  133. }
  134.  
  135. public void recibirMensajeValdiacion(JSONObject mensaje)
  136. {
  137. try{
  138. String status=mensaje.getString("status");
  139. String data=mensaje.getString("paciente");
  140.  
  141.  
  142. JSONObject mainObject = new JSONObject(data);
  143. String id=mainObject.getString("idpaciente");
  144. String idestado=mainObject.getString("estado");
  145.  
  146.  
  147. if(status.equals("true")){
  148.  
  149. Intent startIntent = new Intent(getApplicationContext(), Inicio.class);
  150. startActivity(startIntent);
  151. sesion.createUserLoginSession(id);
  152. sesion.createIdEstado(idestado);
  153.  
  154.  
  155. }
  156.  
  157. }
  158. catch (Exception e){
  159. e.printStackTrace();
  160. dialogo();
  161. }//"status":true,"data":"Paciente fue agregado exitosamente"*/
  162.  
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement