private void loadDataByNet() { BmobQuery<Content> query = new BmobQuery<>(); query.getObject( DetailActivity.this, mContent.getObjectId(), new GetListener<Content>() { @Override public void onSuccess(Content content) { setData(content); // 把数据缓存到本地 SaveCacheAsyncTask savecaheTask = new SaveCacheAsyncTask( DetailActivity.this, content, CacheHelper.CONTENT_CACHE_KEY + mContent.getObjectId()); savecaheTask.execute(); mLoadingLayout.setLoadingLayout(LoadingLayout.HIDE_LAYOUT); mScrollViewEx.setVisibility(View.VISIBLE); } @Override public void onFailure(int i, String s) { if (mLoadingLayout.getState() == LoadingLayout.STATE_REFRESH) { mLoadingLayout.setLoadingLayout(LoadingLayout.NETWORK_ERROR); } } }); }
private void loadData() { // 是否有缓存 boolean hasCache = CacheHelper.isExistDataCache(this, CacheHelper.CONTENT_CACHE_KEY + mContent.getObjectId()); // 缓存是否过期 boolean isCacheOverTiem = CacheHelper.isCacheDataFailure( this, CacheHelper.CONTENT_CACHE_KEY + mContent.getObjectId()); // 是否开启缓存过期设置 boolean isOpenCacheOverTime = CacheHelper.isOpenCacheOverTime(); // 有网络并且有没缓存||有网络且有启动缓存过期设置且缓存过期 就请求网络数据 否则 读取缓存 if (TDevice.hasInternet() && (!hasCache || (isOpenCacheOverTime && isCacheOverTiem))) { // 从网络上读取数据 loadDataByNet(); Logger.e( "detail_page hasCache : " + hasCache + "\n" + "detail_page isCacheOverTiem : " + isCacheOverTiem + "\n" + "detail_page isOpenCacheOverTime : " + isOpenCacheOverTime + "\n" + "detail_page request net"); } else { // 用AsynTask读取缓存 readCache(); Logger.e( "detail_page hasCache : " + hasCache + "\n" + "detail_page isCacheOverTiem : " + isCacheOverTiem + "\n" + "detail_page isOpenCacheOverTime : " + isOpenCacheOverTime + "\n" + "detail_page readCache"); } }
private void readCache() { ReadCacheAsyncTask<Content> readAsyncTask = new ReadCacheAsyncTask<>(this); readAsyncTask.setOnReadCacheToDo( new ReadCacheAsyncTask.OnReadCacheToDo<Content>() { @Override public void preExecute() { mLoadingLayout.setLoadingLayout(LoadingLayout.NETWORK_LOADING); mScrollViewEx.setVisibility(View.GONE); } @Override public void postExectue(Content data) { if (data == null) { loadDataByNet(); } else { mLoadingLayout.setLoadingLayout(LoadingLayout.HIDE_LAYOUT); mScrollViewEx.setVisibility(View.VISIBLE); // 加载数据 setData(data); } } }); readAsyncTask.execute(CacheHelper.CONTENT_CACHE_KEY + mContent.getObjectId()); }