-1

The image contains the data which has no array name and I need to get the data which is in the image using volley.Help me with the code for it. -

PSKP
  • 1,178
  • 14
  • 28

2 Answers2

0

You have to use a JSONArrayRequest . When you parse the response, it's a ready to use JSONArray object. You can loop the array to get your JSONObject items by position.

This has been covered in this thread -> Volley - Sending a POST request using JSONArrayRequest

Steve NDENDE
  • 19
  • 1
  • 9
0

String url="https://earthquake.usgs.gov/archive/product/nearby-cities/ci39269503/ci/1593242325224/nearby-cities.json"; JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.GET, url, new Response.Listener() { @Override public void onResponse(JSONArray response) { StringBuilder stringBuilder=new StringBuilder();

            try {

               for (int i=0;i<response.length();i++){
                   JSONObject citiesjsonObject=response.getJSONObject(i);
                   stringBuilder.append("Name : "+citiesjsonObject.getString("name")+
                           "\n"+"Distance : "+citiesjsonObject.getString("distance")+ "\n"
                           +"Population : "+citiesjsonObject.getString("population"));


                   stringBuilder.append("\n\n");
               }

                
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(jsonArrayRequest);