How to send Send form-urlencoded parameters in post request android volley?

By | June 30, 2017

We have to override the getBodyContentType() method in volley to enable “form-urlencoded parameters” in POST request using Volley.
Here is how the code will look like.

If you are not familiar with Volley, Checkout my post here on volley. It will give you a perfect picture on what Volley can do.

Code – Method 1

StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
		new Response.Listener<String>() {
			public void onResponse(String response) {
				Log.i(TAG, "Success");
		}, new Response.ErrorListener() {

			public void onErrorResponse(VolleyError error) {
				VolleyLog.d("volley", "Error: " + error.getMessage());
				Log.e(TAG, "Success");
		}) {

	public String getBodyContentType() {
		return "application/x-www-form-urlencoded; charset=UTF-8";

	protected Map<String, String> getParams() throws AuthFailureError {
		Map<String, String> params = new HashMap<String, String>();
		params.put("username", edtUsername.getText().toString().trim());
		params.put("password", edtPwd.getText().toString().trim());
		return params;



Here is another method for achieving the same thing.

Code – Method 2


We will create post body first where we will specify the encoding.

public static final String BOUNDARY = "ANY_STRING";

private String createPostBody(Map<String, String> params) {
	StringBuilder sb = new StringBuilder();
	for (String key : params.keySet()) {
		if (params.get(key) != null) {
			sb.append("\r\n" + "--" + BOUNDARY + "\r\n");
			sb.append("Content-Disposition: form-data; name=\"" 
                                   + key + "\"" + "\r\n\r\n");

	return sbPost.toString();

Override the getBody() function

We will use the Post Body here in this function.

public byte[] getBody() {
	Map<String,String> params = new HashMap<>();
	params.add("user_id", userId);
	String postBody = createPostBody(params);
	return postBody.getBytes();

Override the getHeaders() function

You will need to override getHeaders() as well to tell the server what you boundary is :

public Map<String, String> getHeaders() throws AuthFailureError {
	final HashMap<String, String> headers = new HashMap<>();
	headers.put("Content-Type", "multipart/form-data;boundary=" + BOUNDARY;);
	return headers;

Please post your comments here about this article.

4 thoughts on “How to send Send form-urlencoded parameters in post request android volley?

    1. James Post author

      JSON Array can also be converted to a string. Once converted, you can post it like you do with a normal String.

  1. Rahul

    I want to Store Multiple Records like All ArrayList Records in one click in android plz Help


Leave a Reply

Your email address will not be published. Required fields are marked *