我找到了用于将Android App连接到SQL数据库以进行登录的基本教程,以便我可以理解基本原理。在这种情况下,我使用了WampServer和php文件来连接数据。
作为参考,以下是php文件:
conn.php:
<?php
$db_name = "schoolinfo";
$mysql_username = "root";
$mysql_password = "";
$server_name = "localhost";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
?>
login.php:
<?php
require "conn.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from studentinfo where ID like '$user_name' and password like '$user_pass';";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0) {
echo "login successful";
}
else {
echo "login failed";
}
?>
当我使用http://localhost/login.php
测试php文件并将其$user_name
和$user_pass
设置为静态字符串时,php文件可以独立工作。
但是,在测试来自Android应用程序项目的登录名时,我得到了一些奇怪的结果。当您尝试登录时,会弹出一个带有标题和消息的alertDialog。奇怪的是,只有标题弹出了-让我相信在应用程序和php文件之间它们出了问题。如何解决此错误的连接?
有关进一步的参考,请参见以下Java文件:
MainActivity:
public class MainActivity extends AppCompatActivity {
EditText UserEt, PasswordEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserEt = findViewById(R.id.editUser);
PasswordEt = findViewById(R.id.editPassword);
}
public void onLogin(View view) {
String username = UserEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);
}
}
BackgroundWorker.java:
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://ip_address/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();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
**编辑:** 我遍历了我的调用堆栈,发现有一个错误(通过try-catch语句捕获)说:不允许对ip_adress的明文HTTP通信。
当我在清单中切换android:usesCleartextTraffic = "true"
时,它起作用了。这是因为Android sdk 28发生了变化。完成项目后,我将切换到https。