【问题标题】:W/System.err: org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject [duplicate]W/System.err:org.json.JSONException:org.json.JSONArray 类型的值 [] 无法转换为 JSONObject [重复]
【发布时间】:2017-12-18 11:20:37
【问题描述】:

我需要将数据保存在database MySql

这是代码:

private void uploadMultipart(final String imageData,final String titolo,final String sottotitolo,final String data) {
        String tag_string_req = "req_register";

        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.UPLOAD_URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Register Response: " + response.toString());
               // hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {


                        Toast.makeText(getActivity(), "Aggiunto ai preferiti!", Toast.LENGTH_LONG).show();

                    } else {

                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getActivity(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Registration Error: " + error.getMessage());
                Toast.makeText(getActivity(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("url", imageData);
                params.put("titolo", titolo);
                params.put("sottotitolo", sottotitolo);
                params.put("data", data);
                params.put("id", id);


                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

但是通过这段代码我得到了这个exception

W/System.err: org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject

【问题讨论】:

  • 你也可以发布你试图解析的响应吗?
  • JSONArray jsonArray = new JSONArray(response.body.string())

标签: java android mysql json


【解决方案1】:

这里你使用 jsonArray 到 jsonObject

JSONObject jObj = new JSONObject(response);

改为这样使用

JSONArray json = new JSONArray(response);

【讨论】:

  • 布尔错误 = json.getBoolean("error");错误以红色下划线显示
  • 分享你的响应 json
  • 注册响应:[]
  • 响应是JsonArray所以你必须将响应字符串解析为JsonArray。试试上面的代码
【解决方案2】:

您的 onResponse(String response) 方法的响应字符串很可能是 JSONArray,而不是 JSONObject(括在 [] 括号中,而不是 {}),因此当您尝试解析并将其转换为 JSONObject 时会出现错误.

JSONObject jObj = new JSONObject(response); 行更改为JSONArray jArr = new JSONArray(response); - 这应该可以解决问题。

【讨论】:

    【解决方案3】:

    在您编写 JSONObject 的代码中,您正在传递一个 JSON 数组,这意味着您的响应是数组类型。 写 JSONArray jsonArray = new JSONArray(response) 现在你可以访问这个数组了

    如需了解更多信息,请发表评论,我一定会为您提供帮助。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多