创建一个活动登录

我正在使用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一种吐司,这将说成功的连接。 我尝试,但我不知道如何修改我的代码。 非常感谢您的建议!

我不知道kotlin但万一你可以在这里做出来是做的吐司java代码。

 Toast toast = Toast.makeText(getApplicationContext(), "successful connection", Toast.LENGTH_SHORT).show; 

您将需要导入android.widget.Toast欲了解更多详情和培训,请访问官方网站。

编辑好我已经说过我不知道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。

在你的情况下,要在Kotlin语法中做一个Toast

 Toast.makeText(this@MainActivity,obj.getString("message"),Toast.LENGTH_SHORT).show() 

用你现有的Toast线代替这条线