com.android.volley.ParseError:org.json.JSONException

Moh*_*ush 11 android jackson android-volley

我从凌空库中得到了这个错误

@Override
public void onErrorResponse(VolleyError error) {
    error.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

错误

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

如何以字符串形式接收结果然后我将使用jackson处理它?

Pas*_*llo 22

如果要以字符串形式接收结果,请不要使用JSONRequest.使用简单的Request类.你的问题非常简单,服务器只返回一个内部只有一个元素的JSONArray.JSONArray不是JSONObject.这就是解析失败的原因.


小智 6

我们必须使用JsonArrayRequest而不是JsonObjectRequest.代码如下:

    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);
Run Code Online (Sandbox Code Playgroud)

希望,它解决了这个问题.


Moh*_*ush 5

我注意到有一个类JsonArrayRequest由volley支持所以我使用这个类并且问题解决了,我正在使用JsonObjectRequest

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