package http; import java.util.List; import java.util.Map; import Bean.MessageBean; import Bean.News; import io.reactivex.Flowable; import retrofit2.http.GET; import retrofit2.http.QueryMap; public interface ApiService { @GET("93app/data.do") Flowable<MessageBean<List<News>>> getNews(@QueryMap Map<String,String> map); }
package model;import java.util.Map;package http; import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.GsonConverterFactory; import retrofit2.Retrofit; public class RetrofitUtils { private static volatile RetrofitUtils instance; private final Retrofit retrofit; private final ApiService apiService; public RetrofitUtils() { retrofit = new Retrofit.Builder() .baseUrl("http://www.93.gov.cn/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); apiService = retrofit.create(ApiService.class); } public static RetrofitUtils getInstance() { if(instance == null) { synchronized (RetrofitUtils.class) { if(null == instance) { instance = new RetrofitUtils(); } } } return instance; } public ApiService getApiService() { return apiService; } }
public interface IModel {
void getData(Map<String,String> map);
}
然后写Presenter层:package model; import java.util.List; import java.util.Map; import Bean.MessageBean; import Bean.News; import http.RetrofitUtils; import io.reactivex.Flowable; import presenter.NewsPresenter; /** * Created by ZhangTAO on 2017/12/6. */ public class NewsModel implements IModel{ private NewsPresenter presenter; public NewsModel(NewsPresenter newsPresenter) { this.presenter = newsPresenter; } @Override public void getData(Map<String, String> map) { Flowable<MessageBean<List<News>>> flowable = RetrofitUtils.getInstance().getApiService().getNews(map); presenter.getNews(flowable); } }
package presenter; import java.util.Map; /** * Created by ZhangTAO on 2017/12/6. */ public interface BasePresenter { void getData(Map<String,String> map); }
package presenter; import java.util.List; import java.util.Map; import Bean.MessageBean; import Bean.News; import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DisposableSubscriber; import model.NewsModel; import view.IView; /** * Created by ZhangTAO on 2017/12/6. */ public class NewsPresenter implements BasePresenter{ private IView iv; private DisposableSubscriber subscriber; public void attachView(IView iv) { this.iv = iv; } public void detachView() { if(iv != null) { iv = null; } if(subscriber != null) { if(!subscriber.isDisposed()) { subscriber.dispose(); } } } @Override public void getData(Map<String, String> map) { NewsModel model = new NewsModel(this); model.getData(map); } public void getNews(Flowable<MessageBean<List<News>>> flowable) { subscriber = flowable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSubscriber<MessageBean<List<News>>>() { @Override public void onNext(MessageBean<List<News>> listMessageBean) { if(listMessageBean != null) { List<News> list = listMessageBean.getData(); if(list!=null) { iv.onSuccess(list); } } } @Override public void onError(Throwable t) { iv.onFailed(new Exception(t)); } @Override public void onComplete() { } }); } }
View层时通过一个接口将数据传值主界面的:
package view; /** * Created by ZhangTAO on 2017/12/6. */ public interface IView { void onSuccess(Object o); void onFailed(Exception e); }
接下来就是主界面了:
package zhangtao.bwie.com.reretrofitmvpdemo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; import java.util.HashMap; import java.util.List; import Bean.News; import presenter.NewsPresenter; import view.IView; public class MainActivity extends AppCompatActivity implements IView{ private NewsPresenter newsPresenter; private TextView textView; private List<News> news; private Handler ler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); for(int i=0;i<news.size();i++) { String title = news.get(i).getSubTitle(); textView.setText(title); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); HashMap<String, String> map = new HashMap<>(); map.put("channelId","0"); map.put("startNum","0"); newsPresenter = new NewsPresenter(); newsPresenter.attachView(this); newsPresenter.getData(map); textView = (TextView) findViewById(R.id.text1); } @Override public void onSuccess(Object o) { if(o instanceof List) { news = (List<News>) o; Log.d("zzz","传递"+news.size()); ler.sendMessage(new Message()); } } @Override public void onFailed(Exception e) { Log.d("zzz", "onFailed: " + e.getMessage()); } @Override protected void onDestroy() { super.onDestroy(); if(newsPresenter != null) { newsPresenter.detachView(); } } }
Android Retrofit+RxJava+MVP封装
最新推荐文章于 2021-05-25 22:09:02 发布