Home > Enterprise >  Not a primitive array: class org.json.JSONArray
Not a primitive array: class org.json.JSONArray

Time:12-21

{ "records": [ { "id": "rec43JpMrSsSmAoGG", "createdTime": "2022-12-17T15:30:47.000Z", "fields": { "Name": "green." } }, { "id": "recBfJzGGNdwRiCWR", "createdTime": "2022-12-17T15:31:16.000Z", "fields": { "Name": "yellow." } }, { "id": "recDynbN2dibdMLvO", "createdTime": "2022-12-17T15:31:16.000Z", "fields": { "Name": "purple." } }, { "id": "recNZMK3mZda33CXU", "createdTime": "2022-12-17T15:31:16.000Z", "fields": { "Name": "brown." } }, { "id": "reca6MBTrUKQXqcIl", "createdTime": "2022-12-17T15:31:16.000Z", "fields": { "Name": "pink." } }, { "id": "recdM8t7quScDwRLF", "createdTime": "2022-12-17T15:31:16.000Z", "fields": { "Name": "orange." } }, { "id": "recq5aeM3M7Gjsif9", "createdTime": "2022-12-17T15:30:47.000Z", "fields": { "Name": "blue." } }, { "id": "recxcNttZ5UoamShI", "createdTime": "2022-12-17T15:30:47.000Z", "fields": { "Name": "red." } } ] }

its my Json file but i hew to use Volley laibery but ex. Not a primitive array: class org.json.JSONArray help us java code is

RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://loclhost:1111/json";
         JsonObjectRequest jsonArryRequest =new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
             @Override
             public void onResponse(JSONObject response) {

                 try {

                    JSONArray jsonArray = new JSONArray(response.getJSONArray("records").getJSONObject(1));

                 } catch (JSONException e) {
                     e.printStackTrace();
                     Log.d("Papa","Ex - " e.getMessage());
                 }

             }
         }, new Response.ErrorListener() {
             @Override
             public void one rrorResponse(VolleyError error) {
                 String errorm = error.getMessage();
                 Log.d("papa", "error : " errorm);

             }
         });

         queue.add(jsonArryRequest);

CodePudding user response:

You are initializing a new jsonArray that's not the way to do it

Instead of this

JSONArray jsonArray = new JSONArray(response.getJSONArray("records").getJSONObject(1));

Do this

JSONArray jsonArray = response.getJSONArray("records");
JSONObject jsonObject = jsonArray.getJSONObject(1));

By doing it this way you will also get your second json object into jsonObject variable

  • Related