RXJava+Retrofit+MVP的簡單封裝

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<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 +
                '}';
    }
}

那個泛型就是我們最后想要的數據,這個到時根據自己API的數據去寫吧。
那么接下來編寫retrofit接口吧。
因為要這里才用了RxJava,編寫接口有一點點不一樣,前面變成了Observable,這里你就會發現,我們在HttpsResult后面傳入了個泛型Person,而這個Person就是我們需要的數據。

    @POST("restful/loginPost")
    Observable<HttpsResult<Person>> 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<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 = "網絡不給力";
            }
            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<String> 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<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時類型轉換
     * @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;
        }
    }

    /**
     * 授權之后回調
     * @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<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));
    }
}

好了,寫了這么多代碼,那就拿個功能來實現下吧,接下來將以用戶登錄來試試。
首先寫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<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了,看到這些接口,我當時整個人都是懵逼的,這也是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地址 傳送門

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,646評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,595評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,560評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,035評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,814評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,224評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,301評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,444評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,988評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,804評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,998評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,544評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,237評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,665評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,927評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,706評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,993評論 2 374

推薦閱讀更多精彩內容