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.



  1. Jesus October 26, 2017 at 6:05 am

    Hi, what would be the parameters if I want to post a JSONArray?

    • James October 27, 2017 at 5:35 pm

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

  2. Rahul December 4, 2017 at 8:00 am

    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 *




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.