RxJava与RetroFit的封装

1.在Build.gradler文件中搭建环境
 2.自定义RetroFit网络请求接口APISever
 3.对RetroFit网络进行封装RetfitFactory
 4.使用适配器设计模式,简化RetroFit请求代码,BaseObserver

 5.完善

依赖文件
    /*RxJava与RxAndroid的依赖*/
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.1.0'

    /*Retrofit与RxAndroid配置依赖*/
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

    /*使Retrofit的转化器自定义完成数据的转化
    * 就是我们可以接口中少写一些bean类
    * */
    compile 'com.squareup.retrofit2:converter-scalars:+'
    /** 日至拦截器 */
    compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    /*gson解析*/

    compile 'com.google.code.gson:gson:2.8.1'


---------------------------创建文件夹 创建三个文件:ApiServer.java 接口  BaseObserver.java 

RetrofitFactory.java-------------

package com.example.john.myquarter.network;

import java.util.Map;

import io.reactivex.Observable;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;


/**
 * Created by John on 2018/7/8 0008.
 */

public interface ApiService {
    /**
     * Get基本请求,这里从Call改为Observable被观察者
     * @param url
     * @return
     */
    @GET
    public Observable get(@Url String url);

    /**
     * Get请求提交表单
     * @param url
     * @param map
     * @return
     */
    @GET
    public Observable get(@Url String url, @QueryMap Map map);

    /**
     * Post请求提交表单
     * @param url
     * @param map
     * @return
     */
    @POST
    public Observable post(@Url String url, @QueryMap Map map);


}

---------------------------------------------------------------------------------

package com.example.john.myquarter.network;

import android.util.Log;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by John on 2018/7/8 0008.
 */

public abstract class BaseObserver implements Observer {
    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(String s) {
        onSuccess(s);//调用请求成功的方法
    }
    //自己封装失败的回调
    @Override
    public void onError(Throwable e) {
        onFailed();//调用请求失败的方法
    }

    @Override
    public void onComplete() {

    }

    public abstract void onSuccess(String result);

    public abstract void onFailed();
}

-------------------------------------------------------------------------------------

package com.example.john.myquarter.network;

import android.util.Log;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Created by John on 2018/7/8 0008.
 */

public class RetrofitFactory {
    //使全局就一个OkHttpClient对象
    public static OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10,TimeUnit.SECONDS)
            .connectTimeout(10,TimeUnit.SECONDS)
            .build();
    //使全局就一个Retrofit对象,设置基础Url
    public static ApiService apiService = new Retrofit.Builder()
            .baseUrl("http://qbh.2dyt.com")
            //使我们能高度自定义转化器
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(client)
            //把 以前的 call 转化成 Observable,这是Retrofit与RxJava结合使用的关键
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build().create(ApiService.class);

    public static Observable get(String url) {
        return apiService.get(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public static Observable get(String url, Map map){
        return apiService.get(url,map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public static Observable post(String url, Map map) {
        return apiService.post(url, map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}

你可能感兴趣的:(RxJava与RetroFit的封装)