首页 > 解决方案 > 尝试发布到自定义 API,收到 volley.ClientError

问题描述

我正在尝试使用 volley 将一些数据发布为自定义 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", "vasko@hotmail.com");
                    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);

}

我已经尝试寻找解决方案,但我找不到任何有用的东西。是否会发生错误,因为我试图将邮政编码、状态和帐户作为字符串传递,即使它们是模型中的整数?如果是这样,我怎样才能将它们作为整数传递?目前我正在覆盖必须返回 Map 实例的 getParams() 函数。如果这个问题有一个简单的解决方案,我想提前道歉,这是我第一次使用 RESTful API。

标签: javaandroidrestandroid-volley

解决方案


Volley 不适用于 https 请求,也许这个链接可以帮助你:Volley with Https

或者android:usesCleartextTraffic="true" ,如果您想授权 Http 纯文本,请将此行添加到您的清单中。


推荐阅读