MVP:高内聚,低耦合
一:导入依赖
compile 'io.reactivex.rxjava2:rxjava:2.1.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'//解析使用
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'//与RxJava结合时使用
二:请求网络用Retrofit
1:接口
public interface ApiService {
//http://120.27.23.105/product/getProductCatagory?cid=1
@GET("product/getProductCatagory")
Flowable<Bean> get(@QueryMap Map<String,String> map);
}
package com.example.rxretrofitmvp.utils;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by Administrator on 2017/12/7 0007.
*/
public class RetrofitUtils {
private static volatile RetrofitUtils instance;
private final Retrofit retrofit;
private RetrofitUtils(String baseurl) {
retrofit = new Retrofit.Builder()
.baseUrl(baseurl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static RetrofitUtils getInstance(String baseurl) {
if (instance == null) {
synchronized (RetrofitUtils.class) {
if (instance == null) {
instance = new RetrofitUtils(baseurl);
}
}
}
return instance;
}
public Retrofit getretrofit(){
return retrofit;
}
}
三:Model层
1:接口
public interface IModel {
void get(String baseurl,Map<String,String> map);
}
2:类
package com.example.rxretrofitmvp.model;
import com.example.rxretrofitmvp.bean.Bean;
import com.example.rxretrofitmvp.presenter.Presenter;
import com.example.rxretrofitmvp.utils.ApiService;
import com.example.rxretrofitmvp.utils.RetrofitUtils;
import java.util.Map;
import io.reactivex.Flowable;
/**
* Created by Administrator on 2017/12/7 0007.
*/
public class Model implements IModel{
private Presenter presenter;
public Model(Presenter presenter) {
this.presenter = presenter;
}
@Override
public void get(String baseurl, Map<String, String> map) {
Flowable<Bean> flowable = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class).get(map);
presenter.getData(flowable);
}
}
四:Presenter层
1:接口
public interface BasePresenter {
void get(String baseurl,Map<String,String> map);
}
2:类
package com.example.rxretrofitmvp.presenter;
import com.example.rxretrofitmvp.model.Model;
import com.example.rxretrofitmvp.view.IView;
import java.util.Map;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;
/**
* Created by Administrator on 2017/12/7 0007.
*/
public class Presenter implements BasePresenter{
private IView iv;
private DisposableSubscriber subscriber;
public Presenter(IView iv) {
this.iv = iv;
}
@Override
public void get(String baseurl,Map<String, String> map) {
Model model = new Model(this);
model.get(baseurl,map);
}
public void getData(Flowable flowable){
subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSubscriber() {
@Override
public void onNext(Object o) {
iv.onSuccess(o);
}
@Override
public void onError(Throwable t) {
iv.onFailed((Exception) t);
}
@Override
public void onComplete() {
}
});
}
//防止内存泄漏
public void detatch(){
if (iv != null) {
iv = null;
}
if(subscriber!=null){
if(!subscriber.isDisposed()){
subscriber.dispose();
}
}
}
}
五:View层
1:接口
public interface IView {
void onSuccess(Object o);
void onFailed(Exception e);
}
2:类
package com.example.rxretrofitmvp.view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.example.rxretrofitmvp.R;
import com.example.rxretrofitmvp.bean.Bean;
import com.example.rxretrofitmvp.presenter.Presenter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements IView{
private Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//http://120.27.23.105/product/getProductCatagory?cid=1
presenter = new Presenter(this);
Map<String,String> map = new HashMap<>();
map.put("cid","1");
presenter.get("http://120.27.23.105/",map);
}
@Override
public void onSuccess(Object o) {
Bean b = (Bean)o;
List<Bean.DataBean> data = b.getData();
Log.i("TAG",data.size()+"");
}
@Override
public void onFailed(Exception e) {
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detatch();
}
}