返回值没有设置

时间:2018-01-19 16:48:40

标签: android kotlin return-value

我对Kotlin \ Android很新,所以这可能是一个明显的错误,但我很困难,需要一些帮助。

我正在做的是让用户输入他们的名字,取名字并检查mySQL数据库(这样可以正常工作),然后根据他们的授权等级返回0或1,这就是我的部分我遇到了问题。

我尝试过使用return,创建一个全局类,添加到一个可变列表并设置它但没有任何作用。

在Main我有这个。

        var useName:String = editText.text.toString()
        verifiedID = verifyUser(useName)   //Call the function here
  //    verifiedID = Global.validLogin   //Was used to test Global class

        ET_HelloW.setText("$useName  ID $verifiedID")

在验证用户类

var ucounter = 0
fun verifyUser(userName: String) :Int {
//getting the record values

    val userName = userName
    var uname: String = ""
    var uauth: String = ""

    //creating volley string request
   // Checking to see if the Username entered exists in the DBEd

   val stringRequest = object : StringRequest(Request.Method.POST, PHPPost.URL_CHECK_LOGIN,
        Response.Listener<String> { response ->
            try {
                val obj = JSONObject(response)
                if (!obj.getBoolean("error")){
                    val array = obj.getJSONArray("credens")
                    for (i in 0..array.length() - 1) {
                        val objectArtist = array.getJSONObject(i)
                        uname = objectUser.getString("name")
                        uauth = objectUser.getString("auth_level")
                    }
                    if (uauth.toInt()<2)
                    {
                        Global.validLogin = 0 //This sets
                        ucounter = 0   //This sets
                    }else{
                        Global.validLogin = 1 //This sets when checking code but is not picked up when called in Main
                        ucounter = 1   //Debugger shows that I do reach here and that the var is set, but resets to 0 at the point of returning.
                    }
                }else{
                }
            } catch (e: JSONException) {
                e.printStackTrace()
            }
        },
        object : Response.ErrorListener {
            override fun onErrorResponse(volleyError: VolleyError) {
            }
        }) {
    @Throws(AuthFailureError::class)
    override fun getParams(): Map<String, String> {
        val params = HashMap<String, String>()
        params.put("name", userName)

        return params
    }

}
//adding request to queue
VolleySingleton.instance?.addToRequestQueue(stringRequest)
return ucounter //always returns zero
//    return 1 // If set manually this will be returned.
}

我很确定该值在最终返回之前被重置为0,但即使我将其设置为全局也无效。

再次确定这是一个菜鸟错误,但我会感激一些帮助。

0 个答案:

没有答案