【问题标题】:Fuel Android - Make non-cached requestFuel Android - 发出非缓存请求
【发布时间】:2018-11-01 18:57:18
【问题描述】:

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

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

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

我读了这篇文章:fetch(), how do you make a non-cached request?,看来我必须在我的请求中添加标题“pragma:no-cache”和“cache-control:no-cache”。我该怎么做?

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

虽然我的代码示例是在 Kotlin 中,但我可以用 Java 回答。

【问题讨论】:

    标签: android kotlin http-headers get-request


    【解决方案1】:

    这是添加标题的方式:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-28
      • 2016-12-26
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多