Wait for result of Async Volley request and return it
Asked Answered
I

2

6

Below is a method in which I am trying to retrieve an user object by calling getSelf(). Problem is that the result is always null since the Volley request has not finished at the time of returning the result. I'm somewhat new to async processes, so I am not sure of the best way to have the method wait for the result of the API call to return the UserBean object. Can anyone give me some help?

public UserBean getSelf(String url){

    RpcJSONObject jsonRequest = new RpcJSONObject("getSelf", new JSONArray());

    JsonObjectRequest userRequest = new JsonObjectRequest(Request.Method.POST, url, jsonRequest, 
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                String result;
                try {
                    result = response.getString("result");
                    Gson gson = new Gson();
                    java.lang.reflect.Type listType = new TypeToken<UserBean>() {}.getType();

                    //HOW DO I RETURN THIS VALUE VIA THE PARENT METHOD??
                    userBean = (UserBean) gson.fromJson(result, listType);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
               Log.e("Error:", error.toString());
               finish();
            }
        }
    );

    this.queue.add(userRequest);


    return userBean;

}   
Immature answered 16/10, 2013 at 16:44 Comment(2)
You shouldn't do what you're trying to do. The reason for async processing is so that you won't block the program or UI while doing "slow" things. So your onResponse should notify the caller that the object is available, and then display it. If you need the user to wait, throw up a progress dialog and then dismiss it when the result is available.Peba
Also check your response. It could be null.Forepart
E
16

For those coming to this question from search & google.

There is no reason to wait for an async request to finish, as it is asynchronous by design. If you want to achieve synchronous behaviour using Volley, you have to use so-called futures:

String url = "http://www.google.com/humans.txt";

RequestFuture<String> future = RequestFuture.newFuture();
StringRequest request = new StringRequest(Request.Method.GET, url, future, future)
mRequestQueue.add(request);

String result = future.get(); // this line will block

Keep in mind that you have to run blocking code in another thread, so wrap it into AsyncTask (otherwise future.get() will block forever).

Embellish answered 26/6, 2015 at 13:53 Comment(0)
W
0

You can achieve this using the library VolleyPlus https://github.com/DWorkS/VolleyPlus

It has something called VolleyTickle and RequestTickle. Request is the same. It is synchronous Request and only one request at time.

Woodworm answered 7/3, 2014 at 12:5 Comment(2)
i think in VolleyPlus: if cache found its takes from cache and responds back to the UI main thread. It makes me the problem because it did not update the data if JSON is updated. Any solution for this problem ??Logotype
you can use the method setShouldCache in request. Pass false to the method and it will not cache the results.Woodworm

© 2022 - 2024 — McMap. All rights reserved.