Toast else with problems

0

My toast is being shown even when my else is not called; was to be shown only when the user entered a wrong password or email, but even when equals works, 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();
    }}}}
    
asked by anonymous 01.06.2017 / 20:32

1 answer

0

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

I would modify the loop this 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;
        }
    }
    
01.06.2017 / 20:51