我使用 kotlin,我想在检查 url 响应后显示警报 我在 MainActivity.kt 中的代码:
package com.example.test1
import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.net.URL
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main);
val username = findViewById<EditText>(R.id.etUserName);
val password = findViewById<EditText>(R.id.etPassword);
val btn = findViewById<Button>(R.id.button);
var url = URL("https://www.sepehrfarm.ir/app/read.php?username=host&password=1111").readText();
if(url == "okay"){
btn.setOnClickListener {
val mAlertDialog = AlertDialog.Builder(this@MainActivity)
mAlertDialog.setTitle("Show Dialog");
mAlertDialog.setMessage("Login successful");
mAlertDialog.setIcon(R.mipmap.ic_launcher)
mAlertDialog.setPositiveButton("OK") { dialog: DialogInterface, id: Int ->
Toast.makeText(
this@MainActivity,
"OK",
Toast.LENGTH_SHORT
).show()
}
mAlertDialog.show()
}
}
else{
btn.setOnClickListener {
val mAlertDialog = AlertDialog.Builder(this@MainActivity)
mAlertDialog.setTitle("Show Dialog");
mAlertDialog.setMessage("Login failed");
mAlertDialog.setIcon(R.mipmap.ic_launcher)
mAlertDialog.setPositiveButton("OK") { dialog: DialogInterface, id:Int ->
Toast.makeText(
this@MainActivity,
"OK",
Toast.LENGTH_SHORT
).show()
}
mAlertDialog.show()
}
}
}
}
在程序中 var url = URL("https://www.sepehrfarm.ir/app/read.php?username=host&password=1111").readText() 期间,应用程序无法工作,当我评论此内容时使应用程序正常运行 为什么?