| | |
| | | |
| | | |
| | | import com.runt.open.mvi.BuildConfig; |
| | | import com.runt.open.mvi.retrofit.Interceptor.AddHeadersInterceptor; |
| | | import com.runt.open.mvi.retrofit.Interceptor.LogEncryptInterceptor; |
| | | import com.runt.open.mvi.retrofit.Interceptor.OpenInterceptor; |
| | | import com.runt.open.mvi.retrofit.Interceptor.EncryptInterceptor; |
| | | import com.runt.open.mvi.retrofit.Interceptor.HttpLoggingInterceptor; |
| | | import com.runt.open.mvi.retrofit.converter.DecryptGsonConverterFactory; |
| | | import com.runt.open.mvi.retrofit.converter.GsonConverterFactory; |
| | | import com.runt.open.mvi.retrofit.net.NetWorkListener; |
| | | |
| | | import java.util.Collections; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import okhttp3.Interceptor; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Protocol; |
| | | import retrofit2.Retrofit; |
| | |
| | | |
| | | public class RetrofitUtils { |
| | | static RetrofitUtils instance; |
| | | Retrofit retrofit/*log输出,驼峰转换*/,unHumpRetrofit/*log输出,不强制驼峰转换*/, |
| | | unLogRetrofit/*log不输出,驼峰转换*/,unLogHumpRetorfit/*log不输出,不强制驼峰转换*/; |
| | | Retrofit retrofit/*log输出*/,humpRetrofit/*log输出,强制驼峰转换*/, |
| | | unLogRetrofit/*log不输出*/,unLogHumpRetrofit/*log不输出,强制驼峰转换*/, |
| | | encryptRetrofit/*加密log输出*/,humpEncryptRetrofit/*加密log输出,强制驼峰转换*/, |
| | | unLogEncryptRetrofit/*加密log不输出*/,unLogHumpEncryptRetrofit/*加密log不输出,强制驼峰转换*/; |
| | | |
| | | OkHttpClient.Builder builder = new OkHttpClient.Builder() |
| | | .addInterceptor(new AddHeadersInterceptor()); |
| | | OkHttpClient.Builder logBuilder = new OkHttpClient.Builder() |
| | | .addInterceptor(new HttpLoggingInterceptor());//log打印拦截器 |
| | | |
| | | public static RetrofitUtils getInstance() { |
| | | if(instance == null){ |
| | |
| | | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
| | | .build(); |
| | | } |
| | | |
| | | public <T> T getTempRetrofit(Class<T> clas,String url) { |
| | | Interceptor interceptor = BuildConfig.DEBUG ? new HttpLoggingInterceptor() : new OpenInterceptor(); |
| | | return getRetrofit(getOkHttpClient(new OkHttpClient.Builder().addInterceptor(interceptor)), |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(true)),url).create(clas); |
| | | } |
| | | |
| | | /** |
| | | * log输出,gson驼峰转换 |
| | | * log输出 |
| | | * @return |
| | | */ |
| | | public <T> T getRetrofit(Class<T> clas) { |
| | | if(retrofit == null){ |
| | | retrofit = getRetrofit(getOkHttpClient(logBuilder), |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(true))) ; |
| | | } |
| | | if(!BuildConfig.DEBUG){//正式版 不打印log |
| | | return getUnLogRetrofit(clas); |
| | | } |
| | | return retrofit.create(clas); |
| | | } |
| | | |
| | | public <T> T getTempRetrofit(Class<T> clas,String url) { |
| | | return getRetrofit(getOkHttpClient(new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor(BuildConfig.DEBUG))), |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(true)),url).create(clas); |
| | | } |
| | | /** |
| | | * log输出,gson不转换驼峰 |
| | | * @return |
| | | */ |
| | | public <T> T getUnHumpRetrofit(Class<T> clas) { |
| | | if(unHumpRetrofit == null){ |
| | | unHumpRetrofit = getRetrofit(getOkHttpClient(logBuilder), |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())) ; |
| | | } |
| | | if(!BuildConfig.DEBUG){//正式版 不打印log |
| | | return getUnLogHumpRetorfit(clas); |
| | | } |
| | | return unHumpRetrofit.create(clas); |
| | | return getRetrofit(clas,false,false,true); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | public <T> T getUnLogRetrofit(Class<T> clas) { |
| | | if(unLogRetrofit == null){ |
| | | unLogRetrofit = getRetrofit(getOkHttpClient(builder), |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(true))) ; |
| | | } |
| | | return unLogRetrofit.create(clas); |
| | | return getRetrofit(clas,false,false,false); |
| | | } |
| | | |
| | | /** |
| | | * log不输出,gson不转换驼峰 |
| | | * log输出,gson转换驼峰 |
| | | * @return |
| | | */ |
| | | public <T> T getHumpRetrofit(Class<T> clas) { |
| | | if(!BuildConfig.DEBUG){//正式版 不打印log |
| | | return getUnLogHumpRetorfit(clas); |
| | | } |
| | | return getRetrofit(clas,true,false,true); |
| | | } |
| | | |
| | | /** |
| | | * log不输出,gson转换驼峰 |
| | | * @return |
| | | */ |
| | | public <T> T getUnLogHumpRetorfit(Class<T> clas) { |
| | | if(unLogHumpRetorfit == null){ |
| | | unLogHumpRetorfit = getRetrofit(getOkHttpClient(builder), |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())) ; |
| | | return getRetrofit(clas,true,false,false); |
| | | } |
| | | |
| | | /** |
| | | * 加密log输出 |
| | | * @return |
| | | */ |
| | | public <T> T getEncrptRetrofit(Class<T> clas) { |
| | | if(!BuildConfig.DEBUG){//正式版 不打印log |
| | | return getUnLogEncryptRetrofit(clas); |
| | | } |
| | | return unLogHumpRetorfit.create(clas); |
| | | return getRetrofit(clas,false,true,true); |
| | | } |
| | | |
| | | /** |
| | | * 加密log不输出 |
| | | * @return |
| | | */ |
| | | public <T> T getUnLogEncryptRetrofit(Class<T> clas) { |
| | | return getRetrofit(clas,false,true,false); |
| | | } |
| | | |
| | | /** |
| | | * 加密log输出 gson转换驼峰 |
| | | * @return |
| | | */ |
| | | public <T> T getHumpEncryptRetrofit(Class<T> clas) { |
| | | if(!BuildConfig.DEBUG){//正式版 不打印log |
| | | return getUnLogHumpEncryptRetrofit(clas); |
| | | } |
| | | return getRetrofit(clas,true,true,true); |
| | | } |
| | | |
| | | /** |
| | | * 加密log不输出 gson转换驼峰 |
| | | * @return |
| | | */ |
| | | public <T> T getUnLogHumpEncryptRetrofit(Class<T> clas) { |
| | | return getRetrofit(clas,true,true,false); |
| | | } |
| | | |
| | | private <T> T getRetrofit(Class<T> clzz,boolean transHump,boolean encrypt,boolean log){ |
| | | Retrofit temp = null; |
| | | if(transHump && encrypt && log){ |
| | | if(humpEncryptRetrofit == null){ |
| | | humpEncryptRetrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = humpEncryptRetrofit; |
| | | }else if(transHump && encrypt && !log ){ |
| | | if(unLogHumpEncryptRetrofit == null){ |
| | | unLogHumpEncryptRetrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = unLogHumpEncryptRetrofit; |
| | | }else if(transHump && !encrypt && !log ){ |
| | | if(unLogHumpRetrofit == null){ |
| | | unLogHumpRetrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = unLogHumpRetrofit; |
| | | }else if(!transHump && !encrypt && !log ){ |
| | | if(unLogRetrofit == null){ |
| | | unLogRetrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = unLogRetrofit; |
| | | }else if(!transHump && !encrypt && log ){ |
| | | if(retrofit == null){ |
| | | retrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = retrofit; |
| | | }else if(!transHump && encrypt && log ){ |
| | | if(encryptRetrofit == null){ |
| | | encryptRetrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = encryptRetrofit; |
| | | }else if(!transHump && encrypt && !log ){ |
| | | if(unLogEncryptRetrofit == null){ |
| | | unLogEncryptRetrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = unLogEncryptRetrofit; |
| | | }else if(transHump && !encrypt && log ){ |
| | | if(humpRetrofit == null){ |
| | | humpRetrofit = initRetrofit(transHump,encrypt,log); |
| | | } |
| | | temp = humpRetrofit; |
| | | } |
| | | return temp.create(clzz); |
| | | } |
| | | |
| | | private Retrofit initRetrofit(boolean transHump,boolean encrypt,boolean log){ |
| | | Retrofit.Builder builder = new Retrofit.Builder(); |
| | | builder.addConverterFactory(encrypt ? DecryptGsonConverterFactory.create(transHump) : GsonConverterFactory.create(transHump)); |
| | | |
| | | OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder(); |
| | | if(log && encrypt){ |
| | | httpBuilder.addInterceptor(new LogEncryptInterceptor());//log打印加密拦截器 |
| | | } else if (log && !encrypt) { |
| | | httpBuilder.addInterceptor(new HttpLoggingInterceptor());//log打印拦截器 |
| | | } else if (!log && encrypt) { |
| | | httpBuilder.addInterceptor(new EncryptInterceptor());//加密拦截器 |
| | | } else if (!log && !encrypt) { |
| | | httpBuilder.addInterceptor(new OpenInterceptor());//拦截器 |
| | | } |
| | | OkHttpClient httpClient = getOkHttpClient(httpBuilder); |
| | | return getRetrofit(httpClient,builder); |
| | | } |
| | | |
| | | private OkHttpClient getOkHttpClient(OkHttpClient.Builder builder){ |