Retrofit2+RxJava+OkHttp网络请求封装使用+Retrofit注解

1.纯属方便自己使用

2.将网络请求的业务代码和UI展示的代码分离,用时就调用。

3.接口请求复用。 

依赖:

    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

    implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'

    implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0-RC3'
    implementation 'io.reactivex.rxjava2:rxjava:2.0.0-RC3'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.0-RC1'

封装的工具类:

/**
     * 单例模式 懒汉
     */
    private static RetrofitUtils retrofitUtils;

    private RetrofitUtils() {

    }

    public static RetrofitUtils getInstance() {
        if (retrofitUtils == null) {
            synchronized (RetrofitUtils.class) {
                if (retrofitUtils == null) {
                    retrofitUtils = new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }

    /**
     * 获取OkHttp
     */
    private static OkHttpClient okHttpClient;

    private static synchronized OkHttpClient getOkHttpClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("lj", "log: " + message);
            }
        });
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
                .connectTimeout(3000, TimeUnit.SECONDS)
                .readTimeout(3000, TimeUnit.SECONDS)
                .build();
        return okHttpClient;
    }

    /**
     * 获取Retrofit
     */
    private static Retrofit retrofit;

    private static synchronized Retrofit getRetrofit() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(getOkHttpClient())
                .build();
        return retrofit;
    }

    /**
     * 获取动态代理对象
     */

    public  T setCreate(Class apiService) {
        Retrofit retrofit = getRetrofit();
        return retrofit.create(apiService);
    }

 接口:

public class Api {
    public static final String BASE_URL = "http://gank.io/api/data/";
    public static final String MAIN_URL = "%E7%A6%8F%E5%88%A9/10/";
}

动态代理对象 (修改了几个常用注解)

public interface ApiService {

    @GET(Api.MAIN_URL + "{page}")
    Flowable getMain(@Path("page") int page);

    
    @POST(Api.LOGIN_URL)
    @FormUrlEncoded//表单格式
    Flowable login(@FieldMap Map params);


    @Multipart
    @PUT(Api.SYNC_SHOPCAR)
    Flowable syncCarts(@HeaderMap Map map, @Part("data") RequestBody data);

    @GET(Api.SEARCH_URL)
    Flowable getSearch(@Query("keyword") String keyword);

}

实现网络请求: 

 ApiService apiService = RetrofitUtils.getInstance().setCreate(ApiService.class);
        Flowable flowable = apiService.getMain(1);
        flowable.subscribeOn(Schedulers.io())//子线程
        .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(MyBean myBean) {
                        Log.i("xxx", "onNext: "+myBean.getResults().size());
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

RxJava:https://www.jianshu.com/p/0cd258eecf60 

Retrofit2+RxJava+OkHttp网络请求封装使用+Retrofit注解_第1张图片

 

你可能感兴趣的:(Android)