package com.ht.gate; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; public class RetrofitManager { // private final String BASE_URL = "http://localhost:8080/"; private final String BASE_URL = "http://screen.crm.htsc:30000/"; private static RetrofitManager sInstance; private Retrofit mRetrofit; public static RetrofitManager getInstance() { if (null == sInstance) { synchronized (RetrofitManager.class) { if (null == sInstance) { sInstance = new RetrofitManager(); } } } return sInstance; } public void init() { if (mRetrofit == null) { //初始化一个OkHttpClient OkHttpClient.Builder builder = new OkHttpClient.Builder() .connectTimeout(30000, TimeUnit.MILLISECONDS) .readTimeout(30000, TimeUnit.MILLISECONDS) .writeTimeout(30000, TimeUnit.MILLISECONDS); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(loggingInterceptor); OkHttpClient okHttpClient = builder.build(); Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); //使用该OkHttpClient创建一个Retrofit对象 mRetrofit = new Retrofit.Builder() //添加Gson数据格式转换器支持 .addConverterFactory(GsonConverterFactory.create(gson)) //添加RxJava语言支持 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //指定网络请求client .client(okHttpClient) .baseUrl(BASE_URL) .build(); } } public Retrofit getRetrofit() { if (mRetrofit == null) { throw new IllegalStateException("Retrofit instance hasn't init!"); } return mRetrofit; } }