Home > Enterprise >  How to call JsonArrayRequest(response is an object array) for POST Method with a jsonobject body
How to call JsonArrayRequest(response is an object array) for POST Method with a jsonobject body

Time:10-21

My service response is a JsonArray but Body was I sent should be a jsonObject. But When I tried, It does not accept postData(it makes underlined as red in request line) So how can I solve that?

My service data comes but IT goes to one rrorResponse function when I use JsonobjectRequest and gives that error : com.android.volley.ParseError: org.json.JSONException: Value[{"total":120, ....}]

and in console detail message is : Value[{"total":120, ....}]of type org.json.JSONArray cannot be converted to JSONObject

Code is Like that:

GetDataFromService()
{
        JSONArray xClasses = new JSONArray();
        JSONArray xTypes = new JSONArray();
        JSONArray xGroups = new JSONArray();
        JSONArray yTypes = new JSONArray();

        JSONObject filteredItems = new JSONObject();
        try {
            filteredItems.put("level", 0);
            filteredItems.put("subLevel", 0);
            filteredItems.put("id", 0);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JSONObject postData = new JSONObject();
        try
        {
            postData.put("FilteredItems", filteredItems);
            postData.put("XClasses", xClasses);
            postData.put("XTypes", xTypes);
            postData.put("XGroups", xGroups);
            postData.put("YTypes", yTypes);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

        JsonArrayRequest postRequest = new JsonArrayRequest(Request.Method.POST, url, postData,
                new Response.Listener<JSONArray>()
                {
                    @Override
                    public void onResponse(JSONArray response)
                    {
                        try {
                            JSONArray json = new JSONArray(response);
                            System.out.println(json);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        Gson gson = new Gson();
                        System.out.println(response);                }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void one rrorResponse(VolleyError error)
                    {
                        // TODO Auto-generated method stub
                        Log.d("ERROR","error => " error.toString());
                    }
                }
        )
        { // Bearer Token - for Authorization
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError
            {
                Map<String, String>  params = new HashMap<>();
                params.put("Authorization","Bearer "   token);
                return params;
            }
        };
        myQueue.add(postRequest);
    }

Code gives an error for postData's Type. Because I send request as jsonarray but post data is jsonbject.

CodePudding user response:

Seems like it was removed in recent volley version but you can easily modify this constructor and add to JsonArrayRequest.

public JsonArrayRequest(int method, String url, JSONObject jsonRequest, Listener listener, ErrorListener errorListener) { super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); }

CodePudding user response:

The JsonArrayRequest constructror accept a JSONArray as argument.

Try to convert your JSONObject to a JSONArray

  • Related