问题
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