Android整合搭建RxJava+Retrofit+LiveData+OkHttp框架实。
。。
⼀、⼯程项⽬
包名解释:
base:基类库。
BaseDto类为服务器返回公共实体;BaseHttpSubscriber类⾃定义请求服务器被观察者;BaseRepository类请求⽹络数据基类。
exception:异常类模块。
ApiException类前端⾃定义Exception;ServerException类服务器返回的Exception;ExceptionEngine类拦截各种异常处理。
https:Retrofit+OkHttp封装⽹络请求模块。
ApiService接⼝API的Retrofit注解;RequetRetrofit类的⽹络请求⽤到的Retrofit+OkHttp。
interceptor:⾃定义⽹络请求。
model:数据模型层。定义实体类。
repository:数据仓库。包括⽹络数据获取,sqlite⼩型数据库,⽂件File,SharedPreferences数据存
储。
view:视图层。主要包含Activity+Fragment实体类。
viewmodel:VM视图模型层。
⼆、引⽤类库
implementation 'com.alibaba:fastjson:1.1.70.android' implementation 'de.gson:gson:2.8.4' implementation 'com.squareup.okhttp3:okhttp:3.11.0'
//Rxlifecycle
implementation 'llo:rxlifecycle:0.3.1'
implementation 'llo:rxlifecycle-components:0.3.1' implementation 'com.github.bumptech.glide:glide:4.8.0' implementation 'fit2:converter-gson:2.+'
//必须使⽤
implementation 'com.lzy:okgo:3.0.4'
implementation 'com.squareup.okio:okio:1.5.0'
implementation 'java2:rxjava:2.2.2'
implementation 'java2:rxandroid:2.1.0' implementation 'fit:retrofit2-rxjava2-adapter:1.+' implementation 'fit2:retrofit:2.+' implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0' implementation 'fit2:converter-gson:2.+' implementation 'fit2:converter-scalars:2.0.0' implementation 'com.facebook.stetho:stetho:1.4.2' implementation 'com.facebook.stetho:stetho-okhttp3:1.4.2'
// 依赖以下两个库,会⾃动引⽤基础库与Android库
implementation 'lifecycle2:rxlifecycle-components:2.1.0' implementation 'lifecycle2:rxlifecycle-navi:2.1.0' implementation 'permissions:rxpermissions:0.7.0@aar' implementation 'binding:rxbinding:1.+'
// Lifecycles, LiveData 和 ViewModel
implementation "android.arch.lifecycle:extensions:1.1.0"
// 对 RxJava 的⽀持android retrofit
implementation "android.:rxjava2:1.0.0-alpha5"
三、⽹络请求封装
1、BaseDto类
package com.ylink.frameworkdemo.base;
import java.io.Serializable;
/**
* 服务器返回公共实体
*
* @param <T>
* @author twilight
* @Time 2019-07-21
*/
public class BaseDto<T> implements Serializable {
private String statusCode;
private String statusDesc;
private T data;
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
public String getStatusDesc() {
return statusDesc;
}
public void setStatusDesc(String statusDesc) {
this.statusDesc = statusDesc;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
2、BaseHttpSubscriber类
package com.ylink.frameworkdemo.base;
import androidx.lifecycle.MutableLiveData;
import com.ylink.frameworkdemo.Constant;
import com.ption.ApiException;
import com.ption.ExceptionEngine;
import com.ption.ServerException;
activestreams.Subscriber;
activestreams.Subscription;
/**
* ⾃定义请服务器被观察者
*
* @author twilight
* @Time 2019-07-21
*/
public class BaseHttpSubscriber<T> implements Subscriber<BaseDto<T>> { //异常类
private ApiException ex;
public BaseHttpSubscriber() {
data = new MutableLiveData();
}
private MutableLiveData<BaseDto<T>> data;
public MutableLiveData<BaseDto<T>> get() {
return data;
}
public void set(BaseDto<T> t) {
this.data.setValue(t);
}
public void onFinish(BaseDto<T> t) {
set(t);
}
@Override
public void onSubscribe(Subscription s) {
// 观察者接收事件 = 1个
}
@Override
public void onNext(BaseDto<T> t) {
if (t.getStatusCode().equals(Constant.RespCode.R000)) {
onFinish(t);
} else{
ex = ExceptionEngine.handleException(new StatusCode(), t.getStatusDesc())); getErrorDto(ex);
}
}
@Override
public void onError(Throwable t) {
ex = ExceptionEngine.handleException(t);
getErrorDto(ex);
}
/**
* 初始化错误的dto
*
* @param ex
*/
private void getErrorDto(ApiException ex) {
BaseDto dto = new BaseDto();
dto.StatusCode());
dto.StatusDesc());
onFinish((BaseDto<T>) dto);
}
@Override
public void onComplete() {
}
}
3、BaseRepository类
package com.ylink.frameworkdemo.base;
activex.Flowable;
activex.android.schedulers.AndroidSchedulers;
activex.schedulers.Schedulers;
/**
* Repository基类
*
* @author twilight
* @Time 2019-07-21
*/
public class BaseRepository {
/**
* 请求⽹络
* @param flowable
* @param <T>
* @return
*/
public <T> BaseHttpSubscriber<T> request(Flowable<BaseDto<T>> flowable){
BaseHttpSubscriber<T> baseHttpSubscriber = new BaseHttpSubscriber<>(); //RxJava Subscriber回调 flowable.subscribeOn(Schedulers.io()) //解决背压
.observeOn(AndroidSchedulers.mainThread())
.subscribe(baseHttpSubscriber);
return baseHttpSubscriber;
}
}
4、ApiException类
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论