RecyofitUtil











public interface ReService {

    @POST("product/getCatagory")
    @FormUrlEncoded
    Observable<LefteBase> getShuJu(@FieldMap Map<String,String>map);


}




public class Constants {

    public static final String LU_JIN="https://www.zhaoapi.cn/";
}







private static RetrofitUtil retrofitUtil;
private RetrofitUtil(){

}
public RetrofitUtil getRetrofitUtil(){
    if (retrofitUtil==null){
        synchronized (RetrofitUtil.class){
            if (retrofitUtil==null){
                retrofitUtil=new RetrofitUtil();
            }
        }
    }
    return retrofitUtil;
}

public ReSrevice getPost(){
    //日志拦截器
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
          .addInterceptor(new Myin()).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(Content.URL_L)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();
    ReSrevice reSrevice = retrofit.create(ReSrevice.class);
    return  reSrevice;
}



//拦截器
class  Myin implements Interceptor {
    private FormBody.Builder builder;
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        RequestBody body = request.body();
        if (body instanceof FormBody){
            builder = new FormBody.Builder();
            for(int i=0; i<((FormBody) body).size();i++){
                String name = ((FormBody) body).name(i);
                String value = ((FormBody) body).value(i);
                builder.add(name,value);
            }
        }
        builder.add("source","android");
        FormBody build = builder.build();
        Request build1 = request.newBuilder().post(build).build();
        Response proceed = chain.proceed(build1);
        return proceed;


    }
}
            
阅读更多

没有更多推荐了,返回首页