I am trying to use Volley to send a JSON object to a Loopback application API which is using MongoDB. I can see that the data is being saved in the database successfully. But the Volley response always shows that the status code is 422. Can someone point out what I am doing wrong?
This is the error message:
BasicNetwork.performRequest: Unexpected response code 422 for http://localhost:3001/api/Persons
This is the JSON I am trying to send:
{
"map_coordinates": {
"lat": xxxxxx,
"lng": xxxxxxx
},
"first_name": "xxxxx",
"middle_name": "",
"family_name": "xxxxxx",
"gender": "xxxxxx",
"role": "xxxxxx",
"dob": "2017-09-28T05:51:40.836Z",
"picture_of_person": "",
"username": "ajxxxx7@gmail.com",
"deviceToken": "xxxxxxxx",
"portal_address": "xxxxx",
"email": "ajxxxxx7@gmail.com",
"password": "xxxxxxx",
"vcode": 562991,
"mobile": "+91xxxxxxxx"
}
I do not think that there is a problem with the JSON because, if I POST the same object from the Loopback explorer then the request goes through successfully and returns status code 200.
This is the code I am using to send the JSON request.
public void postJsonRequest(String url, JSONObject data, final Callback<JSONObject, VolleyError> callback) {
JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST,
url, data, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onFailure(error);
}
});
makeJsonRequest(objectRequest);
}
private void makeJsonRequest(JsonObjectRequest request) {
RequestQueue queue = Volley.newRequestQueue(EngageMyTime.getInstance().getEMTContext());
queue.add(request);
}
Any help/idea would be appreciated.
Try to set the default charset UTF-8 on server side and encode in UTF-8 the json data before send
In android with
URLEncoder.encode(string,"UTF-8");