Fuel Android - 制作非缓存请求 [英] Fuel Android - Make non-cached request

查看:458
本文介绍了Fuel Android - 制作非缓存请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,我使用 Fuel (一个Kotlin库)来获取JSON文件。
现在,我的代码看起来像这样(url是字符串类型的变量):

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

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

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

我读过这篇文章: fetch(),你如何制作非缓存请求?似乎我必须添加标题pragma:no -cache和cache-control:no-cache到我的请求。我该怎么做?

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?

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

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

推荐答案

这是你添加标题的方式:

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

这篇关于Fuel Android - 制作非缓存请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆