How can I save JsonObjectRequest elements in variables using Kotlin?

Solution for How can I save JsonObjectRequest elements in variables using Kotlin?
is Given Below:

I’m trying to take data from a mySQL database and my code take it correctly.
The problem is that I have the information in a JsonObjectRequest and out of it, I can’t use it. My idea was to use variables to save some of the information I need.
Something like this:

        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)

As I said the problem here is that emailGet and usernameGet (variables declared before this code bloc) store the values only inside the JsonObjectRequest, out of it the variables are empty.
Example:

        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()

Here the Toast will print on the screen only the emailGet content because it’s inside the JsonObjectRequest, the Toast that have to print the usernameGet value will not do it.

Looking for information I have found that this problem could be because this function is asynchronous and I found a possible solution in Java that I tried to translate to 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){
        }

I do not really understand this second code, but it still doesn’t working, the response variable is always empty and the program stays in an infinite loop inside that try.

If you want to use the emailGet and usernameGet variables, you should do it within the callback:

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)

If instead, you have a method method doSomething() that runs immediately after the response is received:

fun doSomething(email:String, username:String){
    // TODO do something with the variables here
}

You can replace the TODO comment in the first code snippet with doSomething(emailGet, usernameGet).


The 4th and 5th parameters for JsonObjectRequest are in fact objects of types Response.Listener and Response.ErrorListener. These two listeners are Single Abstract Method interfaces. If expanded it would look like this:

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()
        }
    }
)

The lambda syntax you were using was a short hand of SAM interfaces.

The simplest way is using @ttyip’s answer but you could also use live data and observing it! You’re calling an asynchronous method and there’s going to be some delay(network API calling and this delay depends on user’s internet connection etc) So First you’ll need to add jetPack’s lifeCycle components inside your project:

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"
}

After syncing your project, define global inside your activity/fragment:

val email: MutableLiveData<String> = MutableLiveData<String>()
val username: MutableLiveData<String> = MutableLiveData<String>()

And inside your response:

 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()
        }
    )

And somewhere inside your activity just observe your livedata:

email.observe(this, Observer { string ->
 // Do some work 
})
username.observe(this, Observer { string ->
// Do some work 
})