Toast She’s in trouble

Asked

Viewed 88 times

0

Man Toast is being shown even when my Else is not called; it was to be shown only when the user enters wrong password or email, but even when the equals works, the Toast appears:

protected void onPostExecute(final String result) {

    super.onPostExecute(result);


    try {

        JSONObject json = new JSONObject(result);

        System.out.println(json.getString("resource"));
        JSONArray array = new JSONArray(json.getString("resource"));
        for (int i = 0; i < array.length(); i++) {

            JSONObject jsonObj = array.getJSONObject(i);
            System.out.println("Email : " + jsonObj.getString("tx_email"));
            System.out.println("Password : " + jsonObj.getString("password"));
            String email = jsonObj.getString("tx_email");
            String password = jsonObj.getString("password");
            String nickteste = jsonObj.getString("tx_nickname");
            String cellteste = jsonObj.getString("nu_cellphone");
            String snome = jsonObj.getString("tx_name");


            if (mEmail.equals(email) && mPassword.equals(password)) {


                Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
                //startActivity(intent);

                //Intent ii = new Intent(LoginActivity.this, ActivityAttCad.class);
                intent.putExtra("mEmail", email);
                intent.putExtra("mPassword", password);
                intent.putExtra("mNick", nickteste);
                intent.putExtra("mCellphone", cellteste);
                intent.putExtra("mNome", snome);
                startActivity(intent);

            }

            else {


                final Toast toast = Toast.makeText(getApplicationContext(), "Email ou senha inválido(s)", Toast.LENGTH_SHORT);

                toast.show();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toast.cancel();
                    }
                }, 500);
            }
        }





    } catch (JSONException e) {
        e.printStackTrace();
    }}}}
  • Maybe it’s my fault but the if part (mEmail.equals(email) && mPassword.equals(password)) { ... shouldn’t be outside the loop for ?

  • From what I understand, you are comparing various read data from a JSON file with email and password. Probably only one of these data corresponds to the condition in if, thus, in the others, he falls into the else

  • The code is correct. I suggest debugging to analyze better.

1 answer

0

You are testing the user/password within a loop that scans the entire list of users returned from the server, so if-Lse is running several times until the end of the loop. At some point in the loop, the user/password match the expected (and does not show Toast) and in the other loop iterations (probably all others), the user/password do not match and display Toast.

I would modify the loop that way:

   for (int i = 0; i < array.length(); i++) {

        JSONObject jsonObj = array.getJSONObject(i);
        System.out.println("Email : " + jsonObj.getString("tx_email"));
        System.out.println("Password : " + jsonObj.getString("password"));
        String email = jsonObj.getString("tx_email");
        String password = jsonObj.getString("password");
        String nickteste = jsonObj.getString("tx_nickname");
        String cellteste = jsonObj.getString("nu_cellphone");
        String snome = jsonObj.getString("tx_name");


        if (mEmail.equals(email)){
            if(mPassword.equals(password)) {

                Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
                //startActivity(intent);

                //Intent ii = new Intent(LoginActivity.this, ActivityAttCad.class);
                intent.putExtra("mEmail", email);
                intent.putExtra("mPassword", password);
                intent.putExtra("mNick", nickteste);
                intent.putExtra("mCellphone", cellteste);
                intent.putExtra("mNome", snome);
                startActivity(intent);
            }

            else {


                final Toast toast = Toast.makeText(getApplicationContext(), "Email ou senha inválido(s)", Toast.LENGTH_SHORT);

                toast.show();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        toast.cancel();
                    }
                }, 500);
            }
            break;
        }
    }
  • Error on this line : if (mEmail.equals(email){ if(mPassword.equals(password)) {

  • I’ve made the correction, great code ,thank you

  • The code is correct. I suggest debugging to analyze it better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.