Fuel Android - Make non-cached request

可紊 提交于 2019-12-25 01:00:31

问题


In Android, I am using Fuel, a Kotlin library, to fetch a JSON file. Right now, my code looks like this (url is a variable of type string):

 url.httpGet().responseJson { _, _, result ->
            when(result) {
                is Result.Failure -> {
                    //Do Stuff
                }
                is Result.Success -> {
                    //Do Stuff
                }
            }
        }

However, I'd like to fetch an uncached version of the JSON file located at the url.

I read this post: fetch(), how do you make a non-cached request? and it seems like I have to add the headers "pragma: no-cache" and "cache-control: no-cache" to my request. How can I do that?

Also - is there a way for me to verify that those two headers are being sent as part of my request, for debugging purposes?

While my code sample is in Kotlin, I'm fine with answers in Java.


回答1:


This is how you add the headers:

url.httpGet().header(Pair("pragma","no-cache"),Pair("cache-control","no-cache")).responseJson //Rest of code goes here

You can verify the headers are being sent with the request like so:

url.httpGet().header(Pair("pragma","no-cache"),Pair("cache-control","no-cache")).responseJson { request, _, result ->
            //Log the request in string format. This will list the headers.
            Log.d("TEST-APP", request.toString())

            when(result) {
                is Result.Failure -> {
                    cont.resumeWithException(result.getException())
                }
                is Result.Success -> {
                    cont.resume(JsonParser().parse(result.value.content) as JsonObject)
                }
            }
        }


来源:https://stackoverflow.com/questions/50477300/fuel-android-make-non-cached-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!