Rxjava2配合Retrofit加MVP
来源:互联网 发布:swf工具 for mac 编辑:程序博客网 时间:2024/05/20 00:14
一.接口
1.ApiService 返回 Flowable观察者
import com.example.xx.rikao_09.bean.MessageBean;import com.example.xx.rikao_09.bean.Newslist;import java.util.List;import java.util.Map;import io.reactivex.Flowable;import retrofit2.http.GET;import retrofit2.http.QueryMap;public interface ApiService { @GET("nba") Flowable<MessageBean<List<Newslist>>> getNews(@QueryMap Map<String,String> map);}
2.MVP交互接口
public interface IView { void OnSuccess(Object o); void OnFailed(Exception e);}public interface IPresenter { void getData(Map<String,String> map);}public interface IModel { void getData(Map<String,String> map);}二.RetrofitUtils 工具类
import retrofit2.Retrofit;import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;import retrofit2.converter.gson.GsonConverterFactory;public class RetrofitUtils { private static volatile RetrofitUtils instance; private final Retrofit retrofit; private RetrofitUtils(){ retrofit = new Retrofit.Builder() .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl("http://api.tianapi.com/") .build(); } public static RetrofitUtils getInstance(){ if(instance==null){ synchronized (RetrofitUtils.class){ if(instance==null){ instance=new RetrofitUtils(); } } } return instance; } public ApiService getApiService(){ ApiService apiService = retrofit.create(ApiService.class); return apiService; }}三.M层
import com.example.xx.rikao_09.bean.MessageBean;import com.example.xx.rikao_09.bean.Newslist;import com.example.xx.rikao_09.http.RetrofitUtils;import com.example.xx.rikao_09.presenter.NewsPresenter;import java.util.List;import java.util.Map;import io.reactivex.Flowable;public class Model implements IModel{ private NewsPresenter presenter; public Model(NewsPresenter presenter) { this.presenter = presenter; } @Override public void getData(Map<String, String> map) { Flowable<MessageBean<List<Newslist>>> flowable = RetrofitUtils.getInstance().getApiService().getNews(map); presenter.get(flowable); }}四.P层
import com.example.xx.rikao_09.bean.MessageBean;import com.example.xx.rikao_09.bean.Newslist;import com.example.xx.rikao_09.model.Model;import com.example.xx.rikao_09.view.IView;import java.util.List;import java.util.Map;import io.reactivex.Flowable;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.schedulers.Schedulers;import io.reactivex.subscribers.DisposableSubscriber;public class NewsPresenter implements IPresenter{ private IView iView; private DisposableSubscriber<MessageBean<List<Newslist>>> subscriber; public void attachView(IView iView){ this.iView=iView; } @Override public void getData(Map<String, String> map) { Model model = new Model(this); model.getData(map); } public void detachView(){ if(subscriber!=null){ //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false if(!subscriber.isDisposed()){ subscriber.dispose(); } } } public void get(Flowable<MessageBean<List<Newslist>>> flowable){ subscriber = flowable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSubscriber<MessageBean<List<Newslist>>>() { @Override public void onNext(MessageBean<List<Newslist>> listMessageBean) { if (listMessageBean != null) { List<Newslist> newslist = listMessageBean.getNewslist(); if (newslist != null) { iView.OnSuccess(newslist); } } } @Override public void onError(Throwable t) { iView.OnFailed(new Exception(t)); } @Override public void onComplete() { } }); }}五.V层
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import com.example.xx.rikao_09.R;import com.example.xx.rikao_09.bean.Newslist;import com.example.xx.rikao_09.presenter.NewsPresenter;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity implements IView { private static final String TAG = "MainActivity"; private NewsPresenter Presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Presenter = new NewsPresenter(); Presenter.attachView(this); Map<String,String> map = new HashMap<>(); map.put("key","71e58b5b2f930eaf1f937407acde08fe"); map.put("num","10"); Presenter.getData(map); } @Override public void OnSuccess(Object o) { if(o instanceof List){ List<Newslist> list= (List<Newslist>) o; Log.e(TAG, "OnSuccess: "+list.size() ); } } @Override public void OnFailed(Exception e) { Log.e(TAG, "onFailed: " + e.getMessage()); } @Override protected void onDestroy() { super.onDestroy(); if(Presenter!=null){ Presenter.detachView(); } }}五.依赖
//2.0 Rxjava compile "io.reactivex.rxjava2:rxjava:2.1.7" compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0" compile 'io.reactivex.rxjava2:rxandroid:2.0.1' //黄油刀 compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' //Retrofit compile 'com.squareup.retrofit2:retrofit:2.3.0'//retrofit依赖 //compile 'com.squareup.retrofit2:converter-scalars:2.1.0 '//场景是这样,服务端返回一个不定类型的json数据,无法确定映射的Gson对象。 compile 'com.squareup.retrofit2:converter-gson:2.3.0'//retrofit内部封装的GSON //compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'//log //fresco compile 'com.facebook.fresco:fresco:0.12.0' // 在 API < 14 上的机器支持 WebP 时,需要添加 compile 'com.facebook.fresco:animated-base-support:0.12.0' // 支持 WebP (静态图+动图),需要添加 compile 'com.facebook.fresco:animated-webp:0.12.0' compile 'com.facebook.fresco:webpsupport:0.12.0'
阅读全文
0 0
- Rxjava2配合Retrofit加MVP
- MVP-Dagger2-Rxjava2-Retrofit整合
- MVP+Dragger2+Rxjava2+Retrofit+OKhttp进行开发。
- Dagger2+Rxjava2+Retrofit+MVP小案例
- RxJava2+MVP+Retrofit框架简单实现RecyclerView
- 高仿《One》APP Design + MVP + RxJava2 + Retrofit + Dagger2 + Realm
- mvp框架学习实战代码(配合retrofit+dagger2+rxjava)
- (安卓) MVP 框架 (Rxjava2+Retrofit)结合 实现网络请求
- MVP + Retrofit
- Retrofit+Rxjava2问题小记
- RxJava2+Retrofit+RxBinding
- 一步步封装Retrofit + RxJava2
- RxJava2和Retrofit
- RxJava2.0+Retrofit使用
- 整合MVP+Retrofit2+RxJava2
- RxJava2+retrofit实现网络封装
- rxjava2 与 retrofit 网络请求
- (备忘录)Rxjava2+Retrofit封装1
- 多类别SVM损失和交叉熵损失比较
- 聚宝川之行
- 机器学习实例之PCA(主成分分析)
- Leetcode代码学习周记——Hamming Distance
- POJ
- Rxjava2配合Retrofit加MVP
- 第7节项目5-将极坐标转换为直角坐标
- 地形建模(一)——TIN地形的生成
- 发现shell中一个有趣的现象
- linux库及静态库及动态库的创建
- cqoi浅谈On2 的排序
- VMWare安装CentOs虚拟机详细步骤
- 面向组件编程之Unity 1.怎样获取脚本所在物体的各种组件,例如:transform/BoxCollider
- 2017.12.09【NOIP提高组】模拟赛B组总结