Reputation:
For example, in my first request I'm getting the movie's ID and in the second volley request, I'll be searching info about the movie by using the movie's ID I got in my firs request. Is there a better method to do this?
Upvotes: 1
Views: 3755
Reputation: 1474
There is a queue for request inside the volley library. You can add multiple requests into the queue. But in my opinion you should get all the data at the same time. It is not worth it for sending multiple requests for a movie(If you are not requests a lot of data).But if you wanna request multiple times you can callback from onResponse interface.
<!-- Inside the class variable -->
RequestQueue queue;
<!-- Inside the onCreate or whatever you want -->
queue = Volley.newRequestQueue(context);
<!--1st request for movie -->
try {
jsonObject.put("movieId",movieID);
jsonObject.put("somedata",somedata);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,YOUR_IP_ADDRESS,jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Call second request here and add it to queue again
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id","1");
params.put("name", "myname");
return params;
};
};
queue.add(jsObjRequest);
}catch (Exception ex){
ex.printStackTrace();
}
Upvotes: 3