如何从数据库接收数据回到android

时间:2013-05-13 20:50:18

标签: php android sql database http-post

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

Login.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");
                }

的login.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应用程序。感谢

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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

Connect android with PHP and MySqlJSON in androidPHP and MySQLi

我使用了这些教程,并设法在没有太多困难的情况下获得您正在尝试的工作。

您可以使用

访问从android发送的传递的get变量
  

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

并且有效的SQL查询将是

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

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