简体   繁体   中英

Get Fuel result in uiThread

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM