如果在volley POST请求android中使用数组作为参数,则意外400响应错误

时间:2018-06-09 20:51:14

标签: android server request android-volley

我知道很多次都会问这个问题,但我的问题不同。

我正在尝试使用具有一些字符串和数组作为参数的volley将数据发布到服务器。 我有一个问题,当我只发送字符串参数到服务器服务器接受数据,我得到服务器的响应,但当我添加数组(使用GSON库转换为字符串)我得到400错误响应。 我还尝试使用" Postman"将此数据发送到服务器。从那里我得到了服务器的真实回应。

我想将数据发送到服务器,如下所示:

{

  "Ordertype": 1,

  "Tablenumber": 1,

  "InvoiceNumber": 1,

  "Terminalno": 1,

  "Clerkid": 1,

  "Total": "0",

  "Discountamount": "0",

  "Deliverycharges": "0",

  "Grandtotal": "0",

  "Products": [
    {
      "LineId": 1,
      "ProductSkuId": "2334",
      "ProductTitle": "Hasnain",
      "Qty": 1,
      "Rate": 23,
      "FinalPrice": 34,
      "GroupId": 1,
      "ProductType": "PRODUCT",
      "CategorySkuId": "sad"
    }
  ]

}

这是我的代码。

private class SendOrders extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(CategoriesActivity.this);
        progressDialog.setMessage("Please wait...");
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        Map<String, String> paramss = new HashMap<String, String>();

        Gson gson = new Gson();
        String products = gson.toJson(productsServerSendModelList);
        Log.e("products", products);

        paramss.put("Ordertype", "1");
        paramss.put("Tablenumber", "2");
        paramss.put("InvoiceNumber", "2");
        paramss.put("Terminalno", "2");
        paramss.put("Clerkid", "2");
        paramss.put("Total", "1");
        paramss.put("Discountamount", "2");
        paramss.put("Deliverycharges", "22");
        paramss.put("Grandtotal", "11");
        paramss.put("Products", products);

        JsonObjectRequest req = new JsonObjectRequest(params[0], new JSONObject(paramss),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            VolleyLog.v("Response:%n %s", response.toString(4));

                            boolean flag = response.getBoolean("Flag");
                            if (flag) {
                                String mesg = response.getString("Result");
                                Toast.makeText(CategoriesActivity.this, mesg , Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(CategoriesActivity.this, mesg , Toast.LENGTH_SHORT).show();

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.e("Error: ", error.toString());
                Toast.makeText(CategoriesActivity.this, error.toString(), Toast.LENGTH_LONG).show();
            }
        });
        AppController.getInstance().addToRequestQueue(req);
        dismissProgressDialog();
        return "success";
    }
}

0 个答案:

没有答案