0

我创建了一个名为“login.php”的网络服务,我在其中从 android 发送 id 和密码信息。Web 服务成功捕获了 ID 和密码。我需要将该 ID 和密码与数据库中已经存在的 ID 和密码进行比较,并检查它们是否存在。如果他们这样做了,我需要向 android 发回一条“好的消息”,这样我就可以开始一个新的意图。如果 id 和密码不存在,我想显示一个错误。下面是我的代码。

登录.java

HttpPost httppost = new HttpPost("http://abc.com/webservice/Login.php");
            try {
                   List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

                   nameValuePairs.add(new BasicNameValuePair("userid", et1.getText().toString()));
                   nameValuePairs.add(new BasicNameValuePair("pass", et2.getText().toString()));                     
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                   client.execute(httppost);
                    Log.d("valueeeeeeeeeeee", et6.getText().toString());


                } catch (ClientProtocolException e) {
                         // TODO Auto-generated catch block
                    Log.d("exppppppp", "msg");
                } catch (IOException e) {
                         // TODO Auto-generated catch block
                    Log.d("exppppppp", "msg");
                }

登录.php:

<?php
$host = "localhost"; 
$user = "user"; 
$pass = "pass";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");

$userid = $_POST['userid'];
$pass = $_POST['pass'];

$db_select=mysql_select_db("my_db");
if(!$db_select){
    die(mysql_error());
    echo "error";
}    

我应该在这里运行什么查询来根据它收到的特定 ID 和密码检查数据库,并将“好的消息”发送回 Android 应用程序。谢谢

4

2 回答 2

1

你可以尝试这样的事情:

爪哇:

HttpPost httppost = new HttpPost("http://abc.com/webservice/Login.php");
        try {
               List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

               nameValuePairs.add(new BasicNameValuePair("userid", et1.getText().toString()));
               nameValuePairs.add(new BasicNameValuePair("pass", et2.getText().toString()));                     
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 //This piece of code should do the trick
 HttpResponse response = client.execute(httppost);
 HttpEntity respEntity = response.getEntity();

 if (respEntity != null) {
    // EntityUtils to get the reponse content
    String content =  EntityUtils.toString(respEntity);

 }
                Log.d("valueeeeeeeeeeee", et6.getText().toString());


            } catch (ClientProtocolException e) {
                     // TODO Auto-generated catch block
                Log.d("exppppppp", "msg");
            } catch (IOException e) {
                     // TODO Auto-generated catch block
                Log.d("exppppppp", "msg");
            }

PHP:

<?php
    $host = "localhost"; 
    $user = "user"; 
    $pass = "pass";
    $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");

    $userid = mysql_real_escape_string($_POST['userid']);
    $pass = mysql_real_escape_string($_POST['pass']);

    $db_select=mysql_select_db("my_db");
    if(!$db_select){
        die(mysql_error());
        echo "error";
    }
    $query = "select count(1) as count_users from user_table where user_field = '".$userid."' and pass_field ='".$pass."'";   

    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    if($row['count_users']>0)
    {
        echo "Okey";
    } 
    else
    {
        echo "Not found";
    }

    ?>

PS:请不要使用 mysql_extension,改用 mysqli 或 PDO。

于 2013-05-13T21:18:29.437 回答
0

我建议照你做。向使用 mysqli 连接到 MySQL 数据库的 PHP 页面发起 HTTP post/get 请求(不推荐使用 mysql_query)。然后,您可以将结果形成一个 JSON 响应以传回,并且可以在 android 中轻松解析以提取任何想要的信息。我会推荐这些教程:

将android与PHP和MySqlAndroid中的JSON以及PHP和MySQLi连接起来

我使用了这些教程并设法轻松地完成了您想要做的工作。

您可以使用从 android 发送的传递的 get 变量

$_GET['userid'] 和 $_GET['pass']

一个有效的 SQL 查询将是

$query = 'SELECT * FROM '%table_name%' WHERE uname ="'.$_GET['uname'].'" AND pass ="'.$_GET['pass'].'"';

您需要注意,因为直接在 SQL 语句中使用未经检查的输入会使您容易受到 SQL 注入攻击,因此应尽可能避免。出于在私人服务器上进行调查和试验的目的,您应该没问题。请注意,在分发具有服务器连接性的软件之前,需要考虑很多安全问题

于 2013-05-13T22:02:16.777 回答