简体   繁体   中英

How to show a progress during fetching JSON-data

I would like to fetch some data from my web server. The data is a JSON object. I fetch the data with the fuel framework. I want to fill a recyclerview with the data. But I want to show a progress until the data is fetched.

But I have no idea how to solve this with fuel.

I have studied the fuel documentation. But I cannot find a solution

The code fetches some JSON data

fun fetchMyThings(): List<Thing> {
    val username = "xxxx"
    val password = "xxxxxxxxx"
    val things = mutableListOf<Thing>()
    Fuel.get("https://www.thingurl.com/things")
            .authentication()
            .basic(username, password)
            .responseJson { request, response, result ->
                request.responseProgress()
                val arr: JSONArray = result.get().array()

                println(arr.length())
                for (i in 0 until arr.length()) {
                    var elem: JSONObject = arr[i] as JSONObject
                    var obj: JSONObject = elem
                    var thing = Thing(UUID.fromString(obj.getString("uuid")))
                    thing.summary = obj.getString("summary")
                    things += thing
                    println(thing)
                }

            }
    return things
}

This code fills the recyclerview

private fun fillRecyclerView(things : List<Thing>) {
    val recyclerView = findViewById<RecyclerView>(R.id.main_recycler)
    val mainActivityRecyclerAdapter = MainActivityAdapter(this, things)
    recyclerView.adapter = mainActivityRecyclerAdapter
    recyclerView.layoutManager = LinearLayoutManager(this)
}

Expectation: Progresshandling with Fuel

Many thanks in advance for any help

Next try...

fun getThings(): Single<Result<String, FuelError>> {
    val username = "********"
    val password = "********"

    val http = "https://www.nowhere.com/thing/"
            .httpGet()
            .authentication()
            .basic(username, password)
            .rxString(Charsets.UTF_8)
    return http
}
class MainActivity : AppCompatActivity() {
    var things: List<Thing> = emptyList()
    protected lateinit var adapter: MainActivityAdapter


    override fun onCreate(savedInstanceState: Bundle?) {
        //....

        fillRecyclerView(things)

    Server.getThings().subscribe { p ->
        println(p)
        val arr: JSONArray = JSONArray(p.component1())

        for (i in 0 until arr.length()) {
            var elem: JSONObject = arr[i] as JSONObject
            var obj: JSONObject = elem
            var thing = Thing(UUID.fromString(obj.getString("uuid")))
            thing.summary = obj.getString("summary")
            things += thing
            println(thing)
        }
        adapter?.notifyDataSetChanged()
        findViewById<ProgressBar>(R.id.ac_main_progress).visibility = View.GONE
    }

        private fun fillRecyclerView(things : List<Thing>) {
                val recyclerView = findViewById<RecyclerView>(R.id.main_recycler)
                val mainActivityRecyclerAdapter = MainActivityAdapter(this, things)
                adapter = mainActivityRecyclerAdapter
                recyclerView.adapter = mainActivityRecyclerAdapter
                recyclerView.layoutManager = LinearLayoutManager(this)

        }

    }
    ```

you can use fuel-rxjava ( https://github.com/kittinunf/fuel/tree/master/fuel-rxjava ) for showing progress. Register an observer to notify the progress bar using rx-java

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