【问题标题】:Why am i getting a null value on my alertDialog.setMessage()?为什么我的 alertDialog.setMessage() 上出现空值?
【发布时间】:2019-08-31 12:09:00
【问题描述】:

我是安卓应用的初学者。我不知道为什么我的 alertDialog 框总是显示 null 值。该代码用于通过检查我的数据库上的数据log。将不胜感激。

我已将 MySql 端口从 3306 更改为 3307,因为 3306 已被阻止。

public class BackgroundWorker extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx) {
        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://192.168.254.106/login.php";
        if(type.equals("login")) {
            try {
                String user_name = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

}

这里是 login.php

<?php
require "connection.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from account_profiles where userName like '$user_name' and passWord like '$user_pass';";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0) {
echo "Login Success";
}
else {
echo "Login Failed";
}
?>

【问题讨论】:

  • 1.编辑您的问题以添加 php 代码。 2.你很容易受到sql注入。 3.请散列您的密码,不要将它们保存为明文。

标签: java php android android-studio


【解决方案1】:

在类构造函数中处理 AlertDialog 的实例化,而不是在 onPreExecute 中,这没有意义,并且会解决您的问题。

BackgroundWorker (Context context ) {
    alertDialog = new AlertDialog.Builder(context).create();
}

现在您也可以删除 Context 全局变量。

【讨论】:

  • 感谢帮助,但它仍然返回一个空值。
  • result 与此代码 while((line = bufferedReader.readLine())!= null) { result += line; } 的值是我认为导致问题的原因。我的语法错了吗?
  • 你说,“我的 alertDialog.setMessage() 上的空值”。现在你说结果是空的。你的问题很糟糕,没有现金记录。
猜你喜欢
  • 1970-01-01
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多