Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to send multiple Volley Requests - in a sequence

I need to use volley to send a request to retrieve a membershipid then pass that membership id into the second volley request to retrieve stats on that member.

I have a problem with my first request working perfectly but the second request seems to start before the variable is returned to be passed. Anyone know how to prevent the second request from starting before value is returned?

like image 208
Randy Avatar asked Oct 20 '15 05:10

Randy


People also ask

How can I call multiple requests at the same time in volley?

To achieve it without using any patterns or other libraries, you can mark the request as finished if it responded, and call the method, in each of them, you want to execute if all the requests are finished. On that method, you just need to check if all the requests are done.

What is request queue in volley?

requestQueue is used to stack your request and handles your cache. You need to create this RequestQueue in your application class or in a Singleton class. Then only you can use the same requestQueue from multiple activities.

What is volley dependency?

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network connections.

Is volley a REST API?

Android Volley and Retrofit are the most used libraries for accessing the REST Web APIs today.


1 Answers

you can't just write each request sequentially and wait to perform each after each success response ... you have to call second request inside first service response... ie

public void firstServiceCall(String url)
{
      JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                     int membershipid=response.getInt("membershipid");
                     //suppose the membershipid comes under main json with key "membershipid"
                     secondServiceCall(membershipid,<url2>);
                     // on the response of first service ... call to the second service ... and continue so on... if required
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    Volley.newRequestQueue(getApplicationContext()).add(jsonObjReq);
  }
  public void secondServiceCall(int membershipid,String url)
  {
       // use this var membershipid acc to your need ... 
       JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    Volley.newRequestQueue(getApplicationContext()).add(jsonObjReq);
  }

also the request call is asynchronous hence... the other process won't wait for service call to finish...hence your second service starts before the first service response

like image 81
Angad Tiwari Avatar answered Sep 21 '22 05:09

Angad Tiwari