Reputation: 11
I'm using the Android volley library and I'm trying to sending data to the API server but it responds to this error every time
E/Volley: [2342] NetworkUtility.shouldRetryException: Unexpected response code 400 for API
This is my code:
JSONObject params = new JSONObject();
try {
params.put("deptcode", 649);
params.put("endDt", "2021-07-22T12:37:28.755Z");
params.put("instCode", 152);
params.put("instSesNO", 0);
params.put("locCode", 2);
params.put("observedBy", obserName);
params.put("sessionId", 0);
params.put("startDt", "2021-07-22T12:37:28.755Z");
params.put("status", "string");
}
catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "Error: " + error.getMessage());
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(300000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MyRequestQueue.add(jsonObjReq);
I tried many ways but still doesn't work for me. Please help me.
Upvotes: 1
Views: 4815
Reputation: 1151
normally 400 is a server error that is something not found on server.
1- try your api on postman and verify it is working.
2- sometimes, the value we're passing in int can be resolved by converting them to string.
3- try to get the error message from error.getMessage()
.
if not resolved, please share your api i'll check.
Upvotes: 2