I am using volley to post the data to server.I am accepting data from user using edittext fileds.I am accepting only first_name, last_name, username, email, phone, address.The format of the data is like
{
"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"
}
]
}
The Post request is like
private void sendData() {
String req="request";
String url="http://192.168.1.106:9500/api/userprofile/";
JSONObject jsonObject=new JSONObject();
try
{
jsonObject.put("count","");
jsonObject.put("next","");
jsonObject.put("previous","");
JSONArray jsonArray=new JSONArray();
JSONObject jsonObjectUser=new JSONObject();
jsonObjectUser.put("first_name",first_name);
jsonObjectUser.put("last_name",last_name);
jsonObjectUser.put("username",username);
jsonObjectUser.put("email",email);
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("user",jsonObjectUser);
jsonObject1.put("address","Pune");
jsonObject1.put("phone",phone);
jsonArray.put(jsonObject1);
jsonObject.put("results",jsonArray);
}
catch (JSONException e)
{
e.printStackTrace();
}
JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST,url,null,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getActivity(),"Done Success",Toast.LENGTH_SHORT).show();
}
}, 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);
}
}
When I send data I am getting Error code 400.How to resolve this ?