如何使用 Kotlin 将 JsonObjectRequest 元素保存在变量中?
How can I save JsonObjectRequest elements in variables using Kotlin?
我正在尝试从 mySQL 数据库中获取数据,而我的代码可以正确获取数据。
问题是我在 JsonObjectRequest 中有信息,但我不能使用它。我的想法是使用变量来保存一些我需要的信息。
像这样:
val queue=Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
正如我所说,这里的问题是 emailGet 和 usernameGet(在此代码块之前声明的变量)仅将值存储在 JsonObjectRequest 中,而在 JsonObjectRequest 之外的变量是空的。
示例:
val queue=Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
Toast.makeText(this, emailGet, Toast.LENGTH_LONG).show()
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
Toast.makeText(this, usernameGet, Toast.LENGTH_LONG).show()
这里 Toast 只会在屏幕上打印 emailGet 内容,因为它在 JsonObjectRequest 中,必须打印 usernameGet 值的 Toast 不会打印。
查找信息我发现这个问题可能是因为这个函数是异步的,我在 Java 中找到了一个可能的解决方案,我试图将其翻译成 Kotlin。
val queue=Volley.newRequestQueue(this)
val future : RequestFuture<JSONObject> = RequestFuture.newFuture()
val request = JsonObjectRequest(
Request.Method.GET, url, null, future, future)
queue.add(request)
try{
var response = future.get()
} catch (e : InterruptedException){
} catch (e : ExecutionException){
}
第二个代码我不是很懂,但还是不行,response 变量总是空的,程序在那个 try 里一直死循环。
如果你想使用 emailGet
和 usernameGet
变量,你应该在回调中使用:
val queue = Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
// TODO do something with the variables here
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
相反,如果您有一个在收到响应后立即运行的方法 doSomething()
:
fun doSomething(email:String, username:String){
// TODO do something with the variables here
}
您可以将第一个代码片段中的 TODO 注释替换为 doSomething(emailGet, usernameGet)
。
JsonObjectRequest
的第四个和第五个参数实际上是Response.Listener
和Response.ErrorListener
类型的对象。这两个监听器是Single Abstract Method interfaces。如果展开它看起来像这样:
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
object : Response.Listener {
override fun onResponse(response: JSONObject) {
emailGet = response.getString("email")
usernameGet = response.getString("name")
doSomething(emailGet, usernameGet)
}
},
object : Response.ErrorListener {
override fun onErrorResponse(error: VolleyError) {
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
}
)
您使用的 lambda 语法是 SAM 接口的缩写。
最简单的方法是使用 @ttyip 的答案,但您也可以使用实时数据并观察它!你正在调用一个异步方法并且会有一些延迟(网络 API 调用,这个延迟取决于用户的互联网连接等)所以首先你需要在你的项目中添加 jetPack 的 lifeCycle 组件:
dependencies {
def lifecycle_version = "2.4.0-alpha02"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
}
同步项目后,在 activity/fragment:
中定义全局
val email: MutableLiveData<String> = MutableLiveData<String>()
val username: MutableLiveData<String> = MutableLiveData<String>()
在您的回复中:
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
val emailGet = response.getString("email")
val usernameGet = response.getString("name")
email.postValue(emailGet)
username.postValue(usernameGet)
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
在你的 activity 内部某处观察你的实时数据:
email.observe(this, Observer { string ->
// Do some work
})
username.observe(this, Observer { string ->
// Do some work
})
我正在尝试从 mySQL 数据库中获取数据,而我的代码可以正确获取数据。 问题是我在 JsonObjectRequest 中有信息,但我不能使用它。我的想法是使用变量来保存一些我需要的信息。 像这样:
val queue=Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
正如我所说,这里的问题是 emailGet 和 usernameGet(在此代码块之前声明的变量)仅将值存储在 JsonObjectRequest 中,而在 JsonObjectRequest 之外的变量是空的。 示例:
val queue=Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
Toast.makeText(this, emailGet, Toast.LENGTH_LONG).show()
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
Toast.makeText(this, usernameGet, Toast.LENGTH_LONG).show()
这里 Toast 只会在屏幕上打印 emailGet 内容,因为它在 JsonObjectRequest 中,必须打印 usernameGet 值的 Toast 不会打印。
查找信息我发现这个问题可能是因为这个函数是异步的,我在 Java 中找到了一个可能的解决方案,我试图将其翻译成 Kotlin。
val queue=Volley.newRequestQueue(this)
val future : RequestFuture<JSONObject> = RequestFuture.newFuture()
val request = JsonObjectRequest(
Request.Method.GET, url, null, future, future)
queue.add(request)
try{
var response = future.get()
} catch (e : InterruptedException){
} catch (e : ExecutionException){
}
第二个代码我不是很懂,但还是不行,response 变量总是空的,程序在那个 try 里一直死循环。
如果你想使用 emailGet
和 usernameGet
变量,你应该在回调中使用:
val queue = Volley.newRequestQueue(this)
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
{ response ->
emailGet = response.getString("email")
usernameGet = response.getString("name")
// TODO do something with the variables here
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(jsonObjectRequest)
相反,如果您有一个在收到响应后立即运行的方法 doSomething()
:
fun doSomething(email:String, username:String){
// TODO do something with the variables here
}
您可以将第一个代码片段中的 TODO 注释替换为 doSomething(emailGet, usernameGet)
。
JsonObjectRequest
的第四个和第五个参数实际上是Response.Listener
和Response.ErrorListener
类型的对象。这两个监听器是Single Abstract Method interfaces。如果展开它看起来像这样:
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
object : Response.Listener {
override fun onResponse(response: JSONObject) {
emailGet = response.getString("email")
usernameGet = response.getString("name")
doSomething(emailGet, usernameGet)
}
},
object : Response.ErrorListener {
override fun onErrorResponse(error: VolleyError) {
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
}
)
您使用的 lambda 语法是 SAM 接口的缩写。
最简单的方法是使用 @ttyip 的答案,但您也可以使用实时数据并观察它!你正在调用一个异步方法并且会有一些延迟(网络 API 调用,这个延迟取决于用户的互联网连接等)所以首先你需要在你的项目中添加 jetPack 的 lifeCycle 组件:
dependencies {
def lifecycle_version = "2.4.0-alpha02"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
}
同步项目后,在 activity/fragment:
中定义全局val email: MutableLiveData<String> = MutableLiveData<String>()
val username: MutableLiveData<String> = MutableLiveData<String>()
在您的回复中:
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,url,null,
{ response ->
val emailGet = response.getString("email")
val usernameGet = response.getString("name")
email.postValue(emailGet)
username.postValue(usernameGet)
}, { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}
)
在你的 activity 内部某处观察你的实时数据:
email.observe(this, Observer { string ->
// Do some work
})
username.observe(this, Observer { string ->
// Do some work
})