zoukankan      html  css  js  c++  java
  • Android_Refrogit与RxJava结合使用(转)

    Refrogit与RxJava结合的使用    达到了非常简单就可以完成请求网络

    一:1.0示例:

    1.导入依赖

    compile 'io.reactivex:rxjava:1.3.4'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.squareup.retrofit2:retrofit:2.0.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'//解析
    compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'//结合使用必加

    2:接口
    返回的不是Call对象,而是RxJava里面的 ‘被观察者对象’

    public interface ApiService {
    @GET("product/getCatagory")
    Observable<Bean> get();
    }

    3:使用
    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://120.27.23.105/")
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .addConverterFactory(GsonConverterFactory.create())
    .build();
    ApiService apiService = retrofit.create(ApiService.class);
    Observable<Bean> beanObservable = apiService.get();
    beanObservable.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<Bean>() {
    @Override
    public void call(Bean bean) {
    List<Bean.DataBean> data = bean.getData();
    Log.i("TAG",data.size()+"");
    }
    });
    二:2.0示例:
    1:依赖

    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'

    2:接口
    public interface ApiService {
    @GET("product/getCatagory")
    Flowable<Bean> get();
    }


    3:使用

    package com.example.rxjava2;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import io.reactivex.android.schedulers.AndroidSchedulers;
    import io.reactivex.schedulers.Schedulers;
    import io.reactivex.subscribers.DisposableSubscriber;
    import retrofit2.Retrofit;
    import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
    import retrofit2.converter.gson.GsonConverterFactory;
    public class MainActivity extends AppCompatActivity {

    private DisposableSubscriber<Bean> disposableSubscriber;

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

    Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://120.27.23.105/")
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .addConverterFactory(GsonConverterFactory.create())
    .build();
    ApiService apiService = retrofit.create(ApiService.class);
    disposableSubscriber = apiService.get()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeWith(new DisposableSubscriber<Bean>() {
    @Override
    public void onNext(Bean bean) {
    Log.i("TAG",bean.getData().size()+"");
    }

    @Override
    public void onError(Throwable t) {

    }

    @Override
    public void onComplete() {

    }
    });
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    //防止内存泄漏
    if(disposableSubscriber!=null){
    if(!disposableSubscriber.isDisposed()){
    disposableSubscriber.dispose();
    }
    }
    }
    }

    ---------------------
    作者:绅V科技
    来源:CSDN
    原文:https://blog.csdn.net/android___vv/article/details/78740129
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    PAT 1010. 一元多项式求导 (25)
    PAT 1009. 说反话 (20) JAVA
    PAT 1009. 说反话 (20)
    PAT 1007. 素数对猜想 (20)
    POJ 2752 Seek the Name, Seek the Fame KMP
    POJ 2406 Power Strings KMP
    ZOJ3811 Untrusted Patrol
    Codeforces Round #265 (Div. 2) 题解
    Topcoder SRM632 DIV2 解题报告
    Topcoder SRM631 DIV2 解题报告
  • 原文地址:https://www.cnblogs.com/weizhxa/p/9875690.html
Copyright © 2011-2022 走看看