public class RetyofitUitl { private static RetyofitUitl retyofitUitl; private RetyofitUitl(){ } public static RetyofitUitl getRetyofitUitl (){ if (retyofitUitl==null){ synchronized (RetyofitUitl.class){ if (retyofitUitl==null){ retyofitUitl=new RetyofitUitl(); } } } } public Resreivce getPost(){
//日志拦截 拦截器 OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)).build(); Retrofit retrofit=new Retrofit.Builder().baseUrl("https://www.zhaoapi.cn/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(okHttpClient) .build(); Resreivce resreivce = retrofit.create(Resreivce.class); return resreivce; } class MyInterceptor implements Interceptor { private FormBody.Builder formBody; private Request build1; @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); RequestBody body = request.body(); if (body instanceof FormBody) { formBody = new FormBody.Builder(); for (int i = 0; i < ((FormBody) body). size(); i++) { String name = ((FormBody) body).name(i); String value = ((FormBody) body).value(i); formBody.add(name, value); } formBody.add("token", "1"); formBody.add("source", "android"); formBody.add("appVersion", "101"); FormBody build = formBody.build(); build1 = request.newBuilder().post(build).build(); } Response proceed = chain.proceed(build1); return proceed; } } }