Search code examples
androidpostandroid-volleyhttp-response-codes

Volley Post Request not working to the API


I used Volley to POST name, password and email of a user for registration purpose. I took reference from This tutorial. And worked same as defined in this tutorial.

Here is my code:

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);

But my code shows this error:

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

Error 422 is defined on the API as:

    {
       "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
}

I tried:

  1. changing JsonObjectRequest to StringRequest,

  2. the original URL at the place of URL_string.api_url,

But the response is still same. Is this because of I didn't make any class for Volley library? I did that too from Androidhive.info, but failed!

I don't know how to go ahead from this step now, while Log in getParams logs the correct value of name, email, and pw entered by the user, but still POST operation is not working. Please help! Thanks in advance!


Solution

  • You need to return the params object you create, not returning the parent's method (which is empty).

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

    As pointed out by pablobu, this also applies to your 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();
    }