Android登录-检查用户名和密码是否与MySQL数据库匹配 [英] Android Login- checking if username and password match with MySQL database

查看:1249
本文介绍了Android登录-检查用户名和密码是否与MySQL数据库匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用,并且有点新的应用开发. PHP和MySQL被用作后端.我正在构建登录页面,我想在JSON解析后检查用户名和密码是否与数据库匹配.我已经解析了JSON,现在只剩下检查部分.我觉得我应该列出一个列表并使用foreach循环对其进行查询,但我不知道如何.任何建议或代码示例将不胜感激!这是我的登录信息和onPostExecute方法-

I am working on an android app and kinda new in app development. PHP and MySQL are being used for backend. I am building the login page and I want to check if username and password match with database after JSON parsing. I have already parsed JSON and now only the check part is left. I feel like I should make a list and query it using a foreach loop, but I don't know how. Any suggestions or code example would be greatly appreciated! Here is my login and onPostExecute method-

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调用,然后发送用户名并服务器密码.

You do not need to write for each loop.You have to make a GET/POST call to something.com.php and send your username and password to the server.

然后,您必须检查用户名和密码与数据库匹配的天气.在 something.com.php

Then you have to check weather the username and password matches with your database.Write that logic in something.com.php

如果匹配,请向您的应用发送成功标志,否则为1,否则为0.

If it is a match send a success flag to your app as 1 and 0 otherwise.

在应用中解析成功标志,并检查天气为0或1.

Parse the success flag in your app and check weather it is 0 or 1.

基于此,用户可以登录您的应用.

Based on it let the user login your app.

请参阅:

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/

这篇关于Android登录-检查用户名和密码是否与MySQL数据库匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆