美文网首页
RXJava+Retrofit+Mvp搭建

RXJava+Retrofit+Mvp搭建

作者: 轻舟_TG | 来源:发表于2019-02-25 17:19 被阅读0次
    导入依赖
        implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
        implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    
        // Okhttp库
        implementation 'com.squareup.okhttp3:okhttp:3.1.2'
        // Retrofit库
        implementation 'com.squareup.retrofit2:retrofit:2.0.2'
        implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
        implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
    
    定义一个解析的bean对象
    public class TestBean {
        private int errno;
        private String errmsg;
        private data data;
    
        public int getErrno() {
            return errno;
        }
    
        public void setErrno(int errno) {
            this.errno = errno;
        }
    
        public String getErrmsg() {
            return errmsg;
        }
    
        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }
    
        public TestBean.data getData() {
            return data;
        }
    
        public void setData(TestBean.data data) {
            this.data = data;
        }
    
        public class data{
            String name;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
        }
    }
    
    定义一个接口类,用来设置网络请求方式Get/Post,请求接口,实体类,请求参数。
    public interface TestService {
        @POST("sign_machine/index/device_identity")
        Observable<TestBean> getData(@Query("device_uid") String device_uid);
    }
    
    定义一个Mvp中的model类
    /**
     * Created by Administrator on 2019/2/21.
     * Retrofit + RXJava
     */
    
    public class BaseModel {
        public TestService getTestService() {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(API.BASE_URL)//基础URL 建议以 / 结尾
                    .addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
    
            TestService service = retrofit.create(TestService.class);
    
            return service;
        }
    }
    
    定义一个Mvp中的presenter类,在类里面定义回调接口,回调数据给View层(包含Activity和xml布局)
    public class MainPresenter {
        private MainCallBack callBack;
        private BaseModel model;
    
        public MainPresenter(MainCallBack callBack) {
            this.callBack = callBack;
            model=new BaseModel();
        }
    
        public void getNetData(){
            model.getTestService().getData("请求参数")
                    .subscribeOn(Schedulers.io())//IO线程加载数据
                    .observeOn(AndroidSchedulers.mainThread())//主线程显示数据
                    .subscribe(new Observer<TestBean>() {
                        @Override
                        public void onSubscribe(Disposable d) {
    
                        }
    
                        @Override
                        public void onNext(TestBean testBean) {
                            callBack.getMessage(testBean);
                        }
    
                        @Override
                        public void onError(Throwable e) {
                            callBack.error();
                        }
    
                        @Override
                        public void onComplete() {
    
                        }
                    });
        }
    
    
        public interface MainCallBack {
    
            void getMessage(TestBean testBean);
            void error();
    
        }
    
    }
    
    测试
    public class MainActivity extends AppCompatActivity implements MainPresenter.MainCallBack {
    
        Button button;
    
        private MainPresenter mainPresenter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            mainPresenter = new MainPresenter(this);
    
    
            button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mainPresenter.getNetData();
                }
            });
    
        }
    
        @Override
        public void getMessage(TestBean testBean) {
            Toast.makeText(MainActivity.this, testBean.getData().getName(), Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void error() {
            Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
        }
    }
    

    相关文章

      网友评论

          本文标题:RXJava+Retrofit+Mvp搭建

          本文链接:https://www.haomeiwen.com/subject/fomgyqtx.html