2015-08-28 22 views
1

我想給我的應用程序提供離線支持,並且我使用排球庫來解析來自jsonobject的數據。我瞭解到凌空本身有一個很好的緩存機制。我已經實現此代碼以從緩存中獲取數據排球緩存總是在android中返回null

Cache cache = GlobalData.getInstance().getRequestQueue().getCache(); 
Cache.Entry entry = cache.get(Constants.URL_WEATHER_REQUEST + city); 

但該條目始終返回空值。我聽說過的服務器站點的標題,我已經使用Mozilla的RESTClient實現addonn看起來像這樣測試它:

header from the server

我不知道問題出在哪裏,但我總是得到緩存條目空,如果我像凌亂一樣對凌亂的網絡進行分析。我現在應該怎麼做?

+1

請閱讀更多在[Android設定排球從緩存使用(http://stackoverflow.com/questions/31897189/android-setup-volley-to-use-from-cache/32022946#32022946) – BNK

+0

我會的,但你能解釋這裏的錯誤嗎? – Reyjohn

+0

也許你的服務器應用程序不允許緩存。你可以試試'Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);'parseNetworkResponse'內部 – BNK

回答

1

Volley不必只使用url作爲緩存鍵。

請致電Request.getCacheKey()獲取緩存密鑰。

/** 
* Returns the cache key for this request. By default, this is the URL. 
*/ 
public String getCacheKey() { 
    return mMethod + ":" + mUrl; 
} 

/** 
* Supported request methods. 
*/ 
public interface Method { 
    int DEPRECATED_GET_OR_POST = -1; 
    int GET = 0; 
    int POST = 1; 
    int PUT = 2; 
    int DELETE = 3; 
} 

The issue on Github.

相關問題