title: Android log in page with MySQL
body: <p>Hey i'm new to android and i'm having trouble with a login page. I'm trying to connect to a MySQL database containing usernames and passwords. I've tried to get my code to work but it just won't let me log in.</p>
  
  <p><strong>Here's my code</strong></p>
  
  <pre><code>package login.CBA; 
  
  import android.app.Activity; 
  import android.content.Intent;
  import android.os.Bundle; 
  import android.os.StrictMode;
  import android.view.View; 
  import android.view.View.OnClickListener; 
  import android.widget.Button; 
  import android.widget.EditText; 
  import android.widget.TextView; 
  
  import java.util.ArrayList;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.apache.http.params.HttpProtocolParams;
  
  public class login extends Activity { 
  
  private EditText etUsername; 
  
  private EditText etPassword; 
  
  private Button btnLogin; 
  
  private Button btnCancel; 
  
  private TextView lblResult;
  
  
  @Override 
  
  
  public void onCreate(Bundle savedInstanceState) { 
  
      super.onCreate(savedInstanceState); 
  
  
  
      setContentView(R.layout.main); 
  
  
  
      etUsername = (EditText)findViewById(R.id.Username); 
  
      etPassword = (EditText)findViewById(R.id.password); 
  
      btnLogin = (Button)findViewById(R.id.login); 
  
      btnCancel = (Button)findViewById(R.id.Cancel); 
  
      lblResult = (TextView)findViewById(R.id.result); 
  
  
  
      btnLogin.setOnClickListener(new OnClickListener() { 
  
          @Override 
  
          public void onClick(View v) { 
  
  
  
              StrictMode.ThreadPolicy policy = new                                                                             
              StrictMode.ThreadPolicy.Builder().permitAll().build(); 
              StrictMode.setThreadPolicy(policy);
  
  
  
              ArrayList&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();
  
              postParameters.add(new BasicNameValuePair("Username",  
              etUsername.getText().toString()));
  
              postParameters.add(new BasicNameValuePair("password",   
              etPassword.getText().toString()));
  
               /*            String valid = "1";*/      
  
              String response = null;
  
  
              try {
  
                 response =  
                 CustomHttpClient.executeHttpPost("localhost/check.php", 
                 postParameters);
  
                 String res = response.toString();
  
                 //res = res.trim();
  
                 res = res.replaceAll("\\s+","");
  
                 // error.setText(res);
  
                 if (res.equals("1")) {
  
                     Intent i = new Intent(getApplicationContext(), mainmenu.class);
                 startActivity(i);
  
                 }
  
                 else lblResult.setText("Incorrect Username or Password Entered");
  
              } 
  
              catch (Exception e) {
  
                 etUsername.setText(e.toString());
  
  
              }
  
  
  
          }
  
  
       });
     }
  }
  </code></pre>
  
  <p><strong>And PHP code</strong></p>
  
  <pre><code> &lt;?php
  
  $un=$_POST['username'];
  $pw=$_POST['password'];
  
  $conn = mysql_connect('localhost', 'root', 'user');
  mysql_select_db('cba');
  
  $query = 'SELECT * FROM login WHERE username = ‘$un’ AND password = ‘$pw’';
  $result = mysql_query($query) or die ('Unable to verify user because : ' .    
  mysql_error());
  
  if (mysql_num_rows($result) &gt; 0) { 
  
  echo 1; 
  } 
  else { 
  
  // print status message 
   echo 0; 
  }
  ?&gt;
  </code></pre>