com.lzy.okgo.cache.CacheEntity Java Examples

The following examples show how to use com.lzy.okgo.cache.CacheEntity. 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: NoCachePolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
    public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
        mCallback = callback;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                callbackOnStart(request);
//                if (mCallback != null) {
//                    mCallback.onStart(request);
//                }
                try {
                    prepareRawCall();
                } catch (Throwable throwable) {
                    Response<T> error = Response.error(false, rawCall, null, throwable);
                    callbackOnError(error);
//                    mCallback.onError(error);
                    return;
                }
                requestNetworkAsync();
            }
        });
    }
 
Example #2
Source File: NoneCacheRequestPolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(final CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            if (cacheEntity != null) {
                Response<T> success = Response.success(true, cacheEntity.getData(), rawCall, null);
                mCallback.onCacheSuccess(success);
                mCallback.onFinish();
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example #3
Source File: NoneCacheRequestPolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = null;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    if (response == null) {
        response = requestNetworkSync();
    }
    return response;
}
 
Example #4
Source File: FirstCacheRequestPolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(final CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            if (cacheEntity != null) {
                Response<T> success = Response.success(true, cacheEntity.getData(), rawCall, null);
                mCallback.onCacheSuccess(success);
            }
            requestNetworkAsync();
        }
    });
}
 
Example #5
Source File: FirstCacheRequestPolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    //同步请求,不能返回两次,只返回正确的数据
    Response<T> response;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example #6
Source File: DefaultCachePolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example #7
Source File: DefaultCachePolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    //HTTP cache protocol
    if (response.isSuccessful() && response.code() == 304) {
        if (cacheEntity == null) {
            response = Response.error(true, rawCall, response.getRawResponse(), CacheException.NON_AND_304(request.getCacheKey()));
        } else {
            response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
        }
    }
    return response;
}
 
Example #8
Source File: NoCachePolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example #9
Source File: RequestFailedCachePolicy.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
@Override
public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
    mCallback = callback;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mCallback.onStart(request);

            try {
                prepareRawCall();
            } catch (Throwable throwable) {
                Response<T> error = Response.error(false, rawCall, null, throwable);
                mCallback.onError(error);
                return;
            }
            requestNetworkAsync();
        }
    });
}
 
Example #10
Source File: RequestFailedCachePolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
    public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
        mCallback = callback;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                callbackOnStart(request);
//                mCallback.onStart(request);

                try {
                    prepareRawCall();
                } catch (Throwable throwable) {
                    Response<T> error = Response.error(false, rawCall, null, throwable);
                    callbackOnError(error);
//                    mCallback.onError(error);
                    return;
                }
                requestNetworkAsync();
            }
        });
    }
 
Example #11
Source File: OkGo.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
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.CONFIG);
    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();
}
 
Example #12
Source File: DefaultCachePolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    //HTTP cache protocol
    if (response.isSuccessful() && response.code() == 304) {
        if (cacheEntity == null) {
            response = Response.error(true, rawCall, response.getRawResponse(), CacheException.NON_AND_304(request.getCacheKey()));
        } else {
            response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
        }
    }
    return response;
}
 
Example #13
Source File: DefaultCachePolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
    public void requestAsync(CacheEntity<T> cacheEntity, Callback<T> callback) {
        mCallback = callback;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                callbackOnStart(request);
//                mCallback.onStart(request);

                try {
                    prepareRawCall();
                } catch (Throwable throwable) {
                    Response<T> error = Response.error(false, rawCall, null, throwable);
                    callbackOnError(error);
//                    mCallback.onError(error);
                    return;
                }
                requestNetworkAsync();
            }
        });
    }
 
Example #14
Source File: FirstCacheRequestPolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    //同步请求,不能返回两次,只返回正确的数据
    Response<T> response;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example #15
Source File: FirstCacheRequestPolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
    public void requestAsync(final CacheEntity<T> cacheEntity, Callback<T> callback) {
        mCallback = callback;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                callbackOnStart(request);
//                mCallback.onStart(request);

                try {
                    prepareRawCall();
                } catch (Throwable throwable) {
                    Response<T> error = Response.error(false, rawCall, null, throwable);
                    callbackOnError(error);
//                    mCallback.onError(error);
                    return;
                }
                if (cacheEntity != null) {
                    Response<T> success = Response.success(true, cacheEntity.getData(), rawCall, null);
                    callbackOnCacheSuccess(success);
//                    mCallback.onCacheSuccess(success);
                }
                requestNetworkAsync();
            }
        });
    }
 
Example #16
Source File: NoneCacheRequestPolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = null;
    if (cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, null);
    }
    if (response == null) {
        response = requestNetworkSync();
    }
    return response;
}
 
Example #17
Source File: OkGo.java    From okhttp-OkGo with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #18
Source File: BaseCachePolicy.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 请求成功后根据缓存模式,更新缓存数据
 *
 * @param headers 响应头
 * @param data    响应数据
 */
private void saveCache(Headers headers, T data) {
    if (data == null) {
        return;
    }
    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);
    }
}
 
Example #19
Source File: CacheCall.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
@Override
    public void execute(Callback<T> callback) {
//        HttpUtils.checkNotNull(callback, "callback == null");//removed by fee: 允许callback为null

        CacheEntity<T> cacheEntity = policy.prepareCache();
        policy.requestAsync(cacheEntity, callback);
    }
 
Example #20
Source File: CacheCall.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Callback<T> callback) {
    HttpUtils.checkNotNull(callback, "callback == null");

    CacheEntity<T> cacheEntity = policy.prepareCache();
    policy.requestAsync(cacheEntity, callback);
}
 
Example #21
Source File: CacheActivity.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@OnClick(R.id.getAll)
public void getAll(View view) {
    // 获取所有的缓存,但是一般每个缓存的泛型都不一样,所以缓存的泛型使用 ?
    List<CacheEntity<?>> all = CacheManager.getInstance().getAll();
    StringBuilder sb = new StringBuilder();
    sb.append("共" + all.size() + "条缓存:").append("\n\n");
    for (int i = 0; i < all.size(); i++) {
        CacheEntity<?> cacheEntity = all.get(i);
        sb.append("第" + (i + 1) + "条缓存:").append("\n").append(cacheEntity).append("\n\n");
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("所有缓存显示").setMessage(sb.toString()).show();
}
 
Example #22
Source File: MyApplication.java    From GoogleVR with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: RequestFailedCachePolicy.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example #24
Source File: NoCachePolicy.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    return requestNetworkSync();
}
 
Example #25
Source File: NoneCacheRequestPolicy.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
@Override
    public void requestAsync(final CacheEntity<T> cacheEntity, Callback<T> callback) {
        mCallback = callback;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                callbackOnStart(request);
//                mCallback.onStart(request);

                try {
                    prepareRawCall();
                } catch (Throwable throwable) {
                    Response<T> error = Response.error(false, rawCall, null, throwable);
                    callbackOnError(error);
//                    mCallback.onError(error);
                    return;
                }
                if (cacheEntity != null) {
                    Response<T> success = Response.success(true, cacheEntity.getData(), rawCall, null);
                    callbackOnCacheSuccess(success);
                    callbackOnFinish();
//                    mCallback.onCacheSuccess(success);
//                    mCallback.onFinish();
                    return;
                }
                requestNetworkAsync();
            }
        });
    }
 
Example #26
Source File: BaseCachePolicy.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
/**
 * 请求成功后根据缓存模式,更新缓存数据
 *
 * @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);
    }
}
 
Example #27
Source File: NoCachePolicy.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    return requestNetworkSync();
}
 
Example #28
Source File: RequestFailedCachePolicy.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
@Override
public Response<T> requestSync(CacheEntity<T> cacheEntity) {
    try {
        prepareRawCall();
    } catch (Throwable throwable) {
        return Response.error(false, rawCall, null, throwable);
    }
    Response<T> response = requestNetworkSync();
    if (!response.isSuccessful() && cacheEntity != null) {
        response = Response.success(true, cacheEntity.getData(), rawCall, response.getRawResponse());
    }
    return response;
}
 
Example #29
Source File: CacheManager.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
/** 返回带泛型的对象,注意必须确保泛型和对象对应才不会发生类型转换异常 */
@SuppressWarnings("unchecked")
public <T> CacheEntity<T> get(String key, Class<T> clazz) {
    return (CacheEntity<T>) get(key);
}
 
Example #30
Source File: CacheManager.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
/** 根据key获取缓存 */
public CacheEntity<?> get(String key) {
    if (key == null) return null;
    List<CacheEntity<?>> cacheEntities = query(CacheEntity.KEY + "=?", new String[]{key});
    return cacheEntities.size() > 0 ? cacheEntities.get(0) : null;
}