0

My JSON RESULT

This is my JSON output. Here array name not available

[{"item":"WATER"},{"item":"DFG"},{"item":"2InchPipe"},{"item":"5InchPipe"}]

Android Code

But I want to display the item in the android spinner. I was used volley method for getting all item from JSON.

 RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
           String getitem=
            try{
                JSONObject jsonObject=new JSONObject(response);

                    JSONArray jsonArray=jsonObject.getJSONArray("*What to do here*");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject1=jsonArray.getJSONObject(i);
                        String country=jsonObject1.getString("Item");
                        CountryName.add(country);
                    }

                spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, CountryName));
            }catch (JSONException e){e.printStackTrace();}
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);
    requestQueue.add(stringRequest);

I have doubt in this line

JSONArray jsonArray=jsonObject.getJSONArray("*What to do here*");

Finally i got a solution using following code

  JsonArrayRequest requestQueue = new JsonArrayRequest(URL,
  new Response.Listener<JSONArray>() {
  @Override
  public void onResponse(JSONArray response) {         
  for (int i = 0; i < response.length(); i++) {
  try {
  JSONObject obj = response.getJSONObject(i);
  String country = obj.getString("item");
  CountryName.add(country);
  spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, 
  android.R.layout.simple_spinner_dropdown_item, CountryName));
  }
  catch (JSONException e) {
  e.printStackTrace();
  }
  }
  }
  }, new Response.ErrorListener() {
  @Override
  public void onErrorResponse(VolleyError error) {
  }
  });     
  }
   MySingleton.getmInstance(MainActivity.this).addToRequestQue(requestQueue );
  • paste your whole JSON response as well. – Neeraj Sewani May 20 '18 at 11:13
  • Possible duplicate of [Get JSONArray without array name?](https://stackoverflow.com/questions/10164741/get-jsonarray-without-array-name) – ADM May 20 '18 at 11:47
  • in the try block, you try to convert it to JSONObject ( JSONObject jsonObject=new JSONObject(response);) . Change it to JSONArray jsonArray=new JSONArray(response); – Ishan Fernando May 21 '18 at 07:25

1 Answers1

0

You can direct pass you output in Array:

JSONArray jsonArray=new JSONArray (response);
 for(int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject1=jsonArray.getJSONObject(i);
                    String country=jsonObject1.getString("Item");
                    CountryName.add(country);
                }
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
  • Am trying this..but it's showing error.JSON array can't to convert object – Shabek Mahmood May 20 '18 at 23:59
  • If using response ,Showig this error bro `org.json.JSONException: Value [{"item":"WATER"},{"item":"DFG"},{"item":"2InchPipe"},{"item":"5InchPipe"}] of type org.json.JSONArray cannot be converted to JSONObject` – Shabek Mahmood May 21 '18 at 06:58