简体   繁体   English

错误JSONObject无法在Android中转换为JSONArray?

[英]Error JSONObject cannot be converted to JSONArray in android?

Here I am trying to send my json array to mysql database.When I run this code I get this error: 在这里,我尝试将json数组发送到mysql数据库。当我运行此代码时,我收到此错误:

com.android.volley.ParseError: org.json.JSONException: Value {"msg":false,"status":false} of type org.json.JSONObject cannot be converted to JSONArray

And here's my code: 这是我的代码:

private void insertToDb() {
        billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
                .getText().toString();
        try {
            jsonObject.put("custInfo", custSelected.toString());
            jsonObject.put("invoiceNo", textViewInvNo.getText().toString());
            jsonObject.put("barcode", barCode.getText().toString());
            jsonObject.put("desc", itemDesc.getText().toString());
            jsonObject.put("weight", weightLine.getText().toString());
            jsonObject.put("rate", rateAmount.getText().toString());
            jsonObject.put("makingAmt", makingAmount.getText().toString());
            jsonObject.put("net_rate", netRate.getText().toString());
            jsonObject.put("itemTotal", itemtotal.getText().toString());
            jsonObject.put("vat", textViewVat.getText().toString());
            jsonObject.put("sum_total", textViewSum.getText().toString());
            jsonObject.put("bill_type", billType);
            jsonObject.put("date", textViewCurrentDate.getText().toString());


        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            itemSelectedJson.put(index, jsonObject);
            index++;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.POST, INVEST_URL, itemSelectedJson, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d("RESPONSE", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("JSON ERROR", error.toString());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                headers.put("Accept", "application/json");
                return headers;
            }
        };

        final RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(arrayRequest);

    }

I also tried displaying the json array inside a text view.It displays the following json array. 我也尝试在文本视图中显示json数组,它显示以下json数组。

[
    {
        "custInfo": "Ujwal 9975022560",
        "rate": "24000",
        "weight": "21.00000",
        "desc": "GENTS ANGTHI 22k NO STONE",
        "makingAmt": "200",
        "sum_total": "RS.104188.92",
        "vat": "RS.2042.92",
        "itemTotal": "51073",
        "barcode": "BQSP78BB",
        "net_rate": "24200",
        "date": "2015-12-01",
        "invoiceNo": "1",
        "bill_type": "Estimate"
    },
    {
        "custInfo": "Ujwal 9975022560",
        "rate": "24000",
        "weight": "21.00000",
        "desc": "GENTS ANGTHI 22k NO STONE",
        "makingAmt": "200",
        "sum_total": "RS.104188.92",
        "vat": "RS.2042.92",
        "itemTotal": "51073",
        "barcode": "BQSP78BB",
        "net_rate": "24200",
        "date": "2015-12-01",
        "invoiceNo": "1",
        "bill_type": "Estimate"
    }
]

I tested this json array on json lint.It is a valid JSON. 我在json lint上测试了这个json数组,它是有效的JSON。 As suggested I changed to json object request.Now I am getting a Response it say RESPONSE: {"msg":false,"status":false} I get it at public void onResponse(JSONObject response) { Log.d("RESPONSE", response.toString()); } 按照建议,我更改为json对象request.Now,现在我收到一个响应,它说RESPONSE: {"msg":false,"status":false}我在public void onResponse(JSONObject response) { Log.d("RESPONSE", response.toString()); } public void onResponse(JSONObject response) { Log.d("RESPONSE", response.toString()); } what does that mean? public void onResponse(JSONObject response) { Log.d("RESPONSE", response.toString()); }这是什么意思?

private void insertToDb() {
        billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
                .getText().toString();
        try {
            jsonObject.put("custInfo", custSelected.toString());
            jsonObject.put("invoiceNo", textViewInvNo.getText().toString());
            jsonObject.put("barcode", barCode.getText().toString());
            jsonObject.put("desc", itemDesc.getText().toString());
            jsonObject.put("weight", weightLine.getText().toString());
            jsonObject.put("rate", rateAmount.getText().toString());
            jsonObject.put("makingAmt", makingAmount.getText().toString());
            jsonObject.put("net_rate", netRate.getText().toString());
            jsonObject.put("itemTotal", itemtotal.getText().toString());
            jsonObject.put("vat", textViewVat.getText().toString());
            jsonObject.put("sum_total", textViewSum.getText().toString());
            jsonObject.put("bill_type", billType);
            jsonObject.put("date", textViewCurrentDate.getText().toString());


        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            itemSelectedJson.put(index, jsonObject);
            index++;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONObject jsonobj = new JSONObject();
        try {
            jsonobj.put("itemarray",itemSelectedJson);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        final JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, INVEST_URL, jsonobj, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("RESPONSE", response.toString());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("JSONERROR",error.toString());
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                headers.put("Accept", "application/json");
                return headers;
            }
        };
 final RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(objectRequest);

    }

As in log: 如在日志中:

com.android.volley.ParseError: org.json.JSONException: Value {"msg":false,"status":false} com.android.volley.ParseError:org.json.JSONException:值{“ msg”:false,“ status”:false}

Means Server is returning JSONObject instead of JSONArray as response String. 意味着服务器返回JSONObject而不是JSONArray作为响应字符串。

So, use JsonObjectRequest instead of JsonArrayRequest to make Volley request. 因此,使用JsonObjectRequest而不是JsonArrayRequest发出Volley请求。

POST a JSONArray doesn't mean you need to call a JsonArrayRequest request. POST JSONArray并不意味着您需要调用JsonArrayRequest请求。

Request depends on the type of the response you are expecting. 请求取决于您期望的响应类型。 You have to check the object type returned by your webservice and then adapt your request according to that. 您必须检查Web服务返回的对象类型,然后根据该请求调整您的请求。

In your specific case, the error log says you need to use a JsonObjectRequest instead of JsonArrayRequest. 在您的特定情况下,错误日志显示您需要使用JsonObjectRequest而不是JsonArrayRequest。

And I final got it to work this is my current code 我最终得到了它的工作,这是我当前的代码

 private void insertToDb() throws  JSONException{
        //createJsonArray();
        final String jsonArray = itemSelectedJson.toString().trim();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, INVEST_URL,


           new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    display.setText("This is the "+response);
                    Toast.makeText(AddInvEst.this, "This is the response"+response, Toast.LENGTH_LONG).show();
                    Log.d("RESPONSE", response.toString().trim());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String,String> params = new HashMap<>();
            params.put(KEY_JSONARRAY,jsonArray);
            return params;
        }
    };

    RequestQueue requestQ =Volley.newRequestQueue(this);
    requestQ.add(stringRequest);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM