I am using volley library to post json object to server.I am accepting data from user using edittext fields.The request is of the form
{ "count": 1, "next": null, "previous": null, "results": [ { "user": { "first_name": "Satyam", "last_name": "Gondhale", "username": "satyam@gmail.com", "email": "satyam@gmail.com", "groups": [], "is_active": true }, "phone": "9028571487", "address": "Pune" } ] }
I am accepting first_name,last_name,email,phone,address from user.My request is of the form
private void sendData() { String req = "request"; String url = "http://192.168.1.106:9500/api/userprofile/"; JSONObject jsonObject = new JSONObject(); try { JSONObject jsonObjectUser=new JSONObject(); jsonObjectUser.put("first_name",first_name); jsonObjectUser.put("last_name",last_name); jsonObjectUser.put("username",user_email); jsonObjectUser.put("email",user_email); JSONObject jsonObject1=new JSONObject(); jsonObject1.put("address",""); jsonObject1.put("phone",user_phone); JSONArray jsonArray=new JSONArray(); jsonArray.put(jsonObject1); jsonArray.put(jsonObjectUser); jsonObject.put("results",jsonArray); Log.i("result",jsonArray.toString()); } catch (JSONException e) { e.printStackTrace(); } JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<>(); String credentials = name+":"+pass; String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); headers.put("Content-type", "application/json"); headers.put("Authorization", auth); return headers; } }; AppController.getInstance().addToRequestQueue(request,req); } }
But when I send the request data is not post to server and I get error code 400 Bad Request.How to resolve this ?