Retrofit最佳实现
话不多说,直接上代码
1.Rretrofit 封装
public class ServiceGenerator {
//服务器地址
private static final String BASE_URL = "";
//retrofit builder
private static Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
//retrofit object
private static Retrofit retrofit = builder.build();
//log interceptor 打印网络请求日志
private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
private static OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
//service generator 供外部调用
public static <S> S createService(Class<S> serviceClass){
//未添加拦截器 且 处于开发模式
if (!httpClientBuilder.interceptors().contains(loggingInterceptor) && BuildConfig.DEBUG)
{
httpClientBuilder.addInterceptor(loggingInterceptor);
builder = builder.client(httpClientBuilder.build());
retrofit = builder.build();
}
return retrofit.create(serviceClass);
}
}
2.具体调用
RequestService service = ServiceGenerator.createService(RequestService.class);
service.listDevices().enqueue(new Callback<BaseResponse<List<Device>>>() {
@Override
public void onResponse(Call<BaseResponse<List<Device>>> call, Response<BaseResponse<List<Device>>> response) {
}
@Override
public void onFailure(Call<BaseResponse<List<Device>>> call, Throwable t) {
}
});