天天看点

Retrofit+RxJava联网

Retrofit+RxJava联网

public class RetrofitUtils {
    private static final String  BASE_URL="http:www.baidu.com/";
    private volatile static RetrofitUtils mRetrofitUtils;
    private Retrofit mRetrofit;

    private RetrofitUtils(){

        initRetrofit();
    }

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


    private void initRetrofit() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                try {
                    String msg = URLDecoder.decode(message, "utf-8");
                    Log.i("OkHttpInterceptor-----", msg);

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    Log.i("OkHttpInterceptor-----", message);
                }
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        /**okhttp默认时间10秒 请求时间较长时,重新设置下  **/
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        OkHttpClient client = builder.build();
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(client)
                .build();
    }

    public <T> T getApiServier(Class<T> reqServer){
        return mRetrofit.create(reqServer);
    }
}
           

依赖

implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.14'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
           

用法

FinanceApiService.class
public interface FinanceApiService {
    //POST请求
    @FormUrlEncoded
    @POST("acc/incomeAccounting")
    Observable<ResultBean> incomeAccounting(@Field("incTime") String incTime, @Field("operator") String operator, @Field("paymentMethod") String paymentMethod,
                                            @Field("incomeReason ") String incomeReason , @Field("incMoney") int incMoney, @Field("paymentUnit") String paymentUnit,
                                            @Field("consNumber") String consNumber, @Field("note") String note);
    //GET请求
    @GET("acc/incomeAccounting")
    Observable<ResultBean> incomeAccounting2(@Query("incTime") String incTime, @Query("operator") String operator, @Query("paymentMethod") String paymentMethod,
                                             @Query("incomeReason ") String incomeReason , @Query("incMoney") int incMoney, @Query("paymentUnit") String paymentUnit,
                                             @Query("consNumber") String consNumber, @Query("note") String note);
}
           
使用方法
RetrofitUtils.getInstance().getApiServier(FinanceApiService.class)
                    .incomeAccounting(time,caozuoren,zhifufangshi,laiyuan,jine,fukuanfang,"HY123456",beizhu)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<ResultBean>() {
                        @Override
                        public void accept(ResultBean resultBean) throws Exception {
                           //请求成功
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            //请求失败
                        }
                    });
           

继续阅读