试图发布到自定义API,接收到volley.ClientError

问题描述 投票:0回答:1

我正在尝试使用齐射将一些数据发布到自定义API。这也是我也在尝试更新信息的API模型。可以在以下POST链接下的http://ecoproduce.eu/swagger/index.html上找到它:http://ecoproduce.eu/api/User/register/

{
  "firstName": "string",
  "lastName": "string",
  "email": "string",
  "password": "string",
  "zipCode": 0,
  "state": 0,
  "account": 0
}

[当我尝试向API添加新的测试用户时,出现以下错误:

E/Volley: [9583] BasicNetwork.performRequest: Unexpected response code 400 for http://ecoproduce.eu/api/User/register/
E/Error response: com.android.volley.ClientError

这是我用来发布用户的功能。

private void addUser() {

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    JsonArrayRequest arrayRequest = new JsonArrayRequest(
            Request.Method.POST,
            "http://ecoproduce.eu/api/User/register/",
            null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Error response", error.toString());
                }
            })

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

                    Map<String,String> params = new HashMap<String,String>();
                    params.put("firstName", "Vasko");
                    params.put("lastName", "Vasilev");
                    params.put("email", "[email protected]");
                    params.put("password", "18yoBonev");
                    params.put("zipCode", "0");
                    params.put("state", "1");
                    params.put("account", "2");

                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Content-Type", "application/json; charset=utf-8");
                    return headers;
                }

            };

    requestQueue.add(arrayRequest);

}

我曾尝试查找解决方案,但找不到任何有用的方法。因为我试图将zipCode,state和account作为字符串传递,即使它们是模型中的整数,也会发生错误吗?如果是这样,如何将它们作为整数传递?目前,我正在重写getParams()函数,该函数必须返回Map实例。如果这个问题有一个简单的解决方案,我想提前道歉,这是我第一次使用RESTful API。

java android rest android-volley
1个回答
0
投票

通过使用字符串请求解决。找到答案:How to send a POST request using volley with string body?

当前代码如下:

 private void addUser() {

    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();

    try {
        jsonObject.put("firstName", "Vasko");
        jsonObject.put("lastName", "Vasilev");
        jsonObject.put("email", "[email protected]");
        jsonObject.put("password", "18yoBonev");
        jsonObject.put("zipCode", 0);
        jsonObject.put("state", 1);
        jsonObject.put("account", 2);

        jsonArray.put(jsonObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    final String requestBody = jsonObject.toString();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://ecoproduce.eu/api/User/register", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("VOLLEY", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("VOLLEY", error.toString());
        }
    }) {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public byte[] getBody() {
            try {
                return requestBody == null ? null : requestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                return null;
            }
        }

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            String responseString = "";
            if (response != null) {
                responseString = String.valueOf(response.statusCode);
                // can get more details such as response.headers
            }
            return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
        }
    };

    requestQueue.add(stringRequest);
}
© www.soinside.com 2019 - 2024. All rights reserved.