如何从 Kotlin 中的 Volley On Response 函数回调值
How to callback a value from Volley On Response function in Kotlin
我正在集成一个登录系统,我希望 Response.Listener
和 Response.ErrorListener
函数成为 return 布尔值。
我在 google 上搜索,发现可以使用回调接口 () 来完成。但我不知道如何在 Kotlin 中做到这一点
[已更新] 这是我的 Kotlin 文件,其中包含我的函数,许多活动都在使用它:
package com.pratham.example
// My required imports
import ...
fun function1(){
...
}
fun function2(){
...
}
// This Auth function will return True if login success
fun auth(activity:Activity):Boolean{
val queue = Volley.newRequestQueue(activity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonObj,
Response.Listener { response ->
val JSONObj = response.getString("Status")
if(JSONObj=="200"){
// return true
}
else{
// return false
}
}, Response.ErrorListener {
// return false
}
)
queue.add(req)
}
fun function4(){
...
}
此函数将 return 根据 Volley Response 判断为真或假。
我如何 return 来自 Kotlin
中的 Response.Listener
和 Response.ErrorListener
的值
Edit:: 我试过这样使用界面
package com.pratham.sitapuriya
import ...
fun function1(){
...
}
fun function2(){
...
}
interface MyInterface {
fun onCallback(response: Boolean,context:Context)
}
class Login : MyInterface {
private val myInterface = this
override fun onCallback(response: Boolean,context:Context) {
Toast.makeText(context,"Working",Toast.LENGTH_SHORT).show()
}
fun auth(activity: Activity): Boolean {
val sp1 = activity.getSharedPreferences("Login", AppCompatActivity.MODE_PRIVATE)
if (sp1.contains("token") && sp1.contains("deviceId")) {
val token = sp1.getString("token", null)
val deviceId = sp1.getString("deviceId", null)
if (!token.isNullOrEmpty() && !deviceId.isNullOrEmpty()) {
// val url = "http://10.0.2.2:80/Projects/Sitapuriya/login.php"
val url = activity.getString(R.string.server) + "/tokenAuth.php"
val params = HashMap<String, String>()
params["token"] = token
params["deviceId"] = deviceId
val jsonObj = JSONObject(params)
val queue = Volley.newRequestQueue(activity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonObj,
Response.Listener { response ->
val JSONObj = response.getString("Status")
if (JSONObj == "200") {
// return true
myInterface.onCallback(true,activity)
} else {
// return false
myInterface.onCallback(false,activity)
}
}, Response.ErrorListener {
// return false
myInterface.onCallback(false,activity)
}
)
queue.add(req)
} else {
return false
}
} else {
return false
}
return false
}
}
fun function4(){
...
}
但现在我不知道回调将如何 return 在 auth() 函数中赋值,也不知道我将如何在我的其他活动中调用此 auth() 函数。
请帮助我以前从未使用过回调..
你不能,你的代码是异步执行的,所以你不能在那里有 return 语句。
但这并不意味着您无法获得价值。最简单的选择是创建一个 LiveData<Boolean>
并在 activity 中观察它。当值更改时,您将收到通知。
接下来您可以创建一个界面。
interface MyCallback{
fun onValueChanged()
}
然后在您的 activity 中实现此接口并覆盖该方法。然后您可以使用它在异步调用完成时获得回调。请参阅下面的代码。
interface MyInterface{
fun onCallback(response:Boolean)
}
class LoginActivity : AppCompatActivity(), AuthListener, MyInterface {
val myInterface = this
override fun onCallback(response: Boolean) {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val queue = Volley.newRequestQueue(activity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonObj,
Response.Listener { response ->
val JSONObj = response.getString("Status")
if(JSONObj=="200"){
//return true
myInterface.onCallback(true)
}
else{
myInterface.onCallback(false)
}
}, Response.ErrorListener {
// return false
}
)
queue.add(req)
}
}
希望这对您有所帮助。谢谢 :)
我正在集成一个登录系统,我希望 Response.Listener
和 Response.ErrorListener
函数成为 return 布尔值。
我在 google 上搜索,发现可以使用回调接口 (
[已更新] 这是我的 Kotlin 文件,其中包含我的函数,许多活动都在使用它:
package com.pratham.example
// My required imports
import ...
fun function1(){
...
}
fun function2(){
...
}
// This Auth function will return True if login success
fun auth(activity:Activity):Boolean{
val queue = Volley.newRequestQueue(activity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonObj,
Response.Listener { response ->
val JSONObj = response.getString("Status")
if(JSONObj=="200"){
// return true
}
else{
// return false
}
}, Response.ErrorListener {
// return false
}
)
queue.add(req)
}
fun function4(){
...
}
此函数将 return 根据 Volley Response 判断为真或假。 我如何 return 来自 Kotlin
中的Response.Listener
和 Response.ErrorListener
的值
Edit:: 我试过这样使用界面
package com.pratham.sitapuriya
import ...
fun function1(){
...
}
fun function2(){
...
}
interface MyInterface {
fun onCallback(response: Boolean,context:Context)
}
class Login : MyInterface {
private val myInterface = this
override fun onCallback(response: Boolean,context:Context) {
Toast.makeText(context,"Working",Toast.LENGTH_SHORT).show()
}
fun auth(activity: Activity): Boolean {
val sp1 = activity.getSharedPreferences("Login", AppCompatActivity.MODE_PRIVATE)
if (sp1.contains("token") && sp1.contains("deviceId")) {
val token = sp1.getString("token", null)
val deviceId = sp1.getString("deviceId", null)
if (!token.isNullOrEmpty() && !deviceId.isNullOrEmpty()) {
// val url = "http://10.0.2.2:80/Projects/Sitapuriya/login.php"
val url = activity.getString(R.string.server) + "/tokenAuth.php"
val params = HashMap<String, String>()
params["token"] = token
params["deviceId"] = deviceId
val jsonObj = JSONObject(params)
val queue = Volley.newRequestQueue(activity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonObj,
Response.Listener { response ->
val JSONObj = response.getString("Status")
if (JSONObj == "200") {
// return true
myInterface.onCallback(true,activity)
} else {
// return false
myInterface.onCallback(false,activity)
}
}, Response.ErrorListener {
// return false
myInterface.onCallback(false,activity)
}
)
queue.add(req)
} else {
return false
}
} else {
return false
}
return false
}
}
fun function4(){
...
}
但现在我不知道回调将如何 return 在 auth() 函数中赋值,也不知道我将如何在我的其他活动中调用此 auth() 函数。
请帮助我以前从未使用过回调..
你不能,你的代码是异步执行的,所以你不能在那里有 return 语句。
但这并不意味着您无法获得价值。最简单的选择是创建一个 LiveData<Boolean>
并在 activity 中观察它。当值更改时,您将收到通知。
接下来您可以创建一个界面。
interface MyCallback{
fun onValueChanged()
}
然后在您的 activity 中实现此接口并覆盖该方法。然后您可以使用它在异步调用完成时获得回调。请参阅下面的代码。
interface MyInterface{
fun onCallback(response:Boolean)
}
class LoginActivity : AppCompatActivity(), AuthListener, MyInterface {
val myInterface = this
override fun onCallback(response: Boolean) {
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val queue = Volley.newRequestQueue(activity)
val req = JsonObjectRequest(Request.Method.POST, url, jsonObj,
Response.Listener { response ->
val JSONObj = response.getString("Status")
if(JSONObj=="200"){
//return true
myInterface.onCallback(true)
}
else{
myInterface.onCallback(false)
}
}, Response.ErrorListener {
// return false
}
)
queue.add(req)
}
}
希望这对您有所帮助。谢谢 :)