加油安卓 - 发出非缓存请求



在Android中,我使用Kotlin库Fuel来获取JSON文件。 现在,我的代码如下所示(url 是字符串类型的变量(:

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

但是,我想获取位于url的 JSON 文件的未缓存版本。

我读了这篇文章:fetch((,你如何发出非缓存请求? 似乎我必须在我的请求中添加标题"杂注:无缓存"和"缓存控制:无缓存"。我该怎么做?

另外 - 我有没有办法验证这两个标头是否作为我的请求的一部分发送,用于调试目的?

虽然我的代码示例是 Kotlin 中的,但我对 Java 中的答案很好。

这是您添加标头的方式:

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

您可以验证标头是否随请求一起发送,如下所示:

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)
}
}
}

最新更新