package com.example.tianwang20181010.utils; import com.example.tianwang20181010.api.Api; import com.example.tianwang20181010.constants.Constant; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; public class HttpUtils { public final Api api; private HttpUtils(){ OkHttpClient okHttpClient = new OkHttpClient.Builder() .addNetworkInterceptor(new LoggingInterceptor()).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constant.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(okHttpClient) .build(); api = retrofit.create(Api.class); } class LoggingInterceptor implements Interceptor{ @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); Response response = chain.proceed(request); long t2 = System.nanoTime(); return response; } } private static class GetHttpUtilsInstance{ private static HttpUtils httpUtils = new HttpUtils(); } public static HttpUtils getHttpUtilsIntance(){ return GetHttpUtilsInstance.httpUtils; } }