1

嗨,我是 android 新手,我正在开发一个有登录屏幕的应用程序。

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

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

我正在使用 mysql 工作台创建数据库。

我正在使用 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

4 回答 4

2

是您正在搜索的完整教程..

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

于 2013-01-16T06:10:41.447 回答
1

为此,您应该创建一个 php 应用程序并将其托管在服务器上。然后使用 php 创建函数(或在线可用的链接),这些函数将被 android 应用程序命中,它们(函数)会将数据输出为 json_encoded,这是 android 所需的。您可以轻松地使用这种方式来设置您的应用程序。
我通常使用 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);
}

安卓需要这个

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

编辑:
如果你使用普通 php,你可以这样做

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

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

从地址栏你可以像这样访问它

 http://tomcatserver/test.php/index/testuser/testpassword

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

于 2013-01-16T05:55:03.157 回答
1

我想简要介绍一下raheel的回答,但以我自己的方式。重要的是您需要服务器-客户端通信来访问您的 my 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;

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

于 2013-01-16T06:02:14.450 回答
1

我到底需要在哪里放置 php 文件

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

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

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

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

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

现在它只是一种您可以实际为您的 android 应用程序提供 PHP 和 MySQL 的方式。我认为教程已经教给您有关使用 php 和 mysql 连接的知识。对于数据交换,您需要了解 XML 或 JSON,我认为后面的教程已经为您介绍了它。

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

于 2013-01-16T06:16:43.253 回答