0

I am trying to post some data a custom API using volley. This is the model of the API which I am trying to update information too. It can be found on http://ecoproduce.eu/swagger/index.html under the following POST link: http://ecoproduce.eu/api/User/register/

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

When I try to add a new test user to the API I receive the following error:

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

This is the function I use to POST the user.

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

}

I have tried looking up solutions to this but I have not been able to find anything helpful. Could the error be occurring because I am trying to pass the zipCode, state and account as Strings even tho they are integers in the model? If so, how can I pass them as integers? Currently I am overriding the getParams() function which must return a Map instance. I would like to apologise in advance if this questions has a simple solution, it is my first time working with RESTful API.

Vasko Vasilev
  • 554
  • 1
  • 10
  • 25

2 Answers2

0

Solved by using a string request. Answer found: How to send a POST request using volley with string body?

Code is currently like this:

 private void addUser() {

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

    try {
        jsonObject.put("firstName", "Vasko");
        jsonObject.put("lastName", "Vasilev");
        jsonObject.put("email", "vaskovaskovasko@hotmail.com");
        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);
}
Vasko Vasilev
  • 554
  • 1
  • 10
  • 25
0

Volley doesn't work with https requests, maybe this link helps you : Volley with Https

Or Add this line android:usesCleartextTraffic="true" to your manifest if you would like to authorize Http plain text.

Abderazak Amiar
  • 776
  • 7
  • 22