java - 无法创建 Android PHP MySQL 登录应用程序

标签 java php android mysql

我正在创建一个 Android PHP MySQL 登录应用程序,它将检查来自 MySQL 服务器的用户名和密码。我从 here 获得教程

我已经检查了 MySQL 中的名称和密码,它与用户类型相匹配,但我仍然无法进入 HomePage Activity ,因为它显示 Invalid用户名或密码。

 private void login(final String username, final String password) {

        class LoginAsync extends AsyncTask<String, Void, String> {

            private Dialog loadingDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
            }

            @Override
            protected String doInBackground(String... params) {
                InputStream is = null;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username", username));
                nameValuePairs.add(new BasicNameValuePair("password", password));
                String result = null;

                try{
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(
                            "http://192.168.1.7:80/Android/CRUD/login.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                String s = result.trim();
                loadingDialog.dismiss();
                if(s.equalsIgnoreCase("success")){
                    Intent intent = new Intent(MainActivity.this, HomePage.class);
                    //intent.putExtra(USER_NAME, username);
                    finish();
                    startActivity(intent);
                }else {
                    Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
                }
            }
        }

        LoginAsync la = new LoginAsync();
        la.execute(username, password);

    }

登录.php

<? php
require_once("dbConnect.php");

$con = mysqli_connect(HOST,USER,PASS,DB);

$username = $_POST['name'];
$password = $_POST['password'];

$sql = "select * from users where name='$username' and password='$password'";

$res = mysqli_query($con,$sql);

$check = mysqli_fetch_array($res);

if(isset($check)){
echo 'success';
}else{
echo 'failure';
}

mysqli_close($con);
?>

最佳答案

$username = $_POST['name'];

改成这个:

$username = $_POST['username'];

关于java - 无法创建 Android PHP MySQL 登录应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34976203/

相关文章:

java - 在 Java 中使用嵌套枚举类型

php - 如何在一行显示 4 <li> 而在另一行显示其他 4 <li>

javascript - 如何使用 javascript 在 WebView 中操作本地文件中的 CSS

Android:当 AudioManager 中的当前模式发生变化时得到通知

android - 如何在扩展 LifecycleActivity 的 View 中设置SupportActionBar

java - 如何访问 View 中 URI 模板中的路径变量(Spring 3/SpringMVC)?

java - jackson 反序列化缺少默认值

java - 使用 Spring 4 PropertySource 时找不到可重复

php - 将表单中的所有用户值提交到数据库中

PHP:使用来自 php 的参数调用 javascript 函数