package com.example.aason.zhusha315lianxi.net;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ResfateUtils {
public static ResfateUtils mInstace;
private final Retrofit retrofit;
//单例
public static ResfateUtils getmInstace(){
if (mInstace == null) {
synchronized (ResfateUtils.class){
if (mInstace == null) {
mInstace = new ResfateUtils();
}
}
}
return mInstace;
}
//日志拦截器
public ResfateUtils(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addNetworkInterceptor(interceptor)
.readTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("https://www.wanandroid.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public <T> T create(Class<T> tClass){
return retrofit.create(tClass);
}
}