2017-06-26 76 views
1

我正在使用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一種吐司,這將說成功的連接。我嘗試但我不知道如何修改我的代碼。非常感謝您的建議!

回答

1

我不知道kotlin但只是在這種情況下,你可以在這裏做出的是java代表製作吐司。

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

您需要導入android.widget.Toast 有關詳細信息和培訓參觀offcial網站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。

+0

由於這條線,但在那裏我可以有我的代碼添加呢? –

+0

已編輯。也許這可能有幫助。 – Rishav

0

在你的情況要進行ToastKotlin語法是:

Toast.makeText([email protected],obj.getString("message"),Toast.LENGTH_SHORT).show() 

替換現有的吐司線