1

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.

Here's my 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<NameValuePair> postParameters = new ArrayList<NameValuePair>();

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


            }



        }


     });
   }
}

And PHP code

 <?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) > 0) { 

echo 1; 
} 
else { 

// print status message 
 echo 0; 
}
?>
Aaron
  • 55,518
  • 11
  • 116
  • 132
  • are you getting "Incorrect Username or Password Entered" message? – kosa Jan 30 '12 at 19:45
  • 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 Jan 30 '12 at 19:59
  • 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). – Nir Alfasi Jan 30 '12 at 21:30
  • @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 – user1058469 Jan 31 '12 at 09:47
  • 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' – user1058469 Jan 31 '12 at 09:49
  • Change the $_POST to $_GET and call your script with: http://localhost/check.php?username=&password= add "echo" to screen in your php script then you'll see exactly where is the point of fauiler – Nir Alfasi Jan 31 '12 at 20:49
  • Hi where are you keeping the php file even i have the same code but when i type the address localhost:8080/check.php i get a 404 error..Please help i am new to android – user1844638 Jan 16 '13 at 05:17

3 Answers3

0

you need to change this

postParameters.add(new BasicNameValuePair("Username", 

Into this

postParameters.add(new BasicNameValuePair("username",

String must be the same

Zamani
  • 306
  • 3
  • 15
0

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

Just add:

activity android:name=".mainmenu" 

into AndroidManifest.xml

demongolem
  • 9,474
  • 36
  • 90
  • 105
emon
  • 1
  • 1
  • 3
0

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 on Android : NoHttpResponseException through UMTS/3G

Community
  • 1
  • 1
Aaron
  • 55,518
  • 11
  • 116
  • 132
  • 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 Jan 30 '12 at 20:00
  • @user1058469 can you get into the DB with MySQL Workbench or Query Analyzer? – Aaron Jan 30 '12 at 20:13
  • I can get into the database using MySQL Workbench yeah – user1058469 Jan 30 '12 at 20:24
  • @user1058469 edit made. Check the linked question, hopefully it helps. – Aaron Jan 30 '12 at 20:39
  • 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 Jan 30 '12 at 21:01
  • It looks like it needs to go where you define our HTTP request. I see your HTTP response, but not the request. Is that in another section of code? – Aaron Jan 31 '12 at 15:34