| | |
| | | |
| | | |
| | | import com.runt.open.mvvm.BuildConfig; |
| | | import com.runt.open.mvvm.retrofit.Interceptor.EncryptInterceptor; |
| | | import com.runt.open.mvvm.retrofit.Interceptor.AddHeadersInterceptor; |
| | | import com.runt.open.mvvm.retrofit.Interceptor.HttpLoggingInterceptor; |
| | | import com.runt.open.mvvm.retrofit.api.CommonApiCenter; |
| | | import com.runt.open.mvvm.retrofit.converter.DecryptGsonConverterFactory; |
| | | import com.runt.open.mvvm.retrofit.converter.GsonConverterFactory; |
| | | import com.runt.open.mvvm.retrofit.net.NetWorkListenear; |
| | | |
| | | import java.util.Collections; |
| | |
| | | */ |
| | | |
| | | public class RetrofitUtils { |
| | | public static String HOST_IP_ADDR; |
| | | static RetrofitUtils instance; |
| | | Retrofit retrofit/*log输出,驼峰转换*/,unHumpRetrofit/*log输出,不强制驼峰转换*/, |
| | | unLogRetrofit/*log不输出,驼峰转换*/,unLogHumpRetorfit/*log不输出,不强制驼峰转换*/; |
| | | CommonApiCenter commonApi;//常用接口 |
| | | |
| | | OkHttpClient.Builder builder = new OkHttpClient.Builder() |
| | | .addInterceptor(new EncryptInterceptor()); |
| | | .addInterceptor(new AddHeadersInterceptor()); |
| | | OkHttpClient.Builder logBuilder = new OkHttpClient.Builder() |
| | | .addInterceptor(new HttpLoggingInterceptor());//log打印拦截器 |
| | | |
| | |
| | | public <T> T getRetrofit(Class<T> clas) { |
| | | if(retrofit == null){ |
| | | retrofit = getRetrofit(getOkHttpClient(logBuilder), |
| | | new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create(true))) ; |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(true))) ; |
| | | } |
| | | if(!BuildConfig.DEBUG){//正式版 不打印log |
| | | return getUnLogRetrofit(clas); |
| | |
| | | public <T> T getUnHumpRetrofit(Class<T> clas) { |
| | | if(unHumpRetrofit == null){ |
| | | unHumpRetrofit = getRetrofit(getOkHttpClient(logBuilder), |
| | | new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create())) ; |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())) ; |
| | | } |
| | | if(!BuildConfig.DEBUG){//正式版 不打印log |
| | | return getUnLogHumpRetorfit(clas); |
| | |
| | | public <T> T getUnLogRetrofit(Class<T> clas) { |
| | | if(unLogRetrofit == null){ |
| | | unLogRetrofit = getRetrofit(getOkHttpClient(builder), |
| | | new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create(true))) ; |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create(true))) ; |
| | | } |
| | | return unLogRetrofit.create(clas); |
| | | } |
| | |
| | | public <T> T getUnLogHumpRetorfit(Class<T> clas) { |
| | | if(unLogHumpRetorfit == null){ |
| | | unLogHumpRetorfit = getRetrofit(getOkHttpClient(builder), |
| | | new Retrofit.Builder().addConverterFactory(DecryptGsonConverterFactory.create())) ; |
| | | new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())) ; |
| | | } |
| | | return unLogHumpRetorfit.create(clas); |
| | | } |
| | |
| | | //设置OKHttpClient |
| | | .client(client) |
| | | //设置baseUrl,注意,baseUrl必须后缀"/" |
| | | .baseUrl(BuildConfig.ENVIRONMENT.equals("develop")?HOST_IP_ADDR:BuildConfig.HOST_IP_ADDR) |
| | | .baseUrl(BuildConfig.HOST_IP_ADDR+"api/v2/") |
| | | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
| | | .build(); |
| | | } |