Página de inicio de sesión de Android con MySQL

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.

Aqui esta mi codigo

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;


public void onCreate(Bundle savedInstanceState) { 



    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() { 


        public void onClick(View v) { 

            StrictMode.ThreadPolicy policy = new                                                                             

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

            postParameters.add(new BasicNameValuePair("Username",  

            postParameters.add(new BasicNameValuePair("password",   

             /*            String valid = "1";*/      

            String response = null;

            try {

               response =  

               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);


               else lblResult.setText("Incorrect Username or Password Entered");


            catch (Exception e) {





Y código PHP



$conn = mysql_connect('localhost', 'root', 'user');

$query = 'SELECT * FROM login WHERE username = ‘$un’ AND password = ‘$pw’';
$result = mysql_query($query) or die ('Unable to verify user because : ' .    

if (mysql_num_rows($result) > 0) { 

echo 1; 
else { 

// print status message 
 echo 0; 

preguntado el 30 de enero de 12 a las 19:01

are you getting "Incorrect Username or Password Entered" message? -

OK so I've change the single quotes but now getting the message 'org.apache.http.NoHttpResponseException:The target server failed to respond' in the emulator -

we need to be more focused: does the PHP work and the android code fails ? if you're not sure try posting the user/pwd directly to the php file and see. then we can take it from there (after you post the exact error that you get). -

@alfasin ok so the error i'm getting is showing just within the emulator and not eclipse. After i put the un/pw in the error 'org.apache.http.NoHttpResponseException:The target server failed to respond' in the emulator' replaces the un in the un textbox. I've tried the php code without the application and found some errors. The errors are -

continued 'Notice: Undefined index: username in C:\xampp\htdocs\check.php on line 3 Notice: Undefined index: password in C:\xampp\htdocs\check.php on line 4 Notice: Undefined variable: un’ in C:\xampp\htdocs\check.php on line 9 Notice: Undefined variable: pw’ in C:\xampp\htdocs\check.php on line 9 Unable to verify user because : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '‘ AND password = ‘' at line 1' -

3 Respuestas

I'm thinking that there might be an issue on this line:

$query = 'SELECT * FROM login WHERE username = ‘$un’ AND password = ‘$pw’';

It looks like you're trying to use different kinds of single-quotes. Try this instead:

$query = "SELECT * FROM login WHERE username = '$un' AND password = '$pw'";

Also, you should escape special chars in the username and password to protect against SQL injection attacks.

$un = mysql_real_escape_string($_POST['username']);
$pw = mysql_real_escape_string($_POST['password']);

Check out this past SO question for info on the "org.apache.http.NoHttpResponseException: The target server failed to respond" HttpClient en Android: NoHttpResponseException a través de UMTS / 3G

contestado el 23 de mayo de 17 a las 15:05

OK so I've change the single quotes but now getting the message 'org.apache.http.NoHttpResponseException:The target server failed to respond' in the emulator - user1058469

@user1058469 can you get into the DB with MySQL Workbench or Query Analyzer? - Aaron

I can get into the database using MySQL Workbench yeah - user1058469

@user1058469 edit made. Check the linked question, hopefully it helps. - Aaron

Thanks for the link. Sorry, i'm really new at this, i've tried HttpProtocolParams.setUseExpectContinue(CustomHttpClient.getParams(), false); Could i just check where exactly this should fit into my code? Got an error with the getParams(). Thanks - user1058469

necesitas cambiar esto

postParameters.add(new BasicNameValuePair("Username", 

Dentro de esto

postParameters.add(new BasicNameValuePair("username",

String must be the same

Respondido el 21 de junio de 12 a las 17:06

I think maybe you forgot to add activity into AndroidManifest.xml file.

Solo agrega:

activity android:name=".mainmenu" 

into AndroidManifest.xml

Respondido 18 Feb 13, 23:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.