抽取基類框架
阿新 • • 發佈:2018-11-09
一:在之前寫好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); } }