Send rest request after async responses volley

141 views Asked by At

I searched about this topic and I didn't found something to clear my doubts. My problem is that I must fill some form fields with data that I have in a server using a rest api.

Basically I throw 3 different independent requests, as they are independent I throw them with async functions. i.e:

        JsonArrayRequest req = new JsonArrayRequest(URL, new Response.Listener<JSONArray> () {

        @Override
        public void onResponse(JSONArray response) {
            try{
                //My code to compound a list from JSON response
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("INFO:", "error consultando api");
            error.printStackTrace();


        }
    });

I don't know how to achieve the following. I need to send another request that depends on the info gathered by the 3 previous request. Is there an elegant way to detect that the 3 previous requests have finished and after that launch my last request?

I tried to put a boolean flag in each request callback and block my app with a for loop until all the flags are true but I think it's a bad code.

Thank you very much

1

There are 1 answers

2
Krishna V On BEST ANSWER

put listener for those request for counting the responses(it is either onResponse or onErrorResponse). If count is reached to 3 then send 4th request.