抽取基类框架

一:在之前写好bean类,然后定义接口封装网络工具

1.Api

接口有参数的写参数

public interface ILoginApi {
    @GET("user/login")
    Observable<LoginBean> login1(@Query("mobile") String mobile,@Query("password") String password);
}

没有参数的不写

@GET("home/getHome")
Observable<ShouBean> shouye();

2.网络请求工具类HttpUtils

public class HttpUtils {
    private static final String BASE_URL = "http://www.zhaoapi.cn/";
    private Retrofit retrofit;

    private static final class SINGLE_INSTANCE{
        private static final HttpUtils _INSTANCE = new HttpUtils();
    }

    public static HttpUtils getInstance(){
        return SINGLE_INSTANCE._INSTANCE;
    }

    private HttpUtils(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(buildOkHttpClient())
                .build();
    }

    private OkHttpClient buildOkHttpClient() {
        return new OkHttpClient.Builder()
                .readTimeout(5000,TimeUnit.MILLISECONDS)
                .writeTimeout(5000,TimeUnit.MILLISECONDS)
                .build();
    }

    public <T> T create(Class<T> clazz){
        return retrofit.create(clazz);
    }
}

二:基类Base

1.BaseView

public interface BaseView {
    Context context();
}

2.BasePresenter

public abstract class BasePresenter<V extends BaseView> {
    protected V iView;

    public BasePresenter(){
        initModel();
    }

    public abstract void initModel();

    public void attach(V iView){
        this.iView = iView;
    }

    public void dettach(){
        this.iView = null;
    }

    protected Context context(){
        if (iView != null){
            return iView.context();
        }
        return null;
    }
}

3.BaseActivity

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView {

    protected P presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(provideLayoutId());
        ButterKnife.bind(this);

        presenter = providePresenter();
        attachView();
        initView();
        initListener();
        initData();
    }

    private void attachView(){
        if (presenter != null){
            presenter.attach(this);
        }
    }

    protected  void initView(){

    }

    protected  void initListener(){

    }

    protected abstract void initData();

    protected abstract P providePresenter();

    protected abstract int provideLayoutId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettach();
    }
}

三:view层

public interface LoginView extends BaseView {
    void onSuccess(LoginBean loginBean);

    void onFailed(Throwable t);
}

四:model层

public class LoginModel {
    public Observable<LoginBean> login(String mobile,String password){
        ILoginApi iLoginApi = RetrofitManager.getInstance().create(ILoginApi.class);
        Observable<LoginBean> loginBeanObservable = iLoginApi.login1(mobile,password);
        return loginBeanObservable;
    }
}

五:presenter层

public class LoginPresenter extends BasePresenter<LoginView> {
    private LoginModel loginModel;

    @Override
    public void initModel() {
        loginModel = new LoginModel();
    }

    public void login(String mobile,String password){
        loginModel.login(mobile,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<LoginBean>() {
                    @Override
                    public void accept(LoginBean loginBean) throws Exception {
                        if (loginBean != null & "0".equals(loginBean.getCode())) {
                            if (iView != null)
                                iView.onSuccess(loginBean);
                            return;
                        }
                        if (iView != null)
                            iView.onFailed(new Throwable("服务未响应"));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (iView != null)
                            iView.onFailed(new Throwable("网络异常"));
                    }
                });
    }
}

六:Mainactivity层

public class MainActivity extends BaseActivity<LoginPresenter> implements LoginView {

    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void initData() {

    }

    @Override
    protected LoginPresenter providePresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int provideLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void onSuccess(LoginBean loginBean) {
        Log.e("aaaaaaa", loginBean.getData().getUsername());
        Toast.makeText(MainActivity.this, "请求成功", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onFailed(Throwable t) {
        Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_LONG).show();
    }

    @Override
    public Context context() {
        return this;
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String trim = edName.getText().toString().trim();
        String pass = edPassword.getText().toString();
        presenter.login(trim,pass);
    }
}

猜你喜欢

转载自blog.csdn.net/gylgww/article/details/83892874
今日推荐