retrofit+rxjava model pop

#retrofitutil

public class RetrofitUtil {
    private static RetrofitUtil instance;
    private final OkHttpClient okHttpClient;
    private final Retrofit retrofit;

    private RetrofitUtil() {

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient = new OkHttpClient.Builder()
                .readTimeout(10,TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .connectTimeout(10,TimeUnit.SECONDS)
                .addInterceptor(httpLoggingInterceptor)
                /*.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request();
                        SharedPreferences preferences = BaseApp.getContext().getSharedPreferences("logincart", Context.MODE_PRIVATE);
                        String userId = preferences.getString("userId", "");
                        String sessionId = preferences.getString("sessionId", "");
                        Request.Builder builder = request.newBuilder();
                        builder.method(request.method(),request.body());
                        if (!TextUtils.isEmpty(userId)&&!TextUtils.isEmpty(sessionId)){
                            builder.addHeader("userId",userId);
                            builder.addHeader("sessionId",sessionId);
                            Log.d("userId",userId);
                            Log.d("seeeionId",sessionId);
                        }
                        Request build = builder.build();
                        return chain.proceed(build);
                    }
                })//添加日志拦截器*/
                .retryOnConnectionFailure(true)
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient)
                .build();

    }

    public ApiService setRetrofit(){
        return retrofit.create(ApiService.class);
    }

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

#model

 @Override
    public void getAddCart(HashMap<String, String> map, HashMap<String, String> map1, final RetrofitRequestCallback callback) {
        ApiService apiService = RetrofitUtil.getInstance().setRetrofit();
        apiService.getAddCart(CartApi.ADDCART_URL,map,map1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<AddCartBean>() {
                    @Override
                    public void accept(AddCartBean addCartBean) throws Exception {
                        callback.SuccessAddCart(addCartBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        callback.FailAddCart("请求错误");
                    }
                });

#pop

View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.pop_layout, null);
                re1 = inflate.findViewById(R.id.re1);
                re2 = inflate.findViewById(R.id.re2);
                PopupWindow popupWindow = new PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setContentView(inflate);
                popupWindow.setBackgroundDrawable(new ColorDrawable(Color.GRAY));
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAsDropDown(inflate,50,50);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值