0

Iam using Volley library to post json array to RestApi. But i get an error BasicNetwork.performRequest: Unexpected response code 400. I lookfor many articles Volley - Sending a POST request using JSONArrayRequest but not get any solution yet. Here's my code,

final JSONArray jsonArray = new JSONArray();
    List<User> users = UserManager.composeUsers();
    jsonArray.put(users);

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, "http://192.168.137.1:8080/create", jsonArray, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.d("Response: ", response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error: ", error.toString());
        }
    }) {

        @Override
        public String getBodyContentType() {
            return String.format("application/json; charset=utf-8");
        }

    };
    queue.add(jsonArrayRequest);
}

In the above code List<User> user return a java object. I want to pass the user object to rest service.

Community
  • 1
  • 1
Narandhran Thangavel
  • 1,392
  • 1
  • 12
  • 20
  • If you go to the server with a browser, entering the url you are using, what do you get back? It should be a JSON String. If not, the server's the issue. – JoeHz Mar 02 '17 at 10:26
  • post hour logcat or error where r u facing problem – Saurabh Bhandari Mar 02 '17 at 10:35
  • when i browse the URL I get the following response `{ "timestamp": 1488450781093, "status": 400, "error": "Bad Request", "exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@ea984ded", "path": "/create" }` – Narandhran Thangavel Mar 02 '17 at 10:36
  • @Narendhran you are not passing the parameters that you required in your webapi. – Saurabh Bhandari Mar 02 '17 at 10:39
  • @SaurabhBhandari what parameters should i pass ?? you mean the `user` object as json – Narandhran Thangavel Mar 02 '17 at 10:43
  • **My log error** *D/Error:: com.android.volley.ServerError* *E/Volley: [323411] BasicNetwork.performRequest: Unexpected response code 400 for http://192.168.137.216:8080/create* – Narandhran Thangavel Mar 02 '17 at 10:44
  • put your right content type and also define same in your web api – Saurabh Bhandari Mar 02 '17 at 11:04
  • @SaurabhBhandari I am new to android brother, Iam not able to understand that you are all saying. If you have any good example or reff, send the link to me I 'll be very thankful to you. Thanks for your kind response. – Narandhran Thangavel Mar 02 '17 at 11:10
  • @Narendhran follow this link http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ as well website this is best for newbie – Saurabh Bhandari Mar 02 '17 at 11:15

0 Answers0