RXJava+Retrofit+MVP的简单封装
马上就要过年了,躁动的心早已按耐不住,还是写上一篇文章来冷静下。这次主要是搭建一个app框架(网络请求方面),这也是自己慢慢摸索去搭建的。首先这个框架主要用的东西:看标题就知道了。
OK,废话不多说,RxJava用的1.0,(这个可以升的,只是有些方法名改了),Retrofit用的2.0
首先引用这些玩意吧:
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'
一般请求服务器,都会返回一些相同的数据,像什么code,message,status等,如果你们后台不是给这样的数据,那你就别这样整了,例如下面的数据:
{
"status": 0,
"message": "成功",
"pageNum": 0,
"total": 0,
"data": {
"id": 1,
"username": "admin",
"password": "admin",
"dsId": 1,
"dsPhone": "10086",
"status": 1,
"createTime": 1481812879000,
"modifyTime": 1481812879000
}
}
这个json数据最外面就是公共的,data一般才是我们需要的数据,所以是可以抽取出来的,所以建一个实体类。
public class HttpsResult {
private int status;
private String message;
private int pageNum;
private int total;
private T data;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "HttpsResult{" +
"status=" + status +
", message='" + message + '\'' +
", pageNum=" + pageNum +
", total=" + total +
", data=" + data +
'}';
}
}
那个泛型就是我们最后想要的数据,这个到时根据自己API的数据去写吧。
那么接下来编写retrofit接口吧。
因为要这里才用了RxJava,编写接口有一点点不一样,前面变成了Observable,这里你就会发现,我们在HttpsResult后面传入了个泛型Person,而这个Person就是我们需要的数据。
@POST("restful/loginPost")
Observable> login(@Body RequestBody body);
好了,避免每次请求都要去初始化Retrofit,这里我们就可以封装下,而且还可以统一设置所有请求的Header。如下就是封装后的RetrofitClient:
public class RetrofitClient {
public static Retrofit mRetrofit;
private static final int DEFAULT_TIMEOUT = 5;
public static Retrofit retrofit() {
if (mRetrofit == null) {
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
// Log信息拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//设置 Debug Log 模式
httpClientBuilder.addInterceptor(loggingInterceptor);
}
/**
* 添加拦截器,设置请求header
*/
httpClientBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Content-Type", "application/json")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
Gson gson = new GsonBuilder()
//配置Gson
.setDateFormat("yyyy-MM-dd hh:mm:ss")
.create();
OkHttpClient okHttpClient = httpClientBuilder.build();
mRetrofit=new Retrofit.Builder()
.baseUrl(ApiStore.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient)
.build();
}
return mRetrofit;
}
}
当然我们还可以将RXJava的Subscriber进行封装,这里我主要是为了拦截Http请求异常,并针对某个异常进行处理,比如说请求服务器接口出现了401,503等错误。
public abstract class ApiCallBack extends Subscriber {
public abstract void onSuccess(M model);
public abstract void onFailure(String msg);
public abstract void onFinish();
@Override
public void onError(Throwable e) {
e.printStackTrace();
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
//httpException.response().errorBody().string()
int code = httpException.code();
String msg = httpException.getMessage();
Log.d("dandy","code=" + code);
if (code == 504) {
msg = "网络不给力";
}
if (code == 502 || code == 404) {
msg = "服务器异常,请稍后再试";
}
onFailure(msg);
} else {
onFailure(e.getMessage());
}
Log.e("dandy","请求异常了 "+e.toString());
onFinish();
}
@Override
public void onCompleted() {
}
@Override
public void onNext(M m) {
onSuccess(m);
}
}
既然是框架,当然少不了对activity,fragment的封装了咯。
public abstract class BaseActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
protected Context mContext;
private static final int REQUEST_CODE_PERMISSON = 2020; //权限请求码
private boolean isNeedCheckPermission = true; //判断是否需要检测,防止无限弹框申请权限
private Toolbar mToolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
protected void initData() {
}
protected abstract void initView();
@Override
protected void onResume() {
super.onResume();
if (isNeedCheckPermission){
checkAllPermission();
}
}
/**
* 检查全部的权限,无权限则申请相关权限
*/
protected void checkAllPermission(){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
List needRequestPermissonList = getDeniedPermissions(getNeedPermissions());
if (null != needRequestPermissonList && needRequestPermissonList.size() > 0) {
ActivityCompat.requestPermissions(this, needRequestPermissonList.toArray(
new String[needRequestPermissonList.size()]), REQUEST_CODE_PERMISSON);
}
}
}
/**
* 获取权限集中需要申请权限的列表
*
* @param permissions
* @return
*/
private List getDeniedPermissions(String[] permissions) {
List needRequestPermissonList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED ||
ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
needRequestPermissonList.add(permission);
}
}
return needRequestPermissonList;
}
/**
*
*/
public void addToolbar(){
mToolbar = findView(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
/**
* 弹出Toast
*
* @param text
*/
public void showToast(String text) {
Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
/**
* 解决绑定view时类型转换
* @param id
* @param
* @return
*/
@SuppressWarnings("unchecked")
public final E findView(int id){
try {
return (E) findViewById(id);
}catch (ClassCastException e){
throw e;
}
}
/**
* 授权之后回调
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==REQUEST_CODE_PERMISSON){
if (!verifyPermissions(grantResults)) {
permissionGrantedFail();
showTipsDialog();
isNeedCheckPermission = false;
} else permissionGrantedSuccess();
}
}
/**
* 检测所有的权限是否都已授权
*
* @param grantResults
* @return
*/
private boolean verifyPermissions(int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* 显示提示对话框
*/
protected void showTipsDialog() {
new AlertDialog.Builder(this).setTitle("信息").setMessage("当前应用缺少" + getDialogTipsPart()
+ "权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
}).show();
}
/**
* 启动当前应用设置页面
*/
private void startAppSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
/**
* 获取需要进行检测的权限数组
*/
protected abstract String[] getNeedPermissions();
/**
* 获取弹框提示部分内容
*
* @return
*/
protected String getDialogTipsPart() {
return "必要";
}
/**
* 权限授权成功
*/
protected abstract void permissionGrantedSuccess();
/**
* 权限授权失败
*/
protected abstract void permissionGrantedFail();
}
上面的注释都很清楚了,OK,接下来对fragment进行封装吧,
public abstract class BaseFragment extends android.support.v4.app.Fragment{
private Activity mActivity;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mActivity = getActivity();
View view = initView(inflater,container);
initFindViewById(view);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
setLinstener();
}
protected abstract View initView(LayoutInflater inflater,ViewGroup container);
protected abstract void initFindViewById(View view);
//初始化数据
public void initData(){
}
//设置事件
public void setLinstener(){
}
}
既然是采用MVP架构,当然少不了对它们的一些封装,首先对view接口进行简单的封装,一般都是一些公共的功能,像显示dialog等,这个可以根据自己项目的需求吧
public interface BaseView {
void showDialog();
void cancelDialog();
void toastMeassager(String msg);
}
少不了MVP中的P(Presenter),它就是负责model与view沟通的,所以它在整个环节处于很重要的位置。这里并没有过多的内容,只是一个网络请求,负责添加,与注销,当有多个subscriber需要工作的时候就可以采用CompositeSubscription来进行添加,这个玩意好像在RxJava2.0里面找不到了,不知道是不是换名字了。
public class BasePresenter {
public V mvpView;
protected ApiStore mApiStore;
private CompositeSubscription mCompositeSubscription;
public void attachView(V mvpView){
this.mvpView=mvpView;
mApiStore = RetrofitClient.retrofit().create(ApiStore.class);
}
public void detachView() {
this.mvpView = null;
onUnsubscribe();
}
/**
* rxJava取消注册
*/
public void onUnsubscribe() {
if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
mCompositeSubscription.unsubscribe();
}
}
public void addSubscription(Observable observable, Subscriber subscriber) {
if (mCompositeSubscription == null) {
mCompositeSubscription = new CompositeSubscription();
}
mCompositeSubscription.add(observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber));
}
}
好了,写了这么多代码,那就拿个功能来实现下吧,接下来将以用户登录来试试。
首先写View接口,主要操作是获取用户名,密码,跳转到主activity,提示错误信息。
public interface IUserLoginView extends BaseView{
String getUseName();
String getPassword();
void toMainActivity();
void showFailedError();
int getUserType();
void userOrPwdIsNull();
}
好了,接下来Presenter,那个保存token不用管,这是我自己项目的玩意,这里就有个login方法,一旦被调用就会通过IUserLoginView去获取用户名和密码,然后转成网络请求的参数去请求服务器:
public class UserLoginPresenter extends BasePresenter{
private int type;
private IUserLoginView mUserLoginView;
private Context mContext;
public UserLoginPresenter( IUserLoginView mUserLoginView) {
this.mUserLoginView = mUserLoginView;
attachView(mUserLoginView);
mContext=DriverApplication.getContextObject();
}
public void login(){
String userName=mUserLoginView.getUseName();
String pwd=mUserLoginView.getPassword();
type=mUserLoginView.getUserType();
if (userName==null||userName.equals("")||pwd==null||pwd.equals("")){
mUserLoginView.userOrPwdIsNull();
return;
}
mUserLoginView.showDialog();
ApiCallBack> subscriber1=new ApiCallBack>() {
@Override
public void onSuccess(HttpsResult model) {
mUserLoginView.cancelDialog();
if (model.getStatus()==0){
closeRetrofit();
mUserLoginView.toMainActivity();
savaUserToken(model);
}else {
mUserLoginView.toastMeassager(model.getMessage());
}
}
@Override
public void onFailure(String msg) {
//Log.e("dandy","error "+msg);
mUserLoginView.cancelDialog();
}
@Override
public void onFinish() {
}
};
User user=new User();
user.setCategory(type);
user.setUsername(userName);
user.setPassword(pwd);
Gson gson=new Gson();
String route= gson.toJson(user);
RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
addSubscription(mApiStore.login(body),subscriber1);
}
private void savaUserToken(HttpsResult person){
UtilSharedPreferences.saveStringData(mContext, Config.KEY_TOKEN,person.getMessage());
UtilSharedPreferences.saveStringData(mContext,Config.KEY_USERNAME,person.getData().getUsername());
if (person.getData().getDiscern()==1){
UtilSharedPreferences.saveStringData(mContext,Config.KEY_USER_TYPE,mContext.getResources().getString(R.string.user_teacher));
}else {
UtilSharedPreferences.saveStringData(mContext,Config.KEY_USER_TYPE,mContext.getResources().getString(R.string.user_student));
}
if (type==1){
UtilSharedPreferences.saveStringData(mContext,Config.KEY_USER_TYPE,mContext.getResources().getString(R.string.user_admin));
}
}
/**
* 注销,取消订阅
*/
public void destory(){
detachView();
}
}
最后就是我们activity了,看到这些接口,我当时整个人都是懵逼的,这也是MVP的恶心之处,但是它的优势就是解耦,一旦项目大的话,你再来看代码会感觉很清晰,所以做项目时不是看哪个框架流行就去用,而是根据项目的需求,如果只是一个简单的APP,你强行整个MVP,那就尴尬了。。。
public class LoginActivity extends BaseActivity implements IUserLoginView{
private UserLoginPresenter mUserLoginPresenter=new UserLoginPresenter(this);
private AutoCompleteTextView mUserName;
private EditText mPassword;
private RadioGroup mRadioGroup;
private RadioButton mAdmin,mTeacher,mStudent;
private ProgressDialog mDialog;
private Button mSingIn;
private int mUserType= Config.USER_TYPE_ADMIN;
private LinearLayout mLoginLayout;
@Override
protected void initView() {
setContentView(R.layout.activity_login);
mLoginLayout=findView(R.id.longin_layout);
mUserName=findView(R.id.login_username);
mPassword=findView(R.id.login_password);
mRadioGroup=findView(R.id.login_group);
mAdmin=findView(R.id.login_admin);
mTeacher=findView(R.id.login_teacher);
mStudent=findView(R.id.login_student);
mSingIn=findView(R.id.login_sing_in);
addToolbar();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
@Override
protected void initData() {
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.login_admin:
mUserType=Config.USER_TYPE_ADMIN;
break;
case R.id.login_teacher:
mUserType=Config.USER_TYPE_TEACHER;
break;
case R.id.login_student:
mUserType=Config.USER_TYPE_STUDENT;
break;
default:
break;
}
}
});
mSingIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mUserLoginPresenter.login();
}
});
mAdmin.setChecked(true);
}
@Override
protected String[] getNeedPermissions() {
return new String[0];
}
@Override
protected void permissionGrantedSuccess() {
}
@Override
protected void permissionGrantedFail() {
}
@Override
public String getUseName() {
return mUserName.getText().toString();
}
@Override
public String getPassword() {
return mPassword.getText().toString();
}
@Override
public void showDialog() {
mDialog=ProgressDialog.show(LoginActivity.this,"提示","正在登录...");
}
@Override
public void cancelDialog() {
mDialog.cancel();
}
@Override
public void toastMeassager(String msg) {
Snackbar.make(mLoginLayout, msg, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
@Override
public void toMainActivity() {
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
finish();
}
@Override
public void showFailedError() {
}
/**
* 获取用户类型
* @return
*/
@Override
public int getUserType() {
return mUserType;
}
@Override
public void userOrPwdIsNull() {
Toast.makeText(LoginActivity.this,"用户名或者密码不能为空",Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
mUserLoginPresenter.destory();
}
}
以上差不多就是这样的,当然还有很多地方是需要慢慢完善的,毕竟是需要时间去现的。github地址 传送门