Android和PHP登录认证


Android and PHP login authentication

我试图在android上制作一个应用程序,用户需要登录应用程序才能使用它。登录身份验证将由PHP web服务完成。我有一个login.javaCustomeHTTPClient,这是我从互联网上得到的一个样本代码。在login.java类名称中有一个方法connectphp连接到web服务并使用响应来显示toast消息

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

这是我的customhtpclient类,其中http连接定义。

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

和我的php web服务名login.ph

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

// username and password sent from Form

接收参数=上的用户名都美元dangillmor addslashes ($ _POST['用户名']);

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

如果结果匹配$myusername和$mypassword,则表行必须为1行

if($count==1)
    {
echo "1";(If result found send 1 to android)
    }
else
    {
echo "0";(If result not found send o to android)
    }
?>

配置文件有所有的连接参数来建立与数据库的连接,即mysql和我使用的是wamp服务器。
* 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");
?>

当我运行我的应用程序时,如果我在这里使用if和else时给出错误的登录细节,它会给我toast消息

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

但是当我给出正确的信息,没有回应..请帮助我,我只是一个初学者的android。如果你能更正我的代码,请。

我忘记从三个wamp服务器启动我的web服务,即wamp- 2。其次,我从我的login。php中删除这行$active=$row['active'];它现在都工作了