我正在使用Android创建一个Kotlin代码来创建一个检查用户名和密码的Activity。我正在使用凌空来做这件事。我在我的MainActivity中使用此功能来执行此操作:
private fun checking(){
val name: String = editText?.text.toString()
val password: String = editText2?.text.toString()
val stringRequest = object : StringRequest(Request.Method.POST, 192.168.1.50,
Response.Listener<String> {
response ->
try {
val obj = JSONObject(response)
Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()
} catch (e: JSONException) {
e.printStackTrace()
}
},
object : Response.ErrorListener {
override fun onErrorResponse(volleyError: VolleyError) {
Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show()
}
}) {
@Throws(AuthFailureError::class)
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("name", name)
params.put("password", password)
return params
}
}
VolleySingleton.instance?.addToRequestQueue(stringRequest)
}
这是我的PHP代码:
<?php
define('HOST', 'localhost');
define('USER', '***');
define('PASS', '***');
define('DB', '***');
$con = mysqli_connect(HOST,USER,PASS,DB);
$name = $_POST['name'];
$password = $_POST['password'];
$sql = "select * from *** where name = '$name' and password = '$password';";
$res = mysqli_query($con,$sql);
$check = mysqli_fetch_array($res);
if(isset($check)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
?>
实际上我想要的只是如果我输入正确的名称和密码,那么我将进入Php echo 'success';
我希望在我的函数checking
中有一种会说成功的Toast连接。我尝试但我不知道如何修改我的代码。非常感谢您的建议!
答案 0 :(得分:0)
我不知道kotlin
但是如果你能在这里做出来的话就是制作祝酒词的java
代码。
Toast toast = Toast.makeText(getApplicationContext(), "successful connection", Toast.LENGTH_SHORT).show;
您需要导入android.widget.Toast
有关更多详细信息和培训,请访问官方网站here。
修改强> 好吧,我已经说过我不知道kotlin但也许可以试试这段代码。
private fun checking(){
val name: String = editText?.text.toString()
val password: String = editText2?.text.toString()
val temp: String ="pass" //Creating a new string I dont know if it is dont this way
val stringRequest = object : StringRequest(Request.Method.POST, 192.168.1.50,
Response.Listener<String> {
response ->
try {
val obj = JSONObject(response)
Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_LONG).show()
} catch (e: JSONException) {
e.printStackTrace()
}
},
object : Response.ErrorListener {
override fun onErrorResponse(volleyError: VolleyError) {
Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show()
temp="fail" //Setting the temp string to fail. dont know if it dont this way either
}
}) {
@Throws(AuthFailureError::class)
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params.put("name", name)
params.put("password", password)
return params
}
}
if(temp.isEqual("pass")) // checkig if temp was changed or not
{
Toast toast = Toast.makeText(getApplicationContext(), "successful connection", Toast.LENGTH_SHORT).show;
}
VolleySingleton.instance?.addToRequestQueue(stringRequest)
}
请不要指出我的synatx是否错误。我不知道Kotlin的K.
答案 1 :(得分:0)
在您的情况下,要在Toast
语法中创建Kotlin
,语法为:
Toast.makeText(this@MainActivity,obj.getString("message"),Toast.LENGTH_SHORT).show()
将此行替换为现有的Toast Line