检查Volley是从缓存还是通过网络获得结果

时间:2021-10-19 03:49:47

How can I check whether Volley gets the results of a JsonObjectRequest from the cache or from the network?

如何检查Volley是从缓存还是从网络获取JsonObjectRequest的结果?

I need to show a progress dialog when it needs a network connection but not when the results are quickly received from the cache.

我需要在需要网络连接时显示进度对话框,但不能在从缓存中快速接收结果时显示。

my request looks something like this

我的请求看起来像这样

volleyQueue = Volley.newRequestQueue(this);
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>(){...stuff}, new Response.ErrorListener(){...errorstuff});
jr.setShouldCache(true);
volleyQueue.add(jr);

5 个解决方案

#1


11  

I did this by overriding Request#addMarker and checking for a "cache-hit" marker being added:

我这样做是通过覆盖Request#addMarker并检查是否添加了“cache-hit”标记:

public class MyRequest<T> extends Request<T> {

    protected boolean cacheHit;

    @Override
    public void addMarker(String tag) {
        super.addMarker(tag);
        cacheHit = false;
        if (tag.equals("cache-hit")){
            cacheHit = true;
        }
    }
}

#2


3  

Before making the Request you can get the cache from the Request Queue and check if the Entry is not null.

在发出请求之前,您可以从请求队列中获取缓存并检查Entry是否为空。

mRequestQueue.getCache().get("key");

The key for each request is usually the URL. I guess you should have to check if the Entry has expired too.

每个请求的密钥通常是URL。我想你应该检查条目是否已过期。

#3


2  

Volley has a built in way to know if image requests are immediate through the ImageContainer class, but it doesn't seem to have a similar mechanism for other requests such a JSON object request.

Volley有一种内置的方式来通过ImageContainer类来了解图像请求是否是立即的,但它似乎没有类似于其他请求的机制,例如JSON对象请求。

It seems that you have 2 main choices:

您似乎有两个主要选择:

  1. You can set a timer for something like 300ms after you request the JSON (test for the best time). When the timer is done, check to see if you have the result already, otherwise show the dialog. I know this is a bit of a "hack" but it could be good enough.
  2. 在请求JSON(测试最佳时间)后,您可以设置300ms等定时器。计时器完成后,检查是否已有结果,否则显示对话框。我知道这有点“黑客”,但它可能足够好。
  3. Edit the Volley code to add an "isImmediate" flag to every request. There are multiple ways to achieve this. I suggest starting at CacheDispatcher
  4. 编辑Volley代码,为每个请求添加“isImmediate”标志。有多种方法可以实现这一目标。我建议从CacheDispatcher开始

#4


1  

Starting from Tim Kelly's answer.

从蒂姆凯利的回答开始。

by the time you check "cacheHit", it'll be reverted to false and you'll not know that it's a cache hit because many other tags are received after "cacheHit" is received and before the "onResponse" is called.

当你检查“cacheHit”时,它将被恢复为false并且你不会知道它是一个缓存命中,因为在收到“cacheHit”之后和调用“onResponse”之前接收到许多其他标记。

So, add

所以,添加

if(tag.equals("network-http-complete")){
            cacheHit = false;
        }

and remove cacheHit = false;

并删除cacheHit = false;

#5


0  

adb shell setprop log.tag.Volley VERBOSE

adb shell setprop log.tag.Volley VERBOSE

Run this command in your terminal, you may need to set 'adb' in your path in order to use that command, it should be located in your sdk/platform-tools/ dir.

在终端中运行此命令,您可能需要在路径中设置'adb'才能使用该命令,它应该位于您的sdk / platform-tools / dir中。

This will provide much more detailed volley logs and will show something along the lines of an execution stack for a volley request which exhibits cache hits or misses.

这将提供更详细的排球日志,并将显示执行堆栈的行,用于显示高速缓存命中或未命中的排球请求。

#1


11  

I did this by overriding Request#addMarker and checking for a "cache-hit" marker being added:

我这样做是通过覆盖Request#addMarker并检查是否添加了“cache-hit”标记:

public class MyRequest<T> extends Request<T> {

    protected boolean cacheHit;

    @Override
    public void addMarker(String tag) {
        super.addMarker(tag);
        cacheHit = false;
        if (tag.equals("cache-hit")){
            cacheHit = true;
        }
    }
}

#2


3  

Before making the Request you can get the cache from the Request Queue and check if the Entry is not null.

在发出请求之前,您可以从请求队列中获取缓存并检查Entry是否为空。

mRequestQueue.getCache().get("key");

The key for each request is usually the URL. I guess you should have to check if the Entry has expired too.

每个请求的密钥通常是URL。我想你应该检查条目是否已过期。

#3


2  

Volley has a built in way to know if image requests are immediate through the ImageContainer class, but it doesn't seem to have a similar mechanism for other requests such a JSON object request.

Volley有一种内置的方式来通过ImageContainer类来了解图像请求是否是立即的,但它似乎没有类似于其他请求的机制,例如JSON对象请求。

It seems that you have 2 main choices:

您似乎有两个主要选择:

  1. You can set a timer for something like 300ms after you request the JSON (test for the best time). When the timer is done, check to see if you have the result already, otherwise show the dialog. I know this is a bit of a "hack" but it could be good enough.
  2. 在请求JSON(测试最佳时间)后,您可以设置300ms等定时器。计时器完成后,检查是否已有结果,否则显示对话框。我知道这有点“黑客”,但它可能足够好。
  3. Edit the Volley code to add an "isImmediate" flag to every request. There are multiple ways to achieve this. I suggest starting at CacheDispatcher
  4. 编辑Volley代码,为每个请求添加“isImmediate”标志。有多种方法可以实现这一目标。我建议从CacheDispatcher开始

#4


1  

Starting from Tim Kelly's answer.

从蒂姆凯利的回答开始。

by the time you check "cacheHit", it'll be reverted to false and you'll not know that it's a cache hit because many other tags are received after "cacheHit" is received and before the "onResponse" is called.

当你检查“cacheHit”时,它将被恢复为false并且你不会知道它是一个缓存命中,因为在收到“cacheHit”之后和调用“onResponse”之前接收到许多其他标记。

So, add

所以,添加

if(tag.equals("network-http-complete")){
            cacheHit = false;
        }

and remove cacheHit = false;

并删除cacheHit = false;

#5


0  

adb shell setprop log.tag.Volley VERBOSE

adb shell setprop log.tag.Volley VERBOSE

Run this command in your terminal, you may need to set 'adb' in your path in order to use that command, it should be located in your sdk/platform-tools/ dir.

在终端中运行此命令,您可能需要在路径中设置'adb'才能使用该命令,它应该位于您的sdk / platform-tools / dir中。

This will provide much more detailed volley logs and will show something along the lines of an execution stack for a volley request which exhibits cache hits or misses.

这将提供更详细的排球日志,并将显示执行堆栈的行,用于显示高速缓存命中或未命中的排球请求。