Variable not changing properly when called in a Volley JsonObjectRequest

Viewed 23

I have a boolean function called isAlreadyInDatabase(String email, String password) which checks if an email address and a password are already present on a database (an excel file).

To do so, the method creates an EmailAndPasswordGet object and calls a checkEmailAndPassword(email, password) method. Here's the code:

public boolean isAlreadyInDatabase(String email, String password) {
    return new EmailAndPasswordGet(context).checkEmailAndPassword(email, password);
}

Now the EmailAndPasswordGet code:

public class EmailAndPasswordGet {
private final Context context;

private String email;
private String password;

private static boolean successful;

private final JsonObjectRequest validEmailAndPasswordRequest = new JsonObjectRequest(Request.Method.GET, Costanti.URL_DB, null, response -> {
    try {
        JSONArray array = response.getJSONArray("values");
        for (int i = 1; i < array.length(); i++) {
            JSONArray row = array.getJSONArray(i);
            if (row.get(4).equals(email) && row.get(5).equals(password)) {
                System.out.println("Match found!");
                successful = true;
                
                break;
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}, null);

public EmailAndPasswordGet(Context context) {
    this.context = context;
    successful = false;
}

public boolean checkEmailAndPassword(String email, String password) {
    this.email = email;
    this.password = password;

    Volley.newRequestQueue(context)
            .add(validEmailAndPasswordRequest
                    .setRetryPolicy(
                            new DefaultRetryPolicy(
                                    Costanti.SOCKET_TIME_OUT,
                                    0,
                                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)));

    System.out.println("Match: " + successful);

    return successful;
}

I used a boolean variable successful to know if something went wrong or if a match was found.

Now the problem: when I try to run my application (correct email and password) the console outputs this:

I/System.out: Match: false
I/System.out: Match found!

Theoretically, the Match found! message should appear before the other one. As you can see by the code when the request is running, it should output Match found!, change successful = true and then output Match: true. This doesn't happen.

As demonstrated the match is found, but then it just returns false which is not right. The only thing that comes to mind is that the successful variable just doesn't work between scopes or the request is delayed somehow so that it doesn't update the variable properly. I have no idea.

So what's wrong? Am I missing something on how requests work? Does the variable work this way? I just wanna know if a match was found or not, how can I do it?

Forgive me if I wrote the question badly, first time around here!

Have a great day, thank you! :)

0 Answers
Related