Java Code Examples for com.android.volley.Cache#get()

The following examples show how to use com.android.volley.Cache#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestTools.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
private void forceExpireCache(String url) {
    Cache cache = BaseApplication.getInstance().getRequestQueue().getCache();
    Cache.Entry entry = cache.get(url);
    if (entry != null && entry.data != null && entry.data.length > 0) {
        if (!entry.isExpired()) {
            setOldCachInvalidWhenHasNewDatas(url);
        }
    }
}
 
Example 2
Source File: RequestTools.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
/***
 * 发送网络请求
 *
 * @param url           网络接口地址
 * @param cached        是否缓存
 * @param requestMethod 请求方法 默认为Request.Method.POST
 * @param requestObject 请求参数对象
 */
public <T> void sendRequest(String url, boolean cached, long cachedTime, int requestMethod, Object requestObject, final TypeToken<BaseResponse<T>> token, String tag) {
    boolean hasNetwork = CommonUtils.checkNetworkEnable(BaseApplication.getInstance().getApplicationContext());
    long oldSoftExpire = 0;
    if (cached) {
        //需要缓存的,先从缓存中取
        Cache cache = BaseApplication.getInstance().getRequestQueue().getCache();
        Cache.Entry entry = cache.get(url);
        Log.d("", "wenjun request getCacheFromUrl entry = " + entry
                + ", url = " + url);
        if (entry != null) {
            try {
                oldSoftExpire = entry.softTtl;
                String data = new String(entry.data, "UTF-8");
                mListener.cacheData(token == null ? data : new Gson().fromJson(data, token.getType()), hasNetwork);
            } catch (Exception e) {
                ZLogUtils.logException(e);
                mListener.cacheDataError(true, hasNetwork);
            }
        } else {
            mListener.cacheDataError(false, hasNetwork);
        }
    } else if (!hasNetwork) {
        mListener.useCacheNotAndNoNetwork();
    }
    if (hasNetwork) {//如果有网络,再从网络上获取一遍
        getFromNetWork(url, cached, cachedTime, oldSoftExpire, requestMethod, requestObject, token, tag);
    }
}
 
Example 3
Source File: PhotosListTaskFragment.java    From soas with Apache License 2.0 5 votes vote down vote up
public void refresh() {
    mLastRequestedPhotos = null;
    mNextItemToLoad = 0;

    String url = getRequestUrl();

    Cache cache = SoasApplication.getRequestQueue(getActivity()).getCache();
    Cache.Entry entry = cache.get(url);
    if (entry != null) {
        // Invalidate cache and reload.
        SoasApplication.getRequestQueue(getActivity()).getCache().invalidate(url, true);
    }

    GsonGetRequest<Photos> mainRequest = new GsonGetRequest<Photos>
            (url, Photos.class,
                    new Response.Listener<Photos>() {
                        @Override
                        public void onResponse(Photos photos) {
                            populateResponse(Task.REFRESH, photos);
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            populateResponse(Task.REFRESH, error);
                        }
                    }
            );
    mainRequest.setShouldCache(true);
    SoasApplication.addToRequestQueue(getActivity(), mainRequest, TAG);
}
 
Example 4
Source File: PhotosListTaskFragment.java    From soas with Apache License 2.0 5 votes vote down vote up
public void requestNextPage() {
    // Create and execute the background task.
    String url = getRequestUrl();

    Cache cache = SoasApplication.getRequestQueue(getActivity()).getCache();
    Cache.Entry entry = cache.get(url);
    if (entry != null) {
        // Soft invalidate cache because we forced it earlier.
        // TODO :: Comment out if cache was based on server response cache headers.
        SoasApplication.getRequestQueue(getActivity()).getCache().invalidate(url, false);
    }

    GsonGetRequest<Photos> mainRequest = new GsonGetRequest<Photos>
            (url, Photos.class,
                    new Response.Listener<Photos>() {
                        @Override
                        public void onResponse(Photos photos) {
                            populateResponse(Task.REQUEST_NEXT, photos);
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            populateResponse(Task.REQUEST_NEXT, error);
                        }
                    }
            );
    mainRequest.setShouldCache(true);
    SoasApplication.addToRequestQueue(getActivity(), mainRequest, TAG);
}