简单的mvp+retrofit+rxjava示例

最近喜欢上了mvp,所以也学习了网站上别人的mvp自己写了一个简易的mvp,我不太确定我这是不是标准的mvp,但我感觉分离的挺开的。
这里的mvp我是结合了retrofit+rxjava,全是简易的,因为都是刚开始学习,希望大家多多指点。
其实在我看来mvp就是将v层和m层完全分离开来,通过p层去控制v层和m层(也就是在p层中调用v层或m层的方法)下面我直接将代码贴出来吧,很容易理解的。
v层:

public class MainActivity extends AppCompatActivity implements MainActivityInterface {

    @Bind(R.id.click_me_BN)
    Button clickMeBN;
    @Bind(R.id.result_tv)
    TextView resultTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.click_me_BN)
    public void onClick() {
        MainPresent mainPresent = new MainPresent(this);
        mainPresent.loadData();
    }



    @Override
    public void showData(MovieEntity movieEntity) {
        resultTv.setText(movieEntity.getTitle());
    }

    @Override
    public void showError() {

    }
}
public interface MainActivityInterface {
    void showData(MovieEntity movieEntity);

}

p层:

public class MainPresent implements MainPresentInterface,MainModel.OngetResult {

    private final MainModel mMainModel;
    public MainActivityInterface mActivityInterface;


    public MainPresent(MainActivityInterface activityInterface) {
        getInterfaceView(activityInterface);
        mMainModel = new MainModel(this);
    }


    @Override
    public void addViewData(MovieEntity movieEntity) {
        mActivityInterface.showData(movieEntity);
    }

    @Override
    public void getInterfaceView(MainActivityInterface mainActivityInterface) {
        this.mActivityInterface = mainActivityInterface;
    }

    @Override
    public void loadData() {
        mMainModel.loadData();
    }


    @Override
    public void getData(MovieEntity movieEntity) {
        //成功获取数据,取消进度条
        addViewData(movieEntity);

    }

    @Override
    public void error(Throwable e) {
        //失败获取数据,取消进度条并进行其他操作

    }
}
public interface MainPresentInterface {

    void addViewData(MovieEntity movieEntity);

    void getInterfaceView(MainActivityInterface mainActivityInterface);

    void loadData();

}

M层:

public class MainModel implements MainModelInterface {
    private MainPresent mMainPresent;

    private OngetResult mOngetResult;

    public interface OngetResult {

        void getData(MovieEntity movieEntity);

        void error(Throwable e);
    }

    public MainModel(MainPresent mainPresent) {
        this.mMainPresent = mainPresent;
    }

    /**
     * 进行网络请求
     * https://api.douban.com/v2/movie/top250?start=0&count=10
     */
    @Override
    public void loadData() {
        String baseUrl = "https://api.douban.com/v2/movie/";

                ApiManager.getWriData(baseUrl,0,10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MovieEntity>() {
                               @Override
                               public void onCompleted() {
                                   //成功

                               }

                               @Override
                               public void onError(Throwable e) {
                                   //出错
                                   mOngetResult.error(e);
                               }

                               @Override
                               public void onNext(MovieEntity movieEntity) {
                                   //拿数据
                                   mOngetResult.getData(movieEntity);
                               }
                           }
                );

    }
}
public interface MainModelInterface {
    void loadData();
}

APImanager:

public class ApiManager {


    private static String baseUrl;
    private static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
   private static MovieService movieService = retrofit.create(MovieService.class);


    public static Observable<MovieEntity> getWriData(String url, int start, int count) {
        baseUrl = url;
        return movieService.getTopMovie(start, count);
    }
}

MovieService :

public interface MovieService {

    @GET("top250")
    Observable<MovieEntity> getTopMovie(@Query("start") int start, @Query("count") int count);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值