RXjava2.0

    //依赖
    implementation 'com.squareup.retrofit2:retrofit:2.0.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.0-RC3'
    implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0-RC3'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.0-RC1'
    implementation 'com.squareup.okio:okio:1.5.0'
    implementation 'com.squareup.okhttp3:okhttp:3.2.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    //网路权限
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Api

//Api 
public class Api {
//http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1
    public static final String wealUrl ="http://gank.io/api/";
    public static final String jokeUrl ="http://api.laifudao.com/";


}

//ApiService 
public interface ApiService {

	//bean类gson生成
    @GET("data/%E7%A6%8F%E5%88%A9/10/{page}")
    Flowable<Weal> getWeal(@Path("page") int page);
	
    @GET("open/xiaohua.json")
    Flowable<List<JokeInfo>> getJoke();
}


MainActivity

public class MainActivity extends AppCompatActivity {

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

    //    getJoke();
    }

    private void getJoke() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.jokeUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);

        //通过retrofit创建被观察者Flowable

        Flowable<List<JokeInfo>> flowable = apiService.getJoke();
        flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())

                .subscribeWith(new DisposableSubscriber<List<JokeInfo>>() {
                    @Override
                    public void onNext(List<JokeInfo> jokeInfos) {
                        Log.i("xxx",jokeInfos.size()+"");
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getWeal() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.wealUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);

        //通过retrofit创建被观察者Flowable

        Flowable<Weal> flowable = apiService.getWeal(1);
        flowable.subscribeOn(Schedulers.io())//指定发送事件的线程,io线程
                .map(new Function<Weal, Weal>() {//一对一的转换
                    @Override
                    public Weal apply(Weal user) throws Exception {



                        return user;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//接收事件在主线程
                .subscribeWith(new DisposableSubscriber<Weal>() {

                    @Override
                    public void onNext(Weal user) {//接收成功的事件
                        Log.i("xxx",user.getResults().get(0).getWho());
                    }

                    @Override
                    public void onError(Throwable t) {//接收失败的事件
                        Log.i("xxx",t.toString());
                    }

                    @Override
                    public void onComplete() {//事件完成

                    }
                });

    }
}

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值