Shay Mishali Shay Mishali - 8 days ago 6
Android Question

How to make app "sleep" while Volley is in porgress

I'm working on a registration form.

I use Volley to check if an Email already exists (I connect to a PHP file which returns the answer).

My problem is that the app keeps working while I'm still waiting for Volley to return the answer.

Code:

if(isValidEmail(emailET.getText().toString())){
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
emailET.setError("Email is already in use");
}else{
System.out.println("Happening");
validator += "0";
}

} catch (JSONException e) {
e.printStackTrace();
}
}
};
EmailCheckRequest emailRequest = new EmailCheckRequest(emailET.getText().toString(), responseListener);
RequestQueue queue = Volley.newRequestQueue(AccountEmail.this);
queue.add(emailRequest);
}
System.out.println("validator=" + validator);
if(validator.matches("00")){
startActivity(dataCollector());
}


Now the problem is that this part of the code:

if(validator.matches("00")){
startActivity(dataCollector());
}


happens before this part of the code:

if(success){
emailET.setError("Email is already in use");
}else{
System.out.println("Happening");
validator += "0";
}


How can I make the app "sleep" and continue only after Volley stopped working?

Answer

Don't sleep the app. Properly wait for Volley by moving the code that performs the checks within the listener, at the end of onResponse

Comments