Reputation: 6892
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": "[email protected]",
"deviceToken": "xxxxxxxx",
"portal_address": "xxxxx",
"email": "[email protected]",
"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.
Upvotes: 1
Views: 1216
Reputation: 201
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");
Upvotes: 1
Reputation: 144
What format are you used in response? İf you can't use JSON,You may see this error. Because postJSONResponse use JSONObject in parsing. Something like this. You can write custom request or change your response type to JSON. Example of custom request is:
public class SignUpRequest extends Request<String> {
private Map<String, String> mParams;
public SignUpRequest(String url, Response.ErrorListener listener) {
super(url, listener);
}
public SignUpRequest(int method, String url,String email,String userName,String password, Response.ErrorListener listener) {
super(method, url, listener);
mParams=new HashMap<>();
mParams.put("username",userName);
mParams.put("email",email);
mParams.put("password",password);
}
@Override
protected void deliverResponse(String response) {
Log.e("Response",response);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return mParams;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String data=new String(response.data);
return Response.success(data+" "+response.statusCode,
HttpHeaderParser.parseCacheHeaders(response));
}
}
Upvotes: 0