Empowering you to understand your world

How To Send A POST Request In Android

Posted By Nicholas Brown.

Android Tutorials

There are multiple networking libraries and classes available for Android that can send POST requests, but this tutorial covers the use of Volley, Google’s library.

Volley makes it easy to send HTTP ‘POST’ requests from your Android tablet or smartphone. To make it even easier, Android Studio speeds up the coding process by generating some of the POST request code for you. As is the case with all libraries, Volley doesn’t suit all applications. Please read more about the strengths and weaknesses of each library. You can use Eclipse, but i’d highly recommend Android Studio for its stability.

There are various ways to send a POST request using Volley, but this article explains how to use the StringRequest with a HashMap. If you need to send a POST request with JSON data using Volley, you can do so with a JsonObjectRequest, and if you need to send or receive a JSON array in your POST request, you can use a JsonArrayRequest.

First, you need to add the Volley library as a dependency of your project, as shown in the video below.

Hire An Android Developer

Then import the Volley and HashMap library into the Activity you’re sending the POST request from.

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

Now you can begin coding your HTTP POST request.

Example Code: Sending POST Data In Android

Declare a RequestQueue:

RequestQueue MyRequestQueue = Volley.newRequestQueue(this);

Create a StringRequest (you need to include the ‘http’ in the url, otherwise your POST request will fail):

String url = "http://yourdomain.com/path";
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //This code is executed if the server responds, whether or not the response contains data.
            //The String 'response' contains the server's response. 
        }
    }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
        @Override
        public void onErrorResponse(VolleyError error) {
            //This code is executed if there is an error.
        }
    }) {
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put("Field", "Value"); //Add the data you'd like to send to the server.
            return MyData;
        }
    };
}

MyRequestQueue.add(MyStringRequest);


Note the use of Request.Method.POST in the StringRequest. That instructs Volley to send a POST request. If you changed that to Request.Method.GET, it would send a ‘GET’ request instead. Get requests are typically used to download URLs, while POST requests are used to send data, but POST requests can be used for downloads as well (if the server is configured for that).

Be sure to add the ‘INTERNET’ permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Voila! You’re done.

Further Reading: How To Receive POST Requests Using Node.js.

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published