Android登录-检查用户名和密码是否与MySQL数据库匹配


Android Login- checking if username and password match with MySQL database

我正在开发一个android应用程序,在应用程序开发方面有点新。PHP和MySQL被用于后端。我正在构建登录页面,我想检查用户名和密码是否与JSON解析后的数据库匹配。我已经解析了JSON,现在只剩下检查部分了。我觉得我应该做一个列表,并使用foreach循环查询它,但我不知道怎么做。任何建议或代码示例将非常感激!这是我的login和onPostExecute方法-

public void login(View view) {
    Log.i("username", userNameField.getText().toString());
    Log.i("password", passwordField.getText().toString());
    DownloadTask task = new DownloadTask();
    task.execute("http://something.com.php");

}
@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.i("Website Content", result);
        try {

           JSONArray arr = new JSONArray(result);
          for (int i = 0; i < arr.length(); i++) {
              JSONObject jsonPart = arr.getJSONObject(i);
         Log.i("user", jsonPart.getString("email"));
         Log.i("pass", jsonPart.getString("password"));
              dbUserName= jsonPart.getString("email");
              dbPassword= jsonPart.getString("password");
         // if ((userName == dbUserName) && (password == (dbPassword))){
           //  Log.i("AppInfo", "Login Successful!");
        //   }
         // else{
         //   Log.i("Appinfo", "you suck");
         //   }
          } catch (JSONException e) {
            e.printStackTrace();
        }
    }

您不需要为每个循环编写代码。您必须对something.com.php进行GET/POST调用,并将您的用户名和密码发送到服务器。

然后您必须检查用户名和密码是否与数据库匹配。将逻辑写入something.com.php

如果是匹配,发送一个成功标志到你的应用程序为1和0,否则

在你的应用程序中解析成功标志并检查它是0还是1。

基于它让用户登录你的应用。

:

https://www.simplifiedcoding.net/android-php-mysql-login-tutorial-android-login-app-3/

http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

你需要发送登录详细信息到服务器,并检查服务器的成功或失败,然后按照响应在你的应用程序中做。这里是它的简单例子

  1. 首先从编辑文本
  2. 获取用户名和密码
String UserName=EditText_UserName.getText().toString();
String Password=EditText_Password.getText().toString();
  • 像这样发送这些字符串给AsyncTask
  • new asynctask().execute(UserName,Password)

  • 获取用户名和密码在doInBackground(String…params)
  • Phone=params[0];

    Password=params[1];

    3:像这样将用户名和密码编码为单个字符串

    > String data = URLEncoder.encode("UserName", "UTF-8") + "=" +
    > URLEncoder.encode(UserName, "UTF-8");
    > data += "&" + URLEncoder.encode("Password", "UTF-8") + "=" + URLEncoder.encode(Password, "UTF-8");
    

    4:现在在BufferedWriter的帮助下将数据写入URL

    URL url = new URL("http://www.Example.com/Login.php");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    OutputStream os = connection.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
    writer.write(data);
    writer.flush();
    writer.close();
    os.close();
    

    5:现在在Php文件中比较结果与数据库并将响应发送回android应用程序,像这样

    //Get Values first 
    $UserName=$_POST['UserName'];
    $Password =$_POST['Password'];
    //then compare it in SQL DATABASE
    $sql = "SELECT * FROM mobile_App WHERE  UserName = '$UserName' AND Password = '$Password'";
    //Send Response to app if query success
    $response['status'] = 'SUCCESS'; 
    //else send failed
    

    6:Check Response Code in App

    int responseCode = connection.getResponseCode();//It will return 200 if Connection Established with server
    //Get Success String or Fail String By InputStream
    

    7:最后比较来自服务器的字符串成功或失败,然后进行进一步的操作。