在POST请求RESTful CART API期间出现意外的响应代码500

时间:2018-04-23 04:49:21

标签: java post cart

我目前正在使用RESTful API进行Woocommerce将商品添加到购物车中。它应该接受" product_id"和"数量"作为基于我使用以下代码的API文档的参数:

JSONObject addcart = new JSONObject();

    try {
        addcart.put("product_id", 28);
        addcart.put("quantity", 1);
        Log.e("params", addcart.toString());

    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, domain+api,addcart,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(OrderActivity.this, response.toString(), Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(OrderActivity.this, error.toString(), Toast.LENGTH_LONG).show();
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json");
            return params;
        }
    };


    Controller.getPermission().addToRequestQueue(request);

}

但是,我在执行POST请求时收到此错误消息:

E/params: {"product_id":28,"quantity":1} 
E/Volley: [365] BasicNetwork.performRequest: Unexpected response code 500 for http://domain+api

产品未添加到购物车中,并且通过GET请求响应为&#34;购物车为空!&#34;。我究竟做错了什么?我很确定API有效,但我无法弄清楚为什么我的POST请求没有通过并且不断收到错误500.有人可以给我一些建议吗?

(如果有人想知道文档:https://seb86.github.io/WooCommerce-Cart-REST-API-Docs/?shell#add-to-cart

1 个答案:

答案 0 :(得分:0)

使用下面的代码后,我能够解决问题:

 StringRequest addrequest = new StringRequest(Request.Method.POST, "http://domainname/test/wp-json/wc/v2/cart/add?product_id=28&quantity=14" ,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    Toast.makeText(OrderActivity.this, response, Toast.LENGTH_LONG).show();
                    Log.e("Response", response);

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(OrderActivity.this, error.toString(), Toast.LENGTH_LONG).show();
            Log.e("Error", error.toString());
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            boolean dbempty = helper.checklogin();
            if (!dbempty) {
                params.put("Authorization", "Bearer " + helper.getToken());
            }
            params.put("Content-Type", "application/json");
            return params;
        }
    };
    Controller.getPermission().addToRequestQueue(addrequest);

我收到了以下字符串请求响应,该项目确实已添加到我的localhost网站上:

E/Response: <br />
<b>Warning</b>:  is_numeric() expects exactly 1 parameter, 3 given in <b>C:\xampp\htdocs\test\wp-includes\rest-api\class-wp-rest-request.php</b> on line <b>858</b><br />
<br />
<b>Warning</b>:  is_numeric() expects exactly 1 parameter, 3 given in <b>C:\xampp\htdocs\test\wp-includes\rest-api\class-wp-rest-request.php</b> on line <b>858</b><br />
{"key":"33e75ff09dd601bbe69f351039152189","product_id":28,"variation_id":0,"variation":[],"quantity":14,"line_tax_data":{"subtotal":[],"total":[]},"line_subtotal":979.86,"line_subtotal_tax":0,"line_total":979.86,"line_tax":0,"data":{}}

我曾尝试过其他POST方法字符串请求,其中我收到完全相同的警告消息并且POST请求未通过但是当我这样做时它仍然有效,尽管我仍然收到错误消息。但希望这可以帮助其他人使用相同的API同样的问题。

注意:我添加的包含授权密钥和承载令牌值的新标题是为每个用户提供他们自己的唯一CART,以便他们不共享同一个购物车。这不是解决问题所必需的。