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



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值