RetrofitUtils封装

 /*
    * 拦截器
    * */
    public RetrofitUtils() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .readTimeout(5,TimeUnit.SECONDS)
                .connectTimeout(5,TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS)
                .build();
    }
//创建单列
    public static RetrofitUtils getInstance(){
        if (retrofitUtils == null){
            synchronized (RetrofitUtils.class){
                if (retrofitUtils == null){
                    retrofitUtils = new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }
    /*
 /*
    * 创建实列
    * */
    public void getRetrofit(RetrofitCallBack retrofitCallBack){
        if (retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://mobile.bwstudent.com/")
                    .client(client)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        retrofitCallBack.getRetrofit(retrofit);
    }
    public interface RetrofitCallBack{
        void getRetrofit(Retrofit retrofit);
    }
}
 @GET("small/user/verify/v1/receiveAddressList")
   Observable<DizhiBean> dizhi (@Header("userId")int userId,
                                @Header("sessionId")String sessionId);


 @POST("small/user/verify/v1/setDefaultReceiveAddress")
    Observable<Add> moren (@Header("userId")int userId,
                           @Header("sessionId")String sessionId,
                           @Query("id") int id);



 @FormUrlEncoded
    @POST("small/order/verify/v1/createOrder")
    Observable<Add> add(@Header("userId")int userId,
                           @Header("sessionId")String sessionId,
                           @Field("orderInfo") String orderInfo,
                           @Field("totalPrice")double totalPrice,
                           @Field("addressId")int addressId);

   Observable<GouWuChe> gouWuCheObservable = resuit.shopping_trolley(userId, sessionId);
                gouWuCheObservable.subscribeOn(Schedulers.io())
                        .subscribeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<GouWuChe>() {
                            @Override
                            public void accept(GouWuChe gouWuChe) throws Exception {

                                shoppingTrolley.onSuccess(gouWuChe.getResult());
                            }
                        });

猜你喜欢

转载自blog.csdn.net/wangshuo_/article/details/88944641