RXJava+Retrofit+MVP的簡單封裝
馬上就要過年了,躁動的心早已按耐不住展箱,還是寫上一篇文章來冷靜下并齐。這次主要是搭建一個(gè)app框架(網(wǎng)絡(luò)請求方面),這也是自己慢慢摸索去搭建的骗灶。首先這個(gè)框架主要用的東西:看標(biāo)題就知道了。
OK秉馏,廢話不多說耙旦,RxJava用的1.0,(這個(gè)可以升的萝究,只是有些方法名改了)免都,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'
一般請求服務(wù)器,都會返回一些相同的數(shù)據(jù)帆竹,像什么code绕娘,message,status等栽连,如果你們后臺不是給這樣的數(shù)據(jù)险领,那你就別這樣整了侨舆,例如下面的數(shù)據(jù):
{
"status": 0,
"message": "成功",
"pageNum": 0,
"total": 0,
"data": {
"id": 1,
"username": "admin",
"password": "admin",
"dsId": 1,
"dsPhone": "10086",
"status": 1,
"createTime": 1481812879000,
"modifyTime": 1481812879000
}
}
這個(gè)json數(shù)據(jù)最外面就是公共的,data一般才是我們需要的數(shù)據(jù)绢陌,所以是可以抽取出來的挨下,所以建一個(gè)實(shí)體類。
public class HttpsResult<T> {
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 +
'}';
}
}
那個(gè)泛型就是我們最后想要的數(shù)據(jù)脐湾,這個(gè)到時(shí)根據(jù)自己API的數(shù)據(jù)去寫吧臭笆。
那么接下來編寫retrofit接口吧。
因?yàn)橐@里才用了RxJava秤掌,編寫接口有一點(diǎn)點(diǎn)不一樣耗啦,前面變成了Observable,這里你就會發(fā)現(xiàn)机杜,我們在HttpsResult后面?zhèn)魅肓藗€(gè)泛型Person帜讲,而這個(gè)Person就是我們需要的數(shù)據(jù)。
@POST("restful/loginPost")
Observable<HttpsResult<Person>> login(@Body RequestBody body);
好了椒拗,避免每次請求都要去初始化Retrofit似将,這里我們就可以封裝下,而且還可以統(tǒng)一設(shè)置所有請求的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);
//設(shè)置 Debug Log 模式
httpClientBuilder.addInterceptor(loggingInterceptor);
}
/**
* 添加攔截器在验,設(shè)置請求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;
}
}
當(dāng)然我們還可以將RXJava的Subscriber進(jìn)行封裝,這里我主要是為了攔截Http請求異常堵未,并針對某個(gè)異常進(jìn)行處理腋舌,比如說請求服務(wù)器接口出現(xiàn)了401,503等錯(cuò)誤渗蟹。
public abstract class ApiCallBack<M> extends Subscriber<M> {
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 = "網(wǎng)絡(luò)不給力";
}
if (code == 502 || code == 404) {
msg = "服務(wù)器異常块饺,請稍后再試";
}
onFailure(msg);
} else {
onFailure(e.getMessage());
}
Log.e("dandy","請求異常了 "+e.toString());
onFinish();
}
@Override
public void onCompleted() {
}
@Override
public void onNext(M m) {
onSuccess(m);
}
}
既然是框架,當(dāng)然少不了對activity雌芽,fragment的封裝了咯授艰。
public abstract class BaseActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
protected Context mContext;
private static final int REQUEST_CODE_PERMISSON = 2020; //權(quán)限請求碼
private boolean isNeedCheckPermission = true; //判斷是否需要檢測,防止無限彈框申請權(quán)限
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();
}
}
/**
* 檢查全部的權(quán)限世落,無權(quán)限則申請相關(guān)權(quán)限
*/
protected void checkAllPermission(){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
List<String> needRequestPermissonList = getDeniedPermissions(getNeedPermissions());
if (null != needRequestPermissonList && needRequestPermissonList.size() > 0) {
ActivityCompat.requestPermissions(this, needRequestPermissonList.toArray(
new String[needRequestPermissonList.size()]), REQUEST_CODE_PERMISSON);
}
}
}
/**
* 獲取權(quán)限集中需要申請權(quán)限的列表
*
* @param permissions
* @return
*/
private List<String> getDeniedPermissions(String[] permissions) {
List<String> 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時(shí)類型轉(zhuǎn)換
* @param id
* @param <E>
* @return
*/
@SuppressWarnings("unchecked")
public final <E extends View> E findView(int id){
try {
return (E) findViewById(id);
}catch (ClassCastException e){
throw e;
}
}
/**
* 授權(quán)之后回調(diào)
* @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();
}
}
/**
* 檢測所有的權(quán)限是否都已授權(quán)
*
* @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("當(dāng)前應(yīng)用缺少" + getDialogTipsPart()
+ "權(quán)限淮腾,該功能暫時(shí)無法使用。如若需要屉佳,請單擊【確定】按鈕前往設(shè)置中心進(jìn)行權(quán)限授權(quán)谷朝。")
.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();
}
/**
* 啟動當(dāng)前應(yīng)用設(shè)置頁面
*/
private void startAppSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
/**
* 獲取需要進(jìn)行檢測的權(quán)限數(shù)組
*/
protected abstract String[] getNeedPermissions();
/**
* 獲取彈框提示部分內(nèi)容
*
* @return
*/
protected String getDialogTipsPart() {
return "必要";
}
/**
* 權(quán)限授權(quán)成功
*/
protected abstract void permissionGrantedSuccess();
/**
* 權(quán)限授權(quán)失敗
*/
protected abstract void permissionGrantedFail();
}
上面的注釋都很清楚了,OK武花,接下來對fragment進(jìn)行封裝吧圆凰,
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);
//初始化數(shù)據(jù)
public void initData(){
}
//設(shè)置事件
public void setLinstener(){
}
}
既然是采用MVP架構(gòu),當(dāng)然少不了對它們的一些封裝髓堪,首先對view接口進(jìn)行簡單的封裝送朱,一般都是一些公共的功能,像顯示dialog等干旁,這個(gè)可以根據(jù)自己項(xiàng)目的需求吧
public interface BaseView {
void showDialog();
void cancelDialog();
void toastMeassager(String msg);
}
少不了MVP中的P(Presenter)驶沼,它就是負(fù)責(zé)model與view溝通的,所以它在整個(gè)環(huán)節(jié)處于很重要的位置争群。這里并沒有過多的內(nèi)容回怜,只是一個(gè)網(wǎng)絡(luò)請求,負(fù)責(zé)添加换薄,與注銷玉雾,當(dāng)有多個(gè)subscriber需要工作的時(shí)候就可以采用CompositeSubscription來進(jìn)行添加,這個(gè)玩意好像在RxJava2.0里面找不到了轻要,不知道是不是換名字了复旬。
public class BasePresenter<V> {
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));
}
}
好了,寫了這么多代碼冲泥,那就拿個(gè)功能來實(shí)現(xiàn)下吧驹碍,接下來將以用戶登錄來試試。
首先寫View接口凡恍,主要操作是獲取用戶名志秃,密碼,跳轉(zhuǎn)到主activity嚼酝,提示錯(cuò)誤信息浮还。
public interface IUserLoginView extends BaseView{
String getUseName();
String getPassword();
void toMainActivity();
void showFailedError();
int getUserType();
void userOrPwdIsNull();
}
好了,接下來Presenter闽巩,那個(gè)保存token不用管钧舌,這是我自己項(xiàng)目的玩意,這里就有個(gè)login方法涎跨,一旦被調(diào)用就會通過IUserLoginView去獲取用戶名和密碼延刘,然后轉(zhuǎn)成網(wǎng)絡(luò)請求的參數(shù)去請求服務(wù)器:
public class UserLoginPresenter extends BasePresenter<IUserLoginView>{
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<HttpsResult<Person>> subscriber1=new ApiCallBack<HttpsResult<Person>>() {
@Override
public void onSuccess(HttpsResult<Person> 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> 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了六敬,看到這些接口碘赖,我當(dāng)時(shí)整個(gè)人都是懵逼的,這也是MVP的惡心之處外构,但是它的優(yōu)勢就是解耦普泡,一旦項(xiàng)目大的話,你再來看代碼會感覺很清晰审编,所以做項(xiàng)目時(shí)不是看哪個(gè)框架流行就去用撼班,而是根據(jù)項(xiàng)目的需求,如果只是一個(gè)簡單的APP垒酬,你強(qiáng)行整個(gè)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();
}
}
以上差不多就是這樣的斟冕,當(dāng)然還有很多地方是需要慢慢完善的,畢竟是需要時(shí)間去現(xiàn)的缅阳。github地址 傳送門