我已經添加了針對獲取Json對象的請求,如果wifi已打開,則需要數據,但在離線模式下無法訪問,即使緩存啓用了請求。Android:Volley不能在離線狀態下使用緩存
我做下面的代碼
public class VolleySingleton extends Application
{
public static final String TAG = VolleySingleton.class.getSimpleName();
private RequestQueue mRequestQueue;
private static VolleySingleton mInstance;
private ImageLoader mImageLoader;
private final String DEFAULT_CACHE_DIR = "sl_cache";
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
@Override
public void onCreate()
{
super.onCreate();
mInstance = this;
}
public static synchronized VolleySingleton getInstance()
{
return mInstance;
}
public ImageLoader getImageLoader()
{
getRequestQueue();
if (mImageLoader == null)
{
mImageLoader = new ImageLoader(this.mRequestQueue, new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag)
{
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req)
{
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag)
{
if (mRequestQueue != null)
{
mRequestQueue.cancelAll(tag);
}
}
public RequestQueue getRequestQueue()
{
if (mRequestQueue == null)
{
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024 * 10); // 10MB cap
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
mRequestQueue.start();
}
return mRequestQueue;
}
}
private void getData(String url, String tag)
{
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
Log.wtf("HOME", response.toString());
String result = parseData(response.toString());
postProcessing(result);
//SocialLadder.getInstance().getRequestQueue().getCache().invalidate(url, true);
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
VolleyLog.wtf("HOME", "Error: " + error.getMessage());
stopRefresher();
}
})
{
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response)
{
try
{
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return !jsonString.isEmpty() ? Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)) : Response.success(new JSONObject(), HttpHeaderParser.parseCacheHeaders(response));
}
catch (JSONException ex)
{
ex.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return null;
}
};
jsonObjReq.setShouldCache(true);
VolleySingleton.getInstance().addToRequestQueue(jsonObjReq, tag);
}
請幫幫忙,我想我的緩存畫面數據。
編輯
緩存數據
private String getCache(String url)
{
String data = "";
Cache cache = VolleySingleton.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(url);
if (entry != null)
{
try
{
data = new String(entry.data, "UTF-8");
// handle data, like converting it to xml, json, bitmap etc.,
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
/*else
{
// Cached response doesn't exists. Make network call here
}*/
return data;
}
哪裏是您的離線查看代碼? – mmlooloo 2014-09-04 12:23:46
@mmlooloo我更新了代碼 – 2014-09-04 12:41:47