Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3. GlobalVariables globalVariables;
  4.  
  5. EditText miUser;
  6. EditText miPass;
  7.  
  8. Boolean UsrOk;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_login);
  14.  
  15. globalVariables = (GlobalVariables)getApplicationContext();
  16.  
  17. miUser = (EditText) findViewById(R.id.eUser);
  18. miPass = (EditText) findViewById(R.id.ePass);
  19. }
  20.  
  21. public void Login(View view) {
  22. UsrOk = false;
  23. new MiUser().execute("http://10.0.3.2/baradm/sesion.php?id="+miUser.getText().toString());
  24. if (UsrOk = true){
  25. new MiTurno().execute("http://10.0.3.2/baradm/turno.php?id=+globalVariables.getUsr());
  26. }
  27. }
  28.  
  29. private class MiUser extends AsyncTask<String, Void, String> {
  30. @Override
  31. protected String doInBackground(String... urls) {
  32. // params comes from the execute() call: params[0] is the url.
  33. try {
  34. return downloadUrl(urls[0]);
  35. } catch (IOException e) {
  36. return "Unable to retrieve web page. URL may be invalid.";
  37. }
  38. }
  39. // onPostExecute displays the results of the AsyncTask.
  40. @Override
  41. protected void onPostExecute(String result) {
  42. try {
  43. JSONArray ja = new JSONArray(result);
  44. // SE VERIFICA QUE EL USUARIO DE LA BD SEA EL MISMO DE EDIT
  45. if (!ja.getString(1).equals(miUser.getText().toString())) {
  46. Toast toast = Toast.makeText(LoginActivity.this,"USUARIO INACTIVO O NO EXISTE",Toast.LENGTH_LONG);
  47. toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
  48. toast.show();
  49. } else if (!ja.getString(2).equals(miPass.getText().toString())) {
  50. Toast toast = Toast.makeText(LoginActivity.this,"CONTRASEÑA INVALIDA",Toast.LENGTH_LONG);
  51. toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
  52. toast.show();
  53. } else {
  54. // SE VERIFICA QUE SEA MESERO Y TENGA TURNO ASIGNADO
  55. globalVariables.setUsr(ja.getString(0));
  56. UsrOk = true;
  57. }
  58. } catch (JSONException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63.  
  64. private class MiTurno extends AsyncTask<String, Void, String> {
  65. @Override
  66. protected String doInBackground(String... urls) {
  67. // params comes from the execute() call: params[0] is the url.
  68. try {
  69. return downloadUrl(urls[0]);
  70. } catch (IOException e) {
  71. return "Unable to retrieve web page. URL may be invalid.";
  72. }
  73. }
  74. // onPostExecute displays the results of the AsyncTask.
  75. @Override
  76. protected void onPostExecute(String result) {
  77. try {
  78. JSONArray ja = new JSONArray(result);
  79. Toast toast = Toast.makeText(LoginActivity.this,"NO PUEDES INGRESAR COMO "+ja.getString(1)+"/n SOLO MESEROS",Toast.LENGTH_LONG);
  80. toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
  81. toast.show();
  82. /*if (!ja.getString(0).equals("04")) {
  83. Toast toast = Toast.makeText(mContext,"NO PUEDES INGRESAR COMO "+ja.getString(1)+"/n SOLO MESEROS",Toast.LENGTH_LONG);
  84. toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
  85. toast.show();
  86. }*/
  87. } catch (JSONException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92.  
  93.  
  94.  
  95. private String downloadUrl(String myurl) throws IOException {
  96. Log.i("URL",""+myurl);
  97. myurl = myurl.replace(" ","%20");
  98. InputStream is = null;
  99. // Only display the first 500 characters of the retrieved
  100. // web page content.
  101. int len = 500;
  102.  
  103. try {
  104. URL url = new URL(myurl);
  105. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  106. conn.setReadTimeout(10000 /* milliseconds */);
  107. conn.setConnectTimeout(15000 /* milliseconds */);
  108. conn.setRequestMethod("GET");
  109. conn.setDoInput(true);
  110. // Starts the query
  111. conn.connect();
  112. int response = conn.getResponseCode();
  113. Log.d("respuesta", "The response is: " + response);
  114. is = conn.getInputStream();
  115.  
  116. // Convert the InputStream into a string
  117. String contentAsString = readIt(is, len);
  118. return contentAsString;
  119.  
  120. // Makes sure that the InputStream is closed after the app is
  121. // finished using it.
  122. } finally {
  123. if (is != null) {
  124. is.close();
  125. }
  126. }
  127. }
  128.  
  129. public String readIt(InputStream stream, int len) throws IOException {
  130. Reader reader;
  131. reader = new InputStreamReader(stream, "UTF-8");
  132. char[] buffer = new char[len];
  133. reader.read(buffer);
  134. return new String(buffer);
  135. }
  136. }
  137.  
  138. new MiUser().execute("http://10.0.3.2/baradm/sesion.php?id="+miUser.getText().toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement