public interface ApiService {
@GET("93app/data.do")
Flowable<MessageBean<List<News>>> getNews(@QueryMap Map<String, String> map);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class RetrofitUtils {
private static volatile RetrofitUtils instance;
private static volatile RetrofitUtils instance;
private ApiService apiService;
private RetrofitUtils() {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("http://www.93.gov.cn/")
.build();
apiService = retrofit.create(ApiService.class);
}
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("http://www.93.gov.cn/")
.build();
apiService = retrofit.create(ApiService.class);
}
public static RetrofitUtils getInstance() {
if (instance == null) {
synchronized (RetrofitUtils.class) {
if (null == instance) {
instance = new RetrofitUtils();
}
}
}
return instance;
}
if (instance == null) {
synchronized (RetrofitUtils.class) {
if (null == instance) {
instance = new RetrofitUtils();
}
}
}
return instance;
}
public ApiService getApiService() {
return apiService;
}
}
return apiService;
}
}
------------------------------------------------------------------------------------------------------------------------
public interface IModel {
void getData(Map<String, String> map);
}
void getData(Map<String, String> map);
}
------------------------------------------------------------------------------------------------------------------------
public class NewsModel implements IModel {
private NewsPresenter presenter;
public NewsModel(NewsPresenter presenter){
this.presenter = presenter;
}
@Override
public void getData(Map<String, String> map) {
Flowable<MessageBean<List<News>>> flowable = RetrofitUtils.getInstance().getApiService()
.getNews(map);
presenter.getNews(flowable);
}
}
private NewsPresenter presenter;
public NewsModel(NewsPresenter presenter){
this.presenter = presenter;
}
@Override
public void getData(Map<String, String> map) {
Flowable<MessageBean<List<News>>> flowable = RetrofitUtils.getInstance().getApiService()
.getNews(map);
presenter.getNews(flowable);
}
}
-------------------------------------------------------------------------------------------------------------------------
public interface BasePresenter {
void getData(Map<String, String> map);
}
void getData(Map<String, String> map);
}
------------------------------------------------------------------------------------------------------------------------
public class NewsPresenter implements BasePresenter {
private IView iv;
private DisposableSubscriber subscriber;
private IView iv;
private DisposableSubscriber subscriber;
public void attachView(IView iv) {
this.iv = iv;
}
this.iv = iv;
}
public void detachView() {
if (iv != null) {
iv = null;
}
if (subscriber != null) {
if (!subscriber.isDisposed()) {
subscriber.dispose();
}
}
}
if (iv != null) {
iv = null;
}
if (subscriber != null) {
if (!subscriber.isDisposed()) {
subscriber.dispose();
}
}
}
@Override
public void getData(Map<String, String> map) {
IModel model = new NewsModel(this);
model.getData(map);
}
public void getData(Map<String, String> map) {
IModel model = new NewsModel(this);
model.getData(map);
}
public void getNews(Flowable<MessageBean<List<News>>> flowable) {
subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
subscriber = (DisposableSubscriber) 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);
}
}
}
.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));
}
public void onError(Throwable t) {
iv.onFailed(new Exception(t));
}
@Override
public void onComplete() {
public void onComplete() {
}
});
});
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------
public interface IView {
void onSuccess(Object o);
void onFailed(Exception e);
}
void onSuccess(Object o);
void onFailed(Exception e);
}
-------------------------------------------------------------------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity implements IView {
private static final String TAG = "MainActivity";
private static final String TAG = "MainActivity";
private NewsPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map<String, String> map = new HashMap<>();
map.put("channelId", "0");
map.put("startNum", "0");
map.put("channelId", "0");
map.put("startNum", "0");
presenter = new NewsPresenter();
presenter.attachView(this);
presenter.getData(map);
}
presenter.attachView(this);
presenter.getData(map);
}
@Override
public void onSuccess(Object o) {
if (o instanceof List) {
List<News> news = (List<News>) o;
Log.i(TAG, "onSuccess: " + news.size());
}
}
public void onSuccess(Object o) {
if (o instanceof List) {
List<News> news = (List<News>) o;
Log.i(TAG, "onSuccess: " + news.size());
}
}
@Override
public void onFailed(Exception e) {
Log.e(TAG, "onFailed: " + e.getMessage());
}
public void onFailed(Exception e) {
Log.e(TAG, "onFailed: " + e.getMessage());
}
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detachView();
}
}
}
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detachView();
}
}
}