简体   繁体   中英

Synchronous request with volley android

I have a problem with the update of a global variable through a set method. The situation in short is the following:

  • Use a class (SessionHandler is the name of this class) to store information about the current user session (logged in), then within a set method, which sets me a Boolean value to true if the user is logged in, false otherwise. The variable is initialized to FALSE.

  • Using a library to implement the login logic with facebook, google plus, personalized login. Within this class there is a method (customSignin) that returns a Boolean based on whether the login has been successful or not.

      @Override public boolean customSignin(SmartUser smartUser) { params = new ArrayList<>(); params.add(smartUser.getUsername()); params.add(smartUser.getPassword()); StringRequest strReq = new LoginStringRequest().getStringRequest(params); AppController.getInstance().addToRequestQueue(strReq); if(SessionHandler.logged){ return true; } else { return false; } } 

Then TRUE = logged in user, FALSE = User not logged in. Obviously if the user exists in the database, the value must be set to true. The problem is that as you can see from the posted code: Within this method, I make a call to another class that takes care of checking that the entered values ​​(username, password) are correct, doing a check on the database, to do this use the library Volley, the problem is that being an asynchronous task, within the Volley onResponse method, if the data is correct septum to TRUE via a set method the value of the Boolean variable LOGGED. I need later in the method of the first (customSignin) to verify the value of the Boolean variable which is always set to FALSE even if the login information is correct. (Then continue checking the Boolean starting from education IF). This is the code of the volley class :

Code of Volley request

Now wondering if anyone can tell me how I can solve this problem, you can avoid using synchronous task to resolve the situation? in particular, it gives me that through volleyball, is set to true if the user exists in the database and then continue running in customSignin method.

sorry for the imperfections, unfortunately use a translator, thanks to those who will know how to help me.

@Applefriend As per the description what I understood is that the code inside the CustomSignIn method makes a network call asynchronously using volley which will set the session info later when the request is complete.

So when you say

AppController.getInstance().addToRequestQueue(strReq); // Dispatch async call

if(SessionHandler.logged){ // This happens in the current UI thread (which be returning the default value or an exception
    return true;
} else {
    return false;
}

Since it is user login functionality, why do you want it in an async manner? Better way would be: show an indeterminate progress dialog and make the call synchronously and return the value.

Correct me if my understanding is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM