com.android.volley.ParseError: org.json.JSONException
Asked Answered
F

4

14

I got this error from volley library

@Override
public void onErrorResponse(VolleyError error) {
    error.printStackTrace();
}

the error

com.android.volley.ParseError: org.json.JSONException: Value [{"id":"admin","name":"Admin"}] of type org.json.JSONArray cannot be converted to JSONObject

How can I receive the result as string and then I will process it using jackson ?

Fumed answered 8/1, 2014 at 14:6 Comment(0)
S
26

If you want to receive the result as a string don't use the JSONRequest. Go with the simple Request class. Your problem is pretty simple the server is giving back a JSONArray with just one element inside. A JSONArray is not a JSONObject. That's why the parsing is failing.

Shady answered 8/1, 2014 at 14:58 Comment(0)
S
11

We Have to use JsonArrayRequest instead of JsonObjectRequest. The code as:

    RequestQueue queue = Volley.newRequestQueue(this);

    final String url = "http://192.168.88.253/mybazar/get_product_list.php";

    // prepare the Request
    JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>()
            {
                @Override
                public void onResponse(JSONArray response) {
                    // display response
                    Log.d("Response", response.toString());
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            }
    );



    // add it to the RequestQueue
    queue.add(getRequest);

Hope, it's solve the problem.

Scoliosis answered 12/7, 2017 at 7:25 Comment(0)
F
5

I noticed that there is class JsonArrayRequest supported by volley so I use this class and the problem solved, I was using JsonObjectRequest

https://android.googlesource.com/platform/frameworks/volley/+/43950676303ff68b23a8b469d6a534ccd1e08cfc/src/com/android/volley/toolbox

Fumed answered 8/1, 2014 at 15:30 Comment(0)
M
0

Probably the below logic will work for you:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.GET,
                url,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONObject jsonObject1 = new JSONObject(response.toString());
                            JSONArray jsonArray = jsonObject1.getJSONArray("statewise");
                            Log.d("Json response", "onResponse: "+jsonObject1.toString());

                            for (int i = 0; i < jsonArray.length; i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                //Here you will get your result so can use textview 
                               //to populate the result
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "onErrorResponse: "+error);
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonObjectRequest);
    }
Marchese answered 26/4, 2020 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.