Home > Blockchain >  How fix " Not a primitive array"
How fix " Not a primitive array"

Time:04-07

I trying to parse this json.

[
    {
        "id": 2,
        "name": "612",
        "code": "auditorium 612"
    },
    {
        "id": 3,
        "name": "613",
        "code": "auditorium 613"
    }
]

JSONException returns an error " Not a primitive array". I tryed to parse JsonObjectRequest, but another error occurred. Finally I came to option JsonArrayRequest

W/System.err: org.json.JSONException: Not a primitive array: class org.json.JSONArray
        at org.json.JSONArray.<init>(JSONArray.java:116)
        at com.example.dyplom.MainActivity$4.onResponse(MainActivity.java:126)
        at com.example.dyplom.MainActivity$4.onResponse(MainActivity.java:122)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:790)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err:     at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Here is the parser method.

private void loadStrings()
    {
        String url_head = "http://10.0.2.2:8080/rStrings";
        JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET,url_head, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    JSONArray jsonArray_string_list = new JSONArray(response);
                    main_name_rString = new String[jsonArray_string_list.length()];
                    main_code_rString = new String[jsonArray_string_list.length()];
                    for (int i = 0; i < jsonArray_string_list.length();i  ){
                        JSONObject string_params_string_list = jsonArray_string_list.getJSONObject(i);

                        Long string_id = string_params_string_list.getLong("id");
                        String string_name = string_params_string_list.getString("name");
                        String string_code = string_params_string_list.getString("code");

                        main_id_rSting[i]=string_id;
                        main_name_rString[i] = string_name;
                        main_code_rString[i] = string_code;

                        Log.i("Id", String.valueOf(main_id_rSting[i]));
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void one rrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });
        mQueue.add(request);
    }

I looked at similar questions, but I didn't really find anything. How can I fix it? Maybe I don't see smth.

CodePudding user response:

If the onResponse method already received the (JSONArray response), you don't need to create again a new JSONArray in the next line in your code. It would help if I can see the line where the error happen.

  • Related