Android_Retrofit+RxJava+MVP开发模式案例

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);
}


2:工具类

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();
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值