Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and PHP login authentication

I am trying to make an app on android in which user needs to log in app to use it. log in authentication will be done by PHP web service. I have a login.java class CustomeHTTPClient and this one is an sample code I have got from internet.There is a method in login.java class name connectphp is connecting to web service and using response to display toast message

Login.java

package com.boyzcorn.android.fyp;

import java.util.ArrayList;
import org.apache.http.NameValuePair;  
import org.apache.http.message.BasicNameValuePair;  


import android.app.Activity;
import android.content.Intent;
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 login extends Activity {
/** Called when the activity is first created. */
     EditText eText ;
    EditText eText2 ;
    Button btnSubmit ;
    Button btnSignup ;

    public void validation()
    {
        if(eText.getText().toString().equals("") ||
                eText.getText().toString().equals(""))
                {
                Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
                }
        else
        {
            connectphp();
        }
        }


    public void connectphp()
    {
    // TODO Auto-generated method stub
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("username", eText.getText().toString()));
    postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString()));
Passing Parameter to the php web service for authentication
    //String valid = "1";
    String response = null;
    try {
    response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php", postParameters);  //Enter Your remote PHP,ASP, Servlet file link
    String res=response.toString();
    // res = res.trim();
    res= res.replaceAll("\\s+","");
    //error.setText(res);
    if(res.equals("1"))
    {
        Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
        Intent i = new Intent(login.this,order_pushing.class);
        startActivity(i);
    }
        else
            if(res.equals("0"))
        {

        Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
        }
    } catch (Exception e) {

    eText.setText(e.toString());
    }}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
 eText = (EditText)findViewById(R.id.uid);
 eText2 = (EditText)findViewById(R.id.editText2);
btnSubmit = (Button)findViewById(R.id.sbtn);
 btnSignup = (Button)findViewById(R.id.signupbtn);
btnSubmit.setOnClickListener(new OnClickListener() {

public void onClick(View v)
{ 
    validation(); (This is to check empty fields)


}
});

btnSignup.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
    Intent i = new Intent(login.this,signup.class);
    startActivity(i);
    }
 });

}}

This is my customhtpclient class in which http connection define.

**CustomHTTPClient.java**

package com.boyzcorn.android.fyp;

import java.util.ArrayList;
import org.apache.http.NameValuePair;  
import org.apache.http.message.BasicNameValuePair;  


import android.app.Activity;
import android.content.Intent;
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 login extends Activity {
/** Called when the activity is first created. */
     EditText eText ;
    EditText eText2 ;
    Button btnSubmit ;
    Button btnSignup ;

    public void validation()
    {
        if(eText.getText().toString().equals("") ||
                eText.getText().toString().equals(""))
                {
                Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
                }
        else
        {
            connectphp();
        }
        }


    public void connectphp()
    {
    // TODO Auto-generated method stub
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("username", eText.getText().toString()));
    postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString()));

    //String valid = "1";
    String response = null;
    try {
    response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php",postParameters);  //Enter Your remote PHP,ASP, Servlet file link 
    String res=response.toString();
    // res = res.trim();
    res= res.replaceAll("\\s+","");
    //error.setText(res);
    if(res.equals("1"))
    {
        Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
        Intent i = new Intent(login.this,order_pushing.class);
        startActivity(i);
    }
        else
            if(res.equals("0"))//Server response if 0
        {

        Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
        }
    } catch (Exception e) {

    eText.setText(e.toString());
    }}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
 eText = (EditText)findViewById(R.id.uid);
 eText2 = (EditText)findViewById(R.id.editText2);
btnSubmit = (Button)findViewById(R.id.sbtn);
 btnSignup = (Button)findViewById(R.id.signupbtn);
btnSubmit.setOnClickListener(new OnClickListener() {

public void onClick(View v)
{ 
    validation();


}
});

btnSignup.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
    Intent i = new Intent(login.this,signup.class);
    startActivity(i);
    }
 });

}}

And my php web service name login.ph

<?php
include("Config.php");


// username and password sent from Form

Receiving parameters $myusername=addslashes($_POST['username']);

$mypassword=addslashes($_POST['pass1']);

$sql="SELECT * FROM signup WHERE user_id='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

$row=mysql_fetch_array($result);

$active=$row['active'];

$count=mysql_num_rows($result);

If result matched $myusername and $mypassword, table row must be 1 row

if($count==1)
    {

echo "1";(If result found send 1 to android)

    }
else
    {
echo "0";(If result not found send o to android)

    }

?>

Config file have all connection parameters to establish connection with database that is mysql and I am using wamp server.
*Config.php*

<?php

$mysql_hostname = "localhost";

$mysql_user = "root";

$mysql_password = "";

$mysql_database = "kse";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)

or
 die("Opps some thing went wrong");

mysql_select_db($mysql_database, $bd) 

or

die("Opps some thing went wrong");
?>

When I am running my app it is giving me toast messgae if I'm giving wrong log in details as I use if and else here

if(res.equals("1"))
  {
      Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
      Intent i = new Intent(login.this,order_pushing.class);
      startActivity(i);
  }
      else
          if(res.equals("0"))
      {

      Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
      }

But when I am giving correct information, no response..Please help me .I am just a beginner to android.If you could make corrections in my code please.

like image 585
Shumaila Hameed Khan Avatar asked Nov 19 '25 17:11

Shumaila Hameed Khan


1 Answers

I forgot to start my web service from thee wamp server i.e wamp- 2.and secondly i delete this line "$active=$row['active'];" from my login.php and its all working now

like image 175
Shumaila Hameed Khan Avatar answered Nov 22 '25 09:11

Shumaila Hameed Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!