Need to send multiple Volley Requests - in a sequence
17,752
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
Related videos on Youtube
Author by
Randy
Updated on August 29, 2022Comments
-
Randy over 1 year
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?
-
Randy over 8 yearsI've been looking through the volley tools to make that happen already. Couldn't figure it out. Why I'm asking someone to tell me.
-
Shivam Sharma over 6 years@Randy thank you bro, It was actually what I searching from last 2 days.
-
-
Randy over 8 yearsQuestion, this would be great to execute but I'm unsure how I would get the values out of the nested calls. What would you do in this case? Thanks for the help
-
Angad Tiwari over 8 years@Randy .. i'm supposing your first service response contain membershipid .. which you need to pass on second service .... i'm right so ? then , for this case.. i've updated my answer... if not ... then plz show your work and help me understand your real question...
-
Randy over 8 yearsFor my question this is a good solution. Thank you for your answer
-
JDOaktown almost 8 years>"you can't just write each request sequentially and wait to perform each after each success response " Actually you CAN write each request sequentially and wait to perform each after each success response. Pls. see my answer below.
-
Talha over 7 yearsNot helping for which I came here. Can you generalize the code for others to understand? Like with 2 simple requests? If you chaining logic work, this could be great addition to add to API feature. Also for people like me to understand the logic better.
-
JDOaktown over 7 yearscheck this out: stackoverflow.com/questions/39521956/…