#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);