Reputation: 11
I know there are similar looking questions but I have searched many of them and they don't seem to be working for me. So what I want to do is send a post request with a simple string "ON"/ "OFF"
as the data. Something like this works (for some $URL):
curl -X POST --header "Content-Type: text/plain; charset=utf-8" -d "ON" "$URL"
Here is the code I am trying and after reading lots of code and answers here I thought it would work but it isn't.
What am I missing ? I get a 400 error
from server.
Request request = new StringRequest
(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Command was sent successfully");
}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Got command error for " + url + " command " + command + " error " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "text/plain; charset=utf-8");
// headers.put("Accept", "application/json");
// headers.put("User-agent", "My useragent");
return headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return command.getBytes("utf-8");
} catch (Exception e) {
Log.d(TAG, "failed converting" + e.getMessage());
return null;
}
}
};
Upvotes: 1
Views: 204
Reputation: 3263
You have to override getParams()
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("postData", "my simple on/off string");
return params;
};
Upvotes: 1