首页 文章

Volley Post请求不使用API

提问于
浏览
0

我使用Volley来注册用户的姓名,密码和电子邮件以进行注册 . 我从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
}

我试过了:

  • 将JsonObjectRequest更改为StringRequest,

  • 在URL_string.api_url处的原始网址,

但反应仍然相同 . 这是因为我没有为Volley库做任何课程吗?我也从Androidhive.info那样做了,但失败了!

我现在不知道如何继续这一步,而登录getParams会记录用户输入的名称,电子邮件和密码的正确值,但仍然无法使用POST操作 . 请帮忙!提前致谢!

1 回答

  • 2

    您需要返回您创建的params对象,而不是返回父方法(为空) .

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

相关问题