使用mysql数据库的Android登录活动

时间:2013-01-16 05:50:06

标签: php android mysql phpmyadmin

您好我是Android的新手,我正在开发一个具有登录屏幕的应用程序。

我尝试了很多教程,但我不知道如何将mysql数据库连接到我的Android应用程序。在所有教程中,他们使用php将android应用程序连接到数据库。

我想知道的是,我需要在哪里放置php文件以及我需要在何处以及如何创建php文件。

我使用mysql workbench创建数据库。

我使用eclipse 4.2编写java代码。

您好我正在使用以下代码检查我输入的用户名是否正确以及PHP代码,但问题是它总是提供不正确的用户名。

String response = null;
try {
response = CustomHttpClient.executeHttpPost("http://192.168.3.27/abc/check.php", postParameters);  //Enetr Your remote PHP,ASP, Servlet file link
String res=response.toString();
// res = res.trim();
res= res.replaceAll("\\s+","");
//error.setText(res);
error.setText(res);
if(res.equals("1")){
    Intent myIntent = new Intent(LoginActivity.this, HomeActivity.class);           
startActivity(myIntent);

}
else
error.setText("Sorry!! Incorrect Username or Password");

这是我的PHP代码。

<?php
$un=$_POST['username'];
$pw=$_POST['password'];
//connect to the db
$user = ‘root’;
$pswd = ‘root’;
$db = ‘mylogin’;
$conn = mysql_connect(‘localhost’, $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
$query = “SELECT * FROM userpass WHERE login_id = ‘$un’ AND login_pass = ‘$pw’”;
$result = mysql_query($query) or die(“Unable to verify user because : ” . mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)

echo 1;  // for correct login response
else
echo 0; // for incorrect login response
?>

4 个答案:

答案 0 :(得分:2)

Here是您要搜索的完整教程..

就像你的普通html页面一样,将android设备的登录值发布到url并在php文件中进行身份验证,就像示例一样

答案 1 :(得分:1)

为此,您应该创建一个php应用程序并将其托管在服务器上。 然后使用php创建函数(或在线可用的链接)将被Android应用程序命中,他们(函数)将输出数据作为android所需的json_encoded。您可以轻松地使用这种方式设置您的应用程序 我通常使用Codeigniter框架,它的工作原理如下。

myapp/
    system/
    application/
         controllers/
             mycontroller

mycontroller有一些像这样的功能

function test(){
    $username = $this->uri->segment(3);
    $password = $this->uri->segment(4);
    // other stuff like fetch data from db table in the variable $data

    header('Content-Type: application/json');
    echo json_encode($data);
}

Android需要这个

http://testdomain/myapp/index.php/mycontroller/test/username/password

编辑:
如果你是usig plain php,你可以这样做

  1. 在您的服务器上创建一个文件夹。
  2. 创建一个文件test.php创建一个连接文件connection.php
  3. 在第一个文件中包含第二个文件。
  4. 在test.php中创建一个名为
  5. 的函数

    include('connection.php');

    function index($username,$password)
    {
        //query database table
        //if result is success full
        //  $data   =   array('message'=>'login success');
        //else
        //  $data   =   array('message'=>'login failed');
        //
        //header('Content-Type: application/json');
        //echo json_encode($data);  
    }
    

    function index($username,$password) { //query database table //if result is success full // $data = array('message'=>'login success'); //else // $data = array('message'=>'login failed'); // //header('Content-Type: application/json'); //echo json_encode($data); }

    从地址栏

    可以像这样访问

    你应该研究一下php是如何工作的

答案 2 :(得分:1)

我想简要介绍一下raheel的答案,但是以我自己的方式。重要的是你需要一个服务器 - 客户端通信来访问我的sql。为此,您需要创建一个响应Android应用程序请求的服务器。您的要求如下:

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://example.com/script.php?var1=androidprogramming");
try {
    HttpResponse response = httpclient.execute(httpget);
    if(response != null) {
        String line = "";
        InputStream inputstream = response.getEntity().getContent();
        line = convertStreamToString(inputstream);
        Toast.makeText(this, line, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Unable to complete your request", Toast.LENGTH_LONG).show();
    }
} catch (ClientProtocolException e) {
    Toast.makeText(this, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    Toast.makeText(this, "Caught IOException", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
    Toast.makeText(this, "Caught Exception", Toast.LENGTH_SHORT).show();
}

输出的消耗将是:

private String convertStreamToString(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    try {
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }
    } catch (Exception e) {
        Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
    }
    return total.toString();
}

然后服务器端可以像:

$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die('Unable to connect');
$db = mysql_selectdb(DB_NAME,$connection) or die('Database not found');
if(isset($_GET['var'])) :
    $query = "SELECT [a few parameters] FROM [some table] WHERE [some conditions]";
    $resultset = mysql_query($query);
    $row = mysql_fetch_array($resultset)
    echo $row['[column name]'];
else:
    echo "No get Request Received";
endif;

这些只是您可以构建的片段

答案 3 :(得分:1)

  

我到底需要放置php文件

您需要将PHP文件放在服务器中。 (您可以使用localhost,即您的计算机,但对于您以外的设备,您需要将其保存在您需要购买域名和托管服务的网络服务器上。)

  

我需要在哪里以及如何创建php文件。

您可以在机器本身中创建PHP文件。它就像创建文本文件一样简单,但扩展名为.php。在Windows上使用wamp(Linux的LAMP),您可以测试它。它里面有一个MySQL。 LAMP和WAMP默认会有apache服务器。

在您完成编写PHP代码和测试后不久,您可以通过FTP将文件传输到您的网络服务器。现在要配置MySQL数据库,您实际上可以在Web服务器上使用控制面板。

您需要使用Android应用程序的URL来链接PHP文件,这些PHP文件依次与MysQL交互。登录让我们认为你已经创建了一个php文件作为login.php。在您的本地主机上,您可以将其引用为http://localhost/myapp/login.php如果您需要在购买的网络服务器上获取它,那么您的网址将为http://www.yourwebsite.com/myapp/login.php。请注意,myapp只是您上传php文件的文件夹。

现在它只是一种方法,你可以实际拥有PHP和MySQL的Android应用程序。我认为教程已经教你如何使用php和mysql连接。对于数据交换,您需要了解XML或JSON我认为后面的教程已经为您介绍了它。

你甚至有一个eclipse插件可以使用php。只需通过互联网获取有关如何安装它的帮助。 This视频可能会对您有所帮助。