当前位置: 首页 > 知识库问答 >
问题:

获取“org.json.jsonArray”不能转换为JsonObject“

包承望
2023-03-14

这是错误:

Error: org.json.JSONException: Value [{"status":"Success","code":1313,"msg":"Request completed successfully"}] of type org.json.JSONArray cannot be converted to JSONObject.

这就是我的代码:

            RequestQueue requestQueue = Volley.newRequestQueue(this);
            JSONObject jsonObject = new JSONObject();

            try {
                jsonObject.put("auth", "---------");
                jsonObject.put("request", "Login");
                jsonObject.put("Name", "raky");
                jsonObject.put("Email", "exp@a.com");
            } catch (JSONException e) {
                e.printStackTrace();
            }


            String url = "http://my.website_name.com/";
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            Log.d("VOLLEY", response.toString());
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("ERROR.VOLLEY", error.getMessage());

                        }

                    });

            jsonObjectRequest.setTag(1);
            requestQueue.add(jsonObjectRequest);

共有1个答案

华升
2023-03-14

这里的要点是,您的网站响应主体对象是一个JSONArray

[
    {
        "status": "Success",
        "code": 1313,
        "msg": "Request completed successfully"
    }
]

所以出现异常是因为在响应处理程序中需要JSONObject,而不能将JSONArray强制转换为JSONObject

服务器(网站)必须返回给您的是根JSONObject,然后在其节点树中可以有JSONArray,但是根必须是JSONObject

    {
        "status": "Success",
        "code": 1313,
        "msg": "Request completed successfully"
    }
 类似资料: