rafavinu rafavinu - 6 months ago 84
JSON Question

Android Volley | Extract json from multiple urls

I am currently using Volley to extract json contents using the following code.

JsonArrayRequest servicesStatus = new JsonArrayRequest(url1,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();

// Parsing json
for (int i = 0; i < response.length(); i++) {
try {

JSONObject obj = response.getJSONObject(i);
// Having obj to process further
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();

}
});


Now, I want one more json handler for the new url and the dialog to be closed once it has successfully downloaded from both the urls.

I tried to copy paste the above thing with url1 replaced by url2 and different jsonarrayrequest name. And added the hideDialog() in the second one. But the second one is not being called at all.

Can anyone please guide how to accomplish this.

Answer

If you want make mutiple request then you will have to add your Request to Queue. You can do it like this:

   RequestQueue request =  Volley.newRequestQueue(Context);
   request.add(FirstRequest);
   request.add(SecondRequest);

This should help you to add multiple request in Volley. I hope this was helpful. ThankYou

Comments