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
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/weizhxa/p/9875690.html