Get Fuel result in uiThread

645 Views Asked by At

I use a library called "Fuel" to fetch data from the server, and here is my code:

button1.setOnClickListener {
            val jb = "{......}"

            Fuel.post("server_url")
                    .header(Headers.CONTENT_TYPE, "application/json")
                    .body(jb)
                    .response { request, response, result ->
                        Log.d("test", request.toString())
                        Log.d("test", response.toString())
                        val (bytes, error) = result
                        if (bytes != null) {
                            val e = JsonParser().parse(String(bytes))
                            val obj = e.asJsonObject
                            val rm = obj.get("rm").asString
                        }
                    }
        }

But here is the problem : how can I get the value of "rm"?

It is defined in the Fuel function and I can't get its value outside from Fuel...

Could someone, help me with how to get it?

1

There are 1 best solutions below

2
On BEST ANSWER

You can declare a var outside the response object and set the value

var globalRM : String? = null;
button1.setOnClickListener {
            val jb = "{......}"
            var rm : String? = null;
            Fuel.post("server_url")
                    .header(Headers.CONTENT_TYPE, "application/json")
                    .body(jb)
                    .response { request, response, result ->
                        Log.d("test", request.toString())
                        Log.d("test", response.toString())
                        val (bytes, error) = result
                        if (bytes != null) {
                            val e = JsonParser().parse(String(bytes))
                            val obj = e.asJsonObject
                            globalRM = obj.get("rm").asString
                            rm = obj.get("rm").asString
                        }
                    }
        }

You might also want to do something just after you get the values, in that case, create a method and use it:

onCreate(...){
button1.setOnClickListener {
            val jb = "{......}"
            var rm : String? = null;
            Fuel.post("server_url")
                    .header(Headers.CONTENT_TYPE, "application/json")
                    .body(jb)
                    .response { request, response, result ->
                        Log.d("test", request.toString())
                        Log.d("test", response.toString())
                        val (bytes, error) = result
                        if (bytes != null) {
                            val e = JsonParser().parse(String(bytes))
                            val obj = e.asJsonObject
                            globalRM = obj.get("rm").asString
                            rm = obj.get("rm").asString
                            onFuelFetched(rm)
                        }
                    }
        }
}

fun onFuelFetched(val rm:String?){
    // do anything you wish to do with this value here
}