Search in sources :

Example 1 with RxCache

use of io.rx_cache2.internal.RxCache in project MVPArms by JessYanCoding.

the class ClientModule method provideRxCache.

/**
 * 提供 {@link RxCache}
 *
 * @param application    {@link Application}
 * @param configuration  {@link RxCacheConfiguration}
 * @param cacheDirectory RxCache 缓存路径
 * @param gson           {@link Gson}
 * @return {@link RxCache}
 */
@Singleton
@Provides
static RxCache provideRxCache(Application application, @Nullable RxCacheConfiguration configuration, @Named("RxCacheDirectory") File cacheDirectory, Gson gson) {
    RxCache.Builder builder = new RxCache.Builder();
    RxCache rxCache = null;
    if (configuration != null) {
        rxCache = configuration.configRxCache(application, builder);
    }
    if (rxCache != null) {
        return rxCache;
    }
    return builder.persistence(cacheDirectory, new GsonSpeaker(gson));
}
Also used : GsonSpeaker(io.victoralbertos.jolyglot.GsonSpeaker) RxCache(io.rx_cache2.internal.RxCache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 2 with RxCache

use of io.rx_cache2.internal.RxCache in project 91Pop by DanteAndroid.

the class ApiException method handleException.

public static ApiException handleException(Throwable e) {
    // 使用RxCache之后返回的是包裹的CompositeException,一般包含2个异常,rxcache异常和原本的异常
    Logger.t(TAG).d("开始解析错误------");
    if (e instanceof CompositeException) {
        CompositeException compositeException = (CompositeException) e;
        for (Throwable throwable : compositeException.getExceptions()) {
            if (!(throwable instanceof RxCacheException)) {
                e = throwable;
                Logger.t(TAG).d("其他异常:" + throwable.getMessage());
            } else {
                Logger.t(TAG).d("RxCache 异常");
            }
        }
    }
    ApiException ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ApiException(httpException, httpException.code());
        ex.message = httpException.getMessage();
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof JsonSerializer || e instanceof NotSerializableException || e instanceof ParseException) {
        ex = new ApiException(e, Error.PARSE_ERROR);
        ex.message = "数据解析错误";
        return ex;
    } else if (e instanceof ClassCastException) {
        ex = new ApiException(e, Error.CAST_ERROR);
        ex.message = "类型转换错误";
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ApiException(e, Error.NETWORD_ERROR);
        ex.message = "连接失败";
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ApiException(e, Error.SSL_ERROR);
        ex.message = "证书验证失败";
        return ex;
    } else if (e instanceof ConnectTimeoutException) {
        ex = new ApiException(e, Error.TIMEOUT_ERROR);
        ex.message = "网络连接超时";
        return ex;
    } else if (e instanceof java.net.SocketTimeoutException) {
        ex = new ApiException(e, Error.TIMEOUT_ERROR);
        ex.message = "网络连接超时";
        return ex;
    } else if (e instanceof UnknownHostException) {
        ex = new ApiException(e, Error.UNKNOWNHOST_ERROR);
        ex.message = "无法解析该域名";
        return ex;
    } else if (e instanceof NullPointerException) {
        ex = new ApiException(e, Error.NULLPOINTER_EXCEPTION);
        ex.message = "NullPointerException";
        return ex;
    } else if (e instanceof VideoException) {
        ex = new ApiException(e, Error.PARSE_VIDEO_URL_ERROR);
        ex.message = e.getMessage();
        return ex;
    } else if (e instanceof FavoriteException) {
        ex = new ApiException(e, Error.FAVORITE_VIDEO_ERROR);
        ex.message = e.getMessage();
        return ex;
    } else if (e instanceof DaoException) {
        ex = new ApiException(e, Error.GREEN_DAO_ERROR);
        ex.message = "数据库错误";
        return ex;
    } else if (e instanceof MessageException) {
        ex = new ApiException(e, Error.COMMON_MESSAGE_ERROR);
        ex.message = e.getMessage();
        return ex;
    } else {
        ex = new ApiException(e, Error.UNKNOWN);
        ex.message = "未知错误:" + e.getMessage();
        return ex;
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) CompositeException(io.reactivex.exceptions.CompositeException) RxCacheException(io.rx_cache2.RxCacheException) JSONException(org.json.JSONException) JsonSerializer(com.google.gson.JsonSerializer) JsonParseException(com.google.gson.JsonParseException) DaoException(org.greenrobot.greendao.DaoException) NotSerializableException(java.io.NotSerializableException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) ConnectException(java.net.ConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 3 with RxCache

use of io.rx_cache2.internal.RxCache in project 91Pop by DanteAndroid.

the class AppApiHelper method loadPorn91VideoByCategory.

@Override
public Observable<BaseResult<List<UnLimit91PornItem>>> loadPorn91VideoByCategory(String category, String viewType, int page, String m, boolean cleanCache, boolean isLoadMoreCleanCache) {
    // RxCache条件区别
    String condition;
    if (TextUtils.isEmpty(m)) {
        condition = category;
    } else {
        condition = category + m;
    }
    DynamicKeyGroup dynamicKeyGroup = new DynamicKeyGroup(condition, page);
    EvictDynamicKey evictDynamicKey = new EvictDynamicKey(cleanCache || isLoadMoreCleanCache);
    Observable<String> categoryPage = noLimit91PornServiceApi.getCategoryPage(category, viewType, page, m, HeaderUtils.getIndexHeader(addressHelper));
    return cacheProviders.getCategoryPage(categoryPage, dynamicKeyGroup, evictDynamicKey).map(new Function<Reply<String>, String>() {

        @Override
        public String apply(Reply<String> responseBody) throws Exception {
            return responseBody.getData();
        }
    }).map(new Function<String, BaseResult<List<UnLimit91PornItem>>>() {

        @Override
        public BaseResult<List<UnLimit91PornItem>> apply(String s) throws Exception {
            return Parse91PronVideo.parseHot(s);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) BaseResult(com.dante.data.model.BaseResult) EvictDynamicKey(io.rx_cache2.EvictDynamicKey) Reply(io.rx_cache2.Reply) EvictDynamicKeyGroup(io.rx_cache2.EvictDynamicKeyGroup) DynamicKeyGroup(io.rx_cache2.DynamicKeyGroup) UnLimit91PornItem(com.dante.data.model.UnLimit91PornItem) FavoriteException(com.dante.exception.FavoriteException) MessageException(com.dante.exception.MessageException)

Example 4 with RxCache

use of io.rx_cache2.internal.RxCache in project 91Pop by DanteAndroid.

the class AppApiHelper method loadPorn91authorVideos.

@Override
public Observable<BaseResult<List<UnLimit91PornItem>>> loadPorn91authorVideos(String uid, String type, int page, boolean cleanCache) {
    // RxCache条件区别
    String condition = null;
    if (!TextUtils.isEmpty(uid)) {
        condition = uid;
    }
    DynamicKeyGroup dynamicKeyGroup = new DynamicKeyGroup(condition, page);
    EvictDynamicKey evictDynamicKey = new EvictDynamicKey(cleanCache);
    Observable<String> stringObservable = noLimit91PornServiceApi.authorVideos(uid, type, page);
    return cacheProviders.authorVideos(stringObservable, dynamicKeyGroup, evictDynamicKey).map(new Function<Reply<String>, String>() {

        @Override
        public String apply(Reply<String> responseBody) throws Exception {
            return responseBody.getData();
        }
    }).map(new Function<String, BaseResult<List<UnLimit91PornItem>>>() {

        @Override
        public BaseResult<List<UnLimit91PornItem>> apply(String s) throws Exception {
            return Parse91PronVideo.parseAuthorVideos(s);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) BaseResult(com.dante.data.model.BaseResult) EvictDynamicKey(io.rx_cache2.EvictDynamicKey) Reply(io.rx_cache2.Reply) EvictDynamicKeyGroup(io.rx_cache2.EvictDynamicKeyGroup) DynamicKeyGroup(io.rx_cache2.DynamicKeyGroup) UnLimit91PornItem(com.dante.data.model.UnLimit91PornItem) FavoriteException(com.dante.exception.FavoriteException) MessageException(com.dante.exception.MessageException)

Aggregations

BaseResult (com.dante.data.model.BaseResult)2 UnLimit91PornItem (com.dante.data.model.UnLimit91PornItem)2 FavoriteException (com.dante.exception.FavoriteException)2 MessageException (com.dante.exception.MessageException)2 Function (io.reactivex.functions.Function)2 DynamicKeyGroup (io.rx_cache2.DynamicKeyGroup)2 EvictDynamicKey (io.rx_cache2.EvictDynamicKey)2 EvictDynamicKeyGroup (io.rx_cache2.EvictDynamicKeyGroup)2 Reply (io.rx_cache2.Reply)2 ParseException (android.net.ParseException)1 JsonParseException (com.google.gson.JsonParseException)1 JsonSerializer (com.google.gson.JsonSerializer)1 Provides (dagger.Provides)1 CompositeException (io.reactivex.exceptions.CompositeException)1 RxCacheException (io.rx_cache2.RxCacheException)1 RxCache (io.rx_cache2.internal.RxCache)1 GsonSpeaker (io.victoralbertos.jolyglot.GsonSpeaker)1 NotSerializableException (java.io.NotSerializableException)1 ConnectException (java.net.ConnectException)1 UnknownHostException (java.net.UnknownHostException)1