title: httpPost android php
body: <p>i am sending a username from android client to a php server , but it seems that POST doesn't work and the username is not read by the server ...</p>
  
  <p>android code </p>
  
  <pre><code>package com.example.election;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.http.HttpEntity;
  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.ResponseHandler;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.BasicResponseHandler;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  import org.apache.http.protocol.BasicHttpContext;
  import org.apache.http.protocol.HttpContext;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  public class authentification extends Activity {
  
      Button Quitter, Annuler, Valider;
      EditText login,password;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.authentification);
          Quitter = (Button) findViewById(R.id.Quitter);
          Valider = (Button) findViewById(R.id.Valider);
          Annuler = (Button) findViewById(R.id.Annuler);
          login = (EditText) findViewById(R.id.login);
  
          password = (EditText) findViewById(R.id.password);
  
  
          Valider.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
                  verifier_admin(v);
  
              }
          });
  
  
          Quitter.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  Intent intent = new Intent(getApplicationContext(),
                          MainActivity.class);
                  startActivity(intent);
  
              }
          });
  
      }
  
  
      public void reset(View v){
          login = (EditText) findViewById(R.id.login);
          password = (EditText) findViewById(R.id.password);
          login.setText("");
          password.setText("");
  
      }
  
      public void verifier_admin(View v){
          InputStream is = null;
          HttpClient httpclient = null;
          HttpPost httppost = null;
          HttpResponse response = null;
          HttpEntity entity = null;
  
          ArrayList&lt;NameValuePair&gt; nameValuePairs = null; 
          String pass;
          String log = login.getText().toString();
  
          HttpClient client = new DefaultHttpClient();
          HttpPost post = new HttpPost("http://172.16.50.239:80/android/test.php");
          HttpContext localContext = new BasicHttpContext();
  
          pass =password.getText().toString();
  
          if(log.length()&gt;0 ){
  
              try{
                  List&lt;NameValuePair&gt; var = new ArrayList&lt;NameValuePair&gt;(2);  
  
                  var.add(new BasicNameValuePair("log",log));
  
                  //var.add(new  BasicNameValuePair("pass",pass));
                 // post.setEntity(new UrlEncodedFormEntity (var,"utf-8"));
                  post.setEntity(new UrlEncodedFormEntity (var));
                  ResponseHandler&lt;String&gt; responseHandler=new BasicResponseHandler();
                  String reponse =client.execute(post,responseHandler);
  
                  //BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                  //String s = reader.readLine();
                  Toast.makeText(this, "message envoyé",Toast.LENGTH_SHORT).show();
                  login.setText("");
  
  
              }
              catch(ClientProtocolException e )
              {
                  e.printStackTrace();
              }
              catch(IOException e )
              {
                  e.printStackTrace();
              }
  
          }
  
          else {
              Toast.makeText(this, "veuillez entrez vos données",Toast.LENGTH_SHORT).show();
  
          }
  }
  }
  </code></pre>
  
  <p>php code </p>
  
  <pre><code>&lt;?php
  if( isset($_POST)  ){
  print_r($_POST);
  
  
  
  
  //connexion base
  $link = mysql_connect("localhost","root","") or die ('ERREUR '.mysql_error()) ;
  $bd_selected=mysql_select_db("mydb",$link) or die ('ERREUR1 '.mysql_error());
  
  $req1=" SELECT * FROM user WHERE (login= '$log')";
  
  $sql=mysql_query($req); 
  $data = mysql_num_rows($sql);// affiche le nombre de ligne satisfaisant condition
  echo $req;
  
  
  
  mysql_close($link);
  }
  ?&gt;
  </code></pre>
  
  <p>the error is that the $log=$_POST['log'] undefined </p>