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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20997924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:47:59  来源:igfitidea点击:

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

androidHymansonandroid-volley

提问by Mohammed Subhi Sheikh Quroush

I got this error from volley library

我从 volley 图书馆得到这个错误

@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 Hymanson ?

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

回答by Pasquale Anatriello

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.

如果您想以字符串形式接收结果,请不要使用 JSONRequest。使用简单的 Request 类。您的问题很简单,服务器返回一个 JSONArray,里面只有一个元素。JSONArray 不是 JSONObject。这就是解析失败的原因。

回答by Md. Shahinur Rahman

We Have to use JsonArrayRequestinstead of JsonObjectRequest. The code as:

我们必须使用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);

Hope, it's solve the problem.

希望,它解决了问题。

回答by Mohammed Subhi Sheikh Quroush

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

我注意到 volley 支持类 JsonArrayRequest 所以我使用这个类并且问题解决了,我使用的是 JsonObjectRequest

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

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

回答by Budhdi Sharma

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);
    }