Volley Post 请求无法正常工作 API

Volley Post Request not working to the API

我使用 Volley POST 用户名、密码和电子邮件进行注册。我参考了 This tutorial。并按照本教程中的定义工作。

这是我的代码:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                            URL_string.api_url,null
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    Log.i("Result", "Success");
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.i("Result", "UnSuccess");
                        }
                    }){

                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params = new HashMap<String, String>();
                            Log.i("Result_Params",emailValue + passwordValue + nameValue);
                            params.put("email", emailValue);
                            params.put("password", passwordValue);
                            params.put("name", nameValue);
                            return super.getParams();
                        }

                        @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 super.getHeaders();
                        }

                    };

                    RequestQueue requestQueue = Volley.newRequestQueue(this);
                    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    requestQueue.add(jsonObjectRequest);

但是我的代码显示了这个错误:

[368] BasicNetwork.performRequest: Unexpected response code 422 for http://myapi.com/api/register

错误 422 在 API 上定义为:

    {
       "message": "422 Unprocessable Entity",
       "errors": {
         "email": [
           "The email field is required."
         ],
         "password": [
           "The password field is required."
         ],
         "name": [
           "The password field is required."
         ]
       },
       "status_code": 422
}

我试过了:

  1. 将 JsonObjectRequest 更改为 StringRequest,

  2. 原来URL在URL_string.api_url,

但是反应还是一样。这是因为我没有为 Volley 库制作 class 吗?我也是从 Androidhive.info 开始的,但是失败了!

我现在不知道如何从这一步继续下去,虽然登录 getParams 记录了用户输入的姓名、电子邮件和密码的正确值,但仍然 POST 操作不起作用.请帮忙! 提前致谢!

您需要 return 您创建的参数对象,而不是 return 父方法(它是空的)。

@Override
protected Map<String, String> getParams() throws AuthFailureError {
    Map<String, String> params = new HashMap<String, String>();
    Log.i("Result_Params",emailValue + passwordValue + nameValue);
    params.put("email", emailValue);
    params.put("password", passwordValue);
    params.put("name", nameValue);
    return params; //not super.getParams();
}

正如 pablobu 所指出的,这也适用于您的 getHeaders():

@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; //not super.getHeaders();
}