Asked  2 Years ago    Answers:  5   Viewed   423 times

I'm experienced with PHP, JavaScript and a lot of other scripting languages, but I don't have a lot of experience with Java or Android.

I'm looking for a way to send POST data to a PHP script and display the result.



Note (Oct 2020): AsyncTask used in the following answer has been deprecated in Android API level 30. Please refer to Official documentation or this blog post for a more updated example

Updated (June 2017) Answer which works on Android 6.0+. Thanks to @Rohit Suthar, @Tamis Bolvari and @sudhiskr for the comments.

    public class CallAPI extends AsyncTask<String, String, String> {
        public CallAPI(){
            //set context variables if required
        protected void onPreExecute() {
         protected String doInBackground(String... params) {
            String urlString = params[0]; // URL to call
            String data = params[1]; //data to post
            OutputStream out = null;

            try {
                URL url = new URL(urlString);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                out = new BufferedOutputStream(urlConnection.getOutputStream());

                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));

            } catch (Exception e) {


  • How to add parameters to HttpURLConnection using POST using NameValuePair

Original Answer (May 2010)

Note: This solution is outdated. It only works on Android devices up to 5.1. Android 6.0 and above do not include the Apache http client used in this answer.

Http Client from Apache Commons is the way to go. It is already included in android. Here's a simple example of how to do HTTP Post using it.

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
Sunday, September 25, 2022

If you don't want your data to be seen by the user, use a PHP session.

Data in a post request is still accessible (and manipulable) by the user.

Checkout this tutorial on PHP Sessions.

Saturday, December 10, 2022

I would NOT use raw sockets to do this. Instead use JSON over HTTP because PHP supports processing HTTP without any special consideration. It's simple to run your PHP pages on the hosted apache instance on GoDaddy or Amazon EC2. Sure you can use sockets, but very very few people actually do that. Massively more people process and respond to HTTP with PHP. That means you'll find vastly more people who can help answer your questions if you follow the herd here. Also there are API libraries on both sides for doing this easily. Using sockets comes with plenty of things you'll have to do yourself or suffer through all the strange bugs that comes with working with raw sockets for the first time.

Also JSON processing is easily supported by both Java and PHP to it's very easy to send the data to the client and server using that.

Well you can certainly use PHP on the backend and Java on the frontend if you like, but I'd suggest canceling your Go-Daddy account and get an Amazon EC2/S3 account because you get a full machine dedicated to whatever you want to put on it. So if you want to do Java on the backend you can just by installing the JDK, Tomcat, etc yourself on the amazon instance and you're good to go. You can also host PHP on there too. There is even plenty of AMI instances pre-installed for Java or PHP stacks.

Wednesday, November 2, 2022

Define a class AsyncT and call it in onCreate method using:

AsyncT asyncT = new AsyncT();

Class definition:

class AsyncT extends AsyncTask<Void,Void,Void>{

        protected Void doInBackground(Void... params) {

            try {
                URL url = new URL(""); //Enter URL here
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST"); // here you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
                httpURLConnection.setRequestProperty("Content-Type", "application/json"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("para_1", "arg_1");

                DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());

            } catch (MalformedURLException e) {
            } catch (IOException e) {
            } catch (JSONException e) {

            return null;

Thursday, October 13, 2022

There are many ways to perform this data transfer using the Android platform.

My preference is to use JSON to communicate between the App and the server. For an example of how to implement this in your app read through this article:

Tuesday, October 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages