I have been struggling with this problem for 3 days now and have received no help or answers that have worked from other posts on Stack.
I am trying to post variables from my mobile application to my server and everything is working fine on postman and I have double checked everything is mirroring from postman to my android http code.
I am using volley and am constantly receiving a status code 400 and have just recently got a message alongside the response code but this just says "Null"
What is null?? I see no errors in my code, it seems the headers are correct and same as postman and when debugged the JSON object is fine and getting the variables.
I am completely lost and would really appreciate some help:
My Code:
private void sendRequest() {
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://xxx.xx.xx.xxx:8080/engAppApi/webservices/engineerTable/";
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("machineType", machineType);
jsonObject.put("workOrderNumber", workOrderNumber);
jsonObject.put("employeeName", employee);
jsonObject.put("activity", activity);
jsonObject.put("durationHours", durationHours);
jsonObject.put("durationMins", durationMins);
jsonObject.put("downtimeHours", downTimeHours);
jsonObject.put("downtimeMins", downTimeMins);
jsonObject.put("details", details);
jsonObject.put("createdTimestamp", currentDateandTime);
} catch (JSONException e) {
// handle exception
}
JsonObjectRequest putRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// response
Log.d("Response", response.toString());
Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", error.toString());
Log.d("errornetwork", String.valueOf(error.networkResponse));
Toast.makeText(MainActivity.this, "ERROR OCCURED", Toast.LENGTH_SHORT).show();
}
}
) {
@Override
public Map<String, String> getHeaders()
{
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json;");
headers.put("securityToken", "Good Token");
return headers;
// charset=utf-8
}
@Override
public byte[] getBody() {
try {
Log.i("json", jsonObject.toString());
return jsonObject.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
putRequest.setRetryPolicy(new DefaultRetryPolicy( 50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
putRequest.setShouldCache(false);
queue.add(putRequest);
}