Cordial greeting,
I have the following code:
public class LoginActivity extends AppCompatActivity {
GlobalVariables globalVariables;
EditText miUser;
EditText miPass;
Boolean UsrOk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
globalVariables = (GlobalVariables)getApplicationContext();
miUser = (EditText) findViewById(R.id.eUser);
miPass = (EditText) findViewById(R.id.ePass);
}
public void Login(View view) {
UsrOk = false;
new MiUser().execute("http://10.0.3.2/baradm/sesion.php?id="+miUser.getText().toString());
if (UsrOk = true){
new MiTurno().execute("http://10.0.3.2/baradm/turno.php?id="+globalVariables.getUsr());
}
}
private class MiUser extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
try {
JSONArray ja = new JSONArray(result);
// SE VERIFICA QUE EL USUARIO DE LA BD SEA EL MISMO DE EDIT
if (!ja.getString(1).equals(miUser.getText().toString())) {
Toast toast = Toast.makeText(LoginActivity.this,"USUARIO INACTIVO O NO EXISTE",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
} else if (!ja.getString(2).equals(miPass.getText().toString())) {
Toast toast = Toast.makeText(LoginActivity.this,"CONTRASEÑA INVALIDA",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
} else {
// SE VERIFICA QUE SEA MESERO Y TENGA TURNO ASIGNADO
globalVariables.setUsr(ja.getString(0));
UsrOk = true;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private class MiTurno extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
try {
JSONArray ja = new JSONArray(result);
Toast toast = Toast.makeText(LoginActivity.this,"NO PUEDES INGRESAR COMO "+ja.getString(1)+"/n SOLO MESEROS",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
/*if (!ja.getString(0).equals("04")) {
Toast toast = Toast.makeText(mContext,"NO PUEDES INGRESAR COMO "+ja.getString(1)+"/n SOLO MESEROS",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}*/
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private String downloadUrl(String myurl) throws IOException {
Log.i("URL",""+myurl);
myurl = myurl.replace(" ","%20");
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("respuesta", "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
public String readIt(InputStream stream, int len) throws IOException {
Reader reader;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
}
The part
new MiUser().execute("http://10.0.3.2/baradm/sesion.php?id="+miUser.getText().toString());
works fine but in debugging it does not give me the last else (it is verified that it is a deadline and has an assigned slot), the ideal is that if UsrOk is true, I will execute a new query. What's wrong ??