本文整理汇总了Java中com.lzy.okgo.cache.CacheMode类的典型用法代码示例。如果您正苦于以下问题:Java CacheMode类的具体用法?Java CacheMode怎么用?Java CacheMode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CacheMode类属于com.lzy.okgo.cache包,在下文中一共展示了CacheMode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: OkGo
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
private OkGo() {
mDelivery = new Handler(Looper.getMainLooper());
mRetryCount = 3;
mCacheTime = CacheEntity.CACHE_NEVER_EXPIRE;
mCacheMode = CacheMode.NO_CACHE;
OkHttpClient.Builder builder = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
loggingInterceptor.setColorLevel(Level.INFO);
builder.addInterceptor(loggingInterceptor);
builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory();
builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);
builder.hostnameVerifier(HttpsUtils.UnSafeHostnameVerifier);
okHttpClient = builder.build();
}
示例2: onViewCreated
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
// super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager = getLayoutManager();
recyclerView.setLayoutManager(layoutManager);
OkGo.get(ApiUrls.URL_Query).cacheKey(ApiUrls.URL_Query).cacheMode(CacheMode.DEFAULT).execute(new StringCallback() {
@Override
public void onSuccess(String s, Call call, Response response) {
try {
JSONObject obj = new JSONObject(s);
String content = obj.getString("content");
List<VideoItem> videoItems = JSON.parseArray(content, VideoItem.class);
recyclerView.setAdapter(new VrVideoAdapter(videoItems));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
示例3: retrieveDrawerMenu
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
private void retrieveDrawerMenu() {
OkGo.get(WebUtils.getCategoriesURL())
.tag(this)
.cacheKey("cacheKey")
.cacheMode(CacheMode.DEFAULT)
.execute(new StringCallback() {
@Override
public void onSuccess(String s, Call call, Response response) {
categories = JSONParser.parseCategoryList(s);
// 获取导航栏内容后异步更新UI
if (categories != null) {
Message msg = new Message();
msg.what = UPDATE_DRAWER_MENU;
handler.sendMessage(msg);
}
}
});
}
示例4: sendSuccessResultCallback
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
/** 成功回调,发送到主线程 */
private void sendSuccessResultCallback(final boolean isFromCache, final T t, final okhttp3.Call call, final okhttp3.Response response) {
final CacheMode cacheMode = baseRequest.getCacheMode();
OkGo.getInstance().getDelivery().post(new Runnable() {
@Override
public void run() {
if (isFromCache) {
mCallback.onCacheSuccess(t, call); //缓存成功回调 (UI线程)
if (cacheMode == CacheMode.DEFAULT || cacheMode == CacheMode.REQUEST_FAILED_READ_CACHE || cacheMode == CacheMode.IF_NONE_CACHE_REQUEST) {
mCallback.onAfter(t, null); //请求结束回调 (UI线程)
}
} else {
mCallback.onSuccess(t, call, response); //请求成功回调 (UI线程)
mCallback.onAfter(t, null); //请求结束回调 (UI线程)
}
}
});
}
示例5: no_cache
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@OnClick(R.id.no_cache)
public void no_cache(View view) {
OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)//
.tag(this)//
.cacheMode(CacheMode.NO_CACHE)//
.cacheKey("no_cache") //对于无缓存模式,该参数无效
.cacheTime(5000) //对于无缓存模式,该时间无效
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new CacheCallBack(this));
}
示例6: cache_default
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@OnClick(R.id.cache_default)
public void cache_default(View view) {
OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)//
.tag(this)//
.cacheMode(CacheMode.DEFAULT)//
.cacheKey("cache_default")//
.cacheTime(5000)//对于默认的缓存模式,该时间无效,依靠的是服务端对304缓存的控制
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new CacheCallBack(this));
}
示例7: request_failed_read_cache
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@OnClick(R.id.request_failed_read_cache)
public void request_failed_read_cache(View view) {
OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)//
.tag(this)//
.cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)//
.cacheKey("request_failed_read_cache")//
.cacheTime(5000) // 单位毫秒.5秒后过期
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new CacheCallBack(this));
}
示例8: if_none_cache_request
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@OnClick(R.id.if_none_cache_request)
public void if_none_cache_request(View view) {
OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)//
.tag(this)//
.cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)//
.cacheKey("if_none_cache_request")//
.cacheTime(5000) // 单位毫秒.5秒后过期
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new CacheCallBack(this));
}
示例9: first_cache_then_request
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@OnClick(R.id.first_cache_then_request)
public void first_cache_then_request(View view) {
OkGo.<LzyResponse<ServerModel>>get(Urls.URL_CACHE)//
.tag(this)//
.cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)//
.cacheKey("first_cache_then_request")//
.cacheTime(5000) // 单位毫秒.5秒后过期
.headers("header1", "headerValue1")//
.params("param1", "paramValue1")//
.execute(new CacheCallBack(this));
}
示例10: saveCache
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
/**
* 请求成功后根据缓存模式,更新缓存数据
*
* @param headers 响应头
* @param data 响应数据
*/
private void saveCache(Headers headers, T data) {
if (request.getCacheMode() == CacheMode.NO_CACHE) return; //不需要缓存,直接返回
if (data instanceof Bitmap) return; //Bitmap没有实现Serializable,不能缓存
CacheEntity<T> cache = HeaderParser.createCacheEntity(headers, data, request.getCacheMode(), request.getCacheKey());
if (cache == null) {
//服务器不需要缓存,移除本地缓存
CacheManager.getInstance().remove(request.getCacheKey());
} else {
//缓存命中,更新缓存
CacheManager.getInstance().replace(request.getCacheKey(), cache);
}
}
示例11: onCreate
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
//全局初始化
OkGo.init(this);
OkGo.getInstance().setConnectTimeout(3000)
.setReadTimeOut(3000)
.setWriteTimeOut(3000)
.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
.setRetryCount(3);
}
示例12: setHeaderView
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
private void setHeaderView() {
View headerView = LayoutInflater.from(getContext())
.inflate(R.layout.news_banner_item_layout, null);
mFlyBanner = (FlyBanner) headerView.findViewById(R.id.fly_banner);
OkGo.get(bannerUrl)
.tag(this)
.cacheMode(CacheMode.DEFAULT)
.execute(new StringCallback() {
@Override
public void onSuccess(String s, Call call, Response response) {
Type type = new TypeToken<NewsBannerBean>() {
}.getType();
NewsBannerBean bannerBean = new Gson().fromJson(s, type);
if (bannerBean.getCount() > 0) {
List<String> imgBanner = new ArrayList<>();
for (NewsBannerBean.DatasEntity entity : bannerBean.getDatas()) {
imgBanner.add(entity.getHeadPicUrl());
}
mFlyBanner.setImagesUrl(imgBanner);
mFlyBanner.setOnItemClickListener(position -> {
String id = bannerBean.getDatas().get(position).getId();
Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
intent.putExtra("id", id);
intent.putExtra(Contants.TITLE, title);
startActivity(intent);
});
}
}
});
mNewsAdapter.setHeaderView(headerView);
}
示例13: handleCache
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
/**
* 请求成功后根据缓存模式,更新缓存数据
*
* @param headers 响应头
* @param data 响应数据
*/
@SuppressWarnings("unchecked")
private void handleCache(Headers headers, T data) {
if (baseRequest.getCacheMode() == CacheMode.NO_CACHE) return; //不需要缓存,直接返回
if (data instanceof Bitmap) return; //Bitmap没有实现Serializable,不能缓存
CacheEntity<T> cache = HeaderParser.createCacheEntity(headers, data, baseRequest.getCacheMode(), baseRequest.getCacheKey());
if (cache == null) {
//服务器不需要缓存,移除本地缓存
CacheManager.INSTANCE.remove(baseRequest.getCacheKey());
} else {
//缓存命中,更新缓存
CacheManager.INSTANCE.replace(baseRequest.getCacheKey(), (CacheEntity<Object>) cache);
}
}
示例14: sendFailResultCallback
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
/** 失败回调,发送到主线程 */
@SuppressWarnings("unchecked")
private void sendFailResultCallback(final boolean isFromCache, final okhttp3.Call call, final okhttp3.Response response, final Exception e) {
final CacheMode cacheMode = baseRequest.getCacheMode();
OkGo.getInstance().getDelivery().post(new Runnable() {
@Override
public void run() {
if (isFromCache) {
mCallback.onCacheError(call, e); //缓存失败回调 (UI线程)
if (cacheMode == CacheMode.DEFAULT || cacheMode == CacheMode.REQUEST_FAILED_READ_CACHE) {
mCallback.onAfter(null, e); //请求结束回调 (UI线程)
}
} else {
mCallback.onError(call, response, e); //请求失败回调 (UI线程)
if (cacheMode != CacheMode.REQUEST_FAILED_READ_CACHE) {
mCallback.onAfter(null, e); //请求结束回调 (UI线程)
}
}
}
});
//不同的缓存模式,可能会导致该失败进入两次,一次缓存失败,一次网络请求失败
if (!isFromCache && cacheMode == CacheMode.REQUEST_FAILED_READ_CACHE) {
if (cacheEntity != null && !cacheEntity.isExpire()) {
T data = cacheEntity.getData();
HttpHeaders headers = cacheEntity.getResponseHeaders();
if (data == null || headers == null) {
//由于没有序列化等原因,可能导致数据为空
sendFailResultCallback(true, call, response, OkGoException.INSTANCE("没有获取到缓存,或者缓存已经过期!"));
} else {
sendSuccessResultCallback(true, data, call, response);
}
} else {
sendFailResultCallback(true, call, response, OkGoException.INSTANCE("没有获取到缓存,或者缓存已经过期!"));
}
}
}
示例15: cache
import com.lzy.okgo.cache.CacheMode; //导入依赖的package包/类
@OnClick(R.id.cache)
public void cache(View view) {
// 详细看文档: https://github.com/jeasonlzy/okhttp-OkGo/wiki/OkRx
OkGo.<String>post(Urls.URL_METHOD)//
.headers("aaa", "111")//
.params("bbb", "222")//
.cacheKey("rx_cache") //这里完全同okgo的配置一样
.cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST) //这里完全同okgo的配置一样
.converter(new StringConvert())//
.adapt(new ObservableResponse<String>())//
.subscribeOn(Schedulers.io())//
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
showLoading();
}
})//
.observeOn(AndroidSchedulers.mainThread())//
.subscribe(new Observer<Response<String>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
addDisposable(d);
}
@Override
public void onNext(@NonNull Response<String> response) {
handleResponse(response);
}
@Override
public void onError(@NonNull Throwable e) {
e.printStackTrace();
showToast("请求失败");
handleError(null);
}
@Override
public void onComplete() {
dismissLoading();
}
});
}