java - 为什么我的 alertDialog.setMessage() 上出现空值?
问题描述
我是做 android 应用程序的完整初学者。我不知道为什么我的alertDialog框总是显示空值。该代码用于通过检查我的数据库上的数据进行登录。将不胜感激任何帮助。
我已将 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";
}
?>
解决方案
AlertDialog
处理类构造函数中的实例化onPreExecute
没有意义,并将解决您的问题。
BackgroundWorker (Context context ) {
alertDialog = new AlertDialog.Builder(context).create();
}
现在您也可以删除Context
全局变量。
推荐阅读
- php - PHP:我怎么知道是什么导致了 500 服务器错误?
- azure-devops - Azure DevOps 2020 - 如何选择为发布管道下载哪些工件 (18.170.30525.1 (Azure DevOps Server 2020))
- java - 在应用程序运行后,如何检查 application.properties 中某个属性的值以获取 springboot 应用程序?
- javascript - 将鼠标滚轮功能添加到灯箱
- android - 如何更新 Android R(Android 11)媒体商店中音频文件的元数据?
- excel - Excel 的小数位问题
- arrays - 显示从矢量化结构获得的矩阵
- docusignapi - DocuSign API 付款 - 将发票号码传递给 Authorize.Net
- swift - 使用 iPad 时如何避免转到 NavigationLink 目标?
- php - 使用 laravel 在 postman api 中转换数据