Android使用RxJava+Retrofit2+Okhttp+MVP练习的APP
项目截图
这是我的目录结构
五步使用RxJava+Retrofit2+Okhttp+RxCache
第一步:导包
compile 'io.reactivex:rxjava:1.1.8'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.github.VictorAlbertos.RxCache:core:1.4.6'
第二步:新建API接口
public interface GanHuoService {
@GET("data/{type}/{number}/{page}")
Observable getDataResults(
@Path("type") String type,
@Path("number") int number,
@Path("page") int page
);
}
第三步:新建缓存接口
/**
* 缓存API接口
*
* @LifeCache设置缓存过期时间. 如果没有设置@LifeCache , 数据将被永久缓存理除非你使用了 EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup .
* EvictProvider可以明确地清理清理所有缓存数据.
* EvictDynamicKey可以明确地清理指定的数据 DynamicKey.
* EvictDynamicKeyGroup 允许明确地清理一组特定的数据. DynamicKeyGroup.
* DynamicKey驱逐与一个特定的键使用EvictDynamicKey相关的数据。比如分页,排序或筛选要求
* DynamicKeyGroup。驱逐一组与key关联的数据,使用EvictDynamicKeyGroup。比如分页,排序或筛选要求
*/
public interface CacheProviders {
//缓存时间 1天
@LifeCache(duration = 7, timeUnit = TimeUnit.DAYS)
Observable>> getHomeTypes(Observable observable, DynamicKey userName, EvictDynamicKey evictDynamicKey);
}
第四步:新建retrofit抽象类
public abstract class RetrofitUtils {
private static Retrofit mRetrofit;
private static OkHt