Android Volley POST string in body
To send a normal POST request (no JSON) with parameters like username and password, you'd usually override getParams() and pass a Map of parameters:
public void HttpPOSTRequestWithParameters() { RequestQueue queue = Volley.newRequestQueue(this); String url = "http://www.somewebsite.com/login.asp"; StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("Response", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("ERROR","error => "+error.toString()); } } ) { // this is the relevant method @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("grant_type", "password"); // volley will escape this for you params.put("randomFieldFilledWithAwkwardCharacters", "{{%stuffToBe Escaped/"); params.put("username", "Alice"); params.put("password", "password123"); return params; } }; queue.add(postRequest);}
And to send an arbitary string as POST body data in a Volley StringRequest, you override getBody()
public void HttpPOSTRequestWithArbitaryStringBody() { RequestQueue queue = Volley.newRequestQueue(this); String url = "http://www.somewebsite.com/login.asp"; StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d("Response", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("ERROR","error => "+error.toString()); } } ) { // this is the relevant method @Override public byte[] getBody() throws AuthFailureError { String httpPostBody="grant_type=password&username=Alice&password=password123"; // usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it try { httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8"); } catch (UnsupportedEncodingException exception) { Log.e("ERROR", "exception", exception); // return null and don't pass any POST string if you encounter encoding error return null; } return httpPostBody.getBytes(); } }; queue.add(postRequest);}
As an aside, Volley documentation is non-existent and quality of StackOverflow answers is pretty bad. Can't believe an answer with an example like this wasn't here already.
First thing, I advise you to see exactly what you're sending by either printing to the log or using a network sniffer like wireshark or fiddler.
How about trying to put the params in the body? If you still want a StringRequest
you'll need to extend it and override the getBody()
method (similarly to JsonObjectRequest
)
I know this is old, but I ran into this same problem and there is a much cleaner solution imo found here: How to send a POST request using volley with string body?