MVP-Dagger2-Rxjava2-Retrofit整合
来源:互联网 发布:公安网络报警平台 编辑:程序博客网 时间:2024/05/22 10:55
最近刚学习了MVP-Dagger2-Rxjava2-Retrofit
将MVP-Dagger2-Rxjava2-Retrofit做了一个简单的整合
贴出部分代码
源码下载地址在最下方
添加依赖:
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' // 用于rxjava和retrofit
//okhttp
compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0' //logging拦截器
//dagger
apt 'com.google.dagger:dagger-compiler:2.0.2'
compile 'com.google.dagger:dagger:2.0.2'
provided 'org.glassfish:javax.annotation:10.0-b28'
//rxjava
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
apply plugin: 'com.neenbedankt.android-apt' //app model中添加
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' //app build中添加}
MVP的model部分,用来做数据处理
public class HomeModel {
ApiService mApiService;
public HomeModel(ApiService apiService) {
mApiService = apiService;
}
public Observable<ResponseBody> getData() {
Observable<ResponseBody> data = mApiService.getBannerData();
return data;
}
}
public class HomePresenter extends BasePresenter<HomeModel, BaseView> {
public HomePresenter(HomeModel model, BaseView view) {
super(model, view);
}
@Override
public void requestData() {
mModel.getData()
.subscribeOn(Schedulers.io())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>() {
Disposable mDisposable;
@Override
public void onSubscribe(@NonNull Disposable d) {
mDisposable = d;
}
@Override
public void onNext(@NonNull ResponseBody responseBody) {
mView.showloading();
if (responseBody != null) {
mView.showResult(responseBody.toString());
} else {
mView.showNoData();
}
mView.showDismiss();
}
@Override
public void onError(@NonNull Throwable e) {
mView.showDismiss();
mView.showError(e.getMessage().toString());
}
@Override
public void onComplete() {
mView.showDismiss();
mDisposable.dispose();
}
});
}
}
@Module
public class HomeModule {
private BaseView mView;
public HomeModule(BaseView view) {
mView = view;
}
@Provides
@UIScope
public BaseView provideView() {
return mView;
}
@Provides
@UIScope
public HomeModel provideHomeModel(ApiService apiService) {
return new HomeModel(apiService);
}
@Provides
@UIScope
public HomePresenter provideHomePresenter(HomeModel model, BaseView view) {
return new HomePresenter(model, view);
}
@Provides
@UIScope
public ProgressDialog provideProgressDialog(BaseView view) {
return new ProgressDialog((Context) view);
}
}
Dagger中Component关联module
@Singleton
@Component(modules = {AppModule.class, HttpModule.class})
public interface AppComponent {
ApiService getApi();
}
public class MyApplication extends Application {
private AppComponent mAppComponent;
public static MyApplication get(Context context) {
return (MyApplication) context.getApplicationContext();
}
public AppComponent getAppComponent() {
return mAppComponent;
}
最后mainActivity中
public class MainActivity extends AppCompatActivity implements BaseView {
@Overridepublic void onCreate() {
super.onCreate();
mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).httpModule(new HttpModule()).build();
}
@Inject
HomePresenter mHomePresenter;
@Inject
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DaggerHomeComponent.builder()
.appComponent(MyApplication.get(this).getAppComponent())
.homeModule(new HomeModule(this))
.build()
.inject(this);
mHomePresenter.requestData();
}
@Override
public void showloading() {
mProgressDialog.show();
}
@Override
public void showDismiss() {
mProgressDialog.dismiss();
}
@Override
public void showError(String msg) {
mProgressDialog.dismiss();
Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
}
@Override
public void showResult(String list) {
Toast.makeText(this, "SUCCESS:" + list.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void showNoData() {
Toast.makeText(this, "" + "数据NULL", Toast.LENGTH_SHORT).show();
}
}
github源码地址:
MVP-Dagger-Retrofit2-RxJava2
阅读全文
1 0
- MVP-Dagger2-Rxjava2-Retrofit整合
- Dagger2+Rxjava2+Retrofit+MVP小案例
- 高仿《One》APP Design + MVP + RxJava2 + Retrofit + Dagger2 + Realm
- MVP+Dagger2+Retrofit+Rxjava
- RxJava+Retrofit+MVP+Dagger2
- RxJava+Retrofit+MVP+Dagger2
- 整合MVP+Retrofit2+RxJava2
- Rxjava2配合Retrofit加MVP
- RxJava2 + Retrofit2 + Mvp + Dagger2框架搭建
- Rxjava+retrofit+mvp整合
- 从0开始搭建rxjava+retrofit+mvp+dagger2整合基础框架(rxjava+retrofit网络层搭建)
- MVP+Dragger2+Rxjava2+Retrofit+OKhttp进行开发。
- RxJava2+MVP+Retrofit框架简单实现RecyclerView
- 一套整合主流HTTP网络、图片加载、MVP(RxJava2+Dagger2)架构的快速高效的开发框架RxEasyAndroid
- Rxjava + retrofit + dagger2 + mvp搭建Android框架
- 简单实用的mvp+retrofit+rxjava+dagger2
- Android Mvp快速搭建框架MVP+Dagger2+Retrofit+Rxjava
- MVP+Dagger2+Retrofit实现更清晰的架构
- WPF 解决winform控件遮盖WPF控件问题
- PHP-redis中文文档
- 就先有父类先有子类(个人理解)
- maven 官网版本介绍
- ZipOutputStream输出流
- MVP-Dagger2-Rxjava2-Retrofit整合
- PHP底层工作原理
- 运行循环-RunLoop
- linux下配置jdk步骤
- Eclipse中的BuildPath详解
- 程序员最核心的竞争力是什么?
- OpenCV学习笔记(15)Opencv之<Vec3b>
- Android中的include标签
- 循环神经网络(RNN, Recurrent Neural Networks)介绍