title: Post data from Android to PHP script on localhost
body: <p>I am new to Android and trying to post data to localhost from my Android App. While it is not showing any errors, it's just displaying <code>0&lt;br&gt;0</code> in Android code. But in PHP code it's displayed <code>"empty"</code><br>
  My php code is:<br></p>
  
  <pre><code>&lt;?php
              if (empty($_POST['name']) || empty($_POST['pass']))
          {
              echo "empty";
          }
          else{
              $name = $_POST['name'];
              $pass = $_POST['pass'];
              echo "Name:" + $name;
              echo "&lt;br&gt;";
              echo "Pass:" + $pass;
          }
  ?&gt;
  </code></pre>
  
  <p>I'm just sending username and password from an Android App to print in an PHP page. My Android code is:</p>
  
  <pre><code>    package com.example.connecttophp;
  
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.List;
  
      import org.apache.http.HttpResponse;
      import org.apache.http.NameValuePair;
      import org.apache.http.client.ClientProtocolException;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.entity.UrlEncodedFormEntity;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.message.BasicNameValuePair;
      import org.apache.http.util.EntityUtils;
  
      import android.os.AsyncTask;
      import android.os.Bundle;
      import android.app.Activity;
      import android.view.Menu;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
  
      public class MainActivity extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final EditText usn = (EditText)findViewById(R.id.editText1);
      final EditText pwd = (EditText)findViewById(R.id.editText2);
      final TextView logme = (TextView)findViewById(R.id.textView3);
      Button clear = (Button)findViewById(R.id.button2);
      clear.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
              usn.setText("");
              pwd.setText("");
              logme.setText("");
          }
      });
      Button send = (Button)findViewById(R.id.button1);
      send.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              new SendDatatoPhp(logme).execute(usn.getText().toString(),pwd.getText().toString());
          }
      });
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
     }
  
     class SendDatatoPhp extends AsyncTask&lt;String,String,String&gt;{
  
  TextView logme;
  public SendDatatoPhp(TextView et){
      logme = et;
  }
  
  @Override
  protected String doInBackground(String... arg0) {
      // TODO Auto-generated method stub
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://10.0.2.2/demo.php");
      String responseBody;
      try{
      List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
      nameValuePairs.add(new BasicNameValuePair("name",arg0[0]));
      nameValuePairs.add(new BasicNameValuePair("pass",arg0[1]));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);
      responseBody = EntityUtils.toString(response.getEntity());
      return responseBody;
      }
      catch (ClientProtocolException e) {
          // TODO Auto-generated catch block
      } 
      catch (IOException e) {
          // TODO Auto-generated catch block
      }
      return "Login Not Successful";
  }
  protected void onPostExecute(String response){
      logme.setText(response);
        }
      }   
  </code></pre>
  
  <p>My XML is as follows </p>
  
  <pre><code>   &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="16dp"
      android:layout_marginTop="28dp"
      android:text="Enter Username:"
      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  &lt;EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView1"
      android:layout_below="@+id/textView1"
      android:ems="10" &gt;
  
  &lt;/EditText&gt;
  
  
  &lt;TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editText1"
      android:layout_below="@+id/editText1"
      android:layout_marginTop="36dp"
      android:text="Enter Password"
      android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
  
  &lt;EditText
      android:id="@+id/editText2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignRight="@+id/editText1"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="33dp"
      android:ems="10"
      android:inputType="textPassword" &gt;
  &lt;/EditText&gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editText2"
      android:layout_below="@+id/editText2"
      android:layout_marginTop="37dp"
      android:text="Send" /&gt;
  
  &lt;Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/button1"
      android:layout_alignRight="@+id/editText1"
      android:layout_marginRight="28dp"
      android:text="Clear" /&gt;
  
  &lt;TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/textView2"
      android:layout_below="@+id/button1"
      android:layout_marginTop="62dp"
      android:text="Small Text"
      android:textAppearance="?android:attr/textAppearanceSmall" /&gt;
  
     &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I have declared permission in mainfest file.</p>
  
  <p>I don't know what the error is.</p>
  
  <p>I want the PHP file to receive username and password and print it.</p>
  
  <p>How can I do this ?</p>