My code is trying to POST data to a server and I need to add a header, I am using the Volley library.
The request works if I do not include the "getparams" method, I am able to post but with no data.
If I include the "getparams" method, the request fails with a 400 (Bad request).
I have not been able to find out where the error is.
public void tryPost() {
RequestQueue queue = Volley.newRequestQueue(this);
String serverUrl = "http://10.0.2.2:3000/tasks";
StringRequest stringRequest = new StringRequest(Request.Method.POST, serverUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG", "response = "+ response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("TAG", "Error = "+ error);
}
})
{
//
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers;
}
////
@Override
public Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("userId","sargent");
params.put("password","1234567");
return params; //return the parameters
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
}