| package com.dayu.qiheonlinelibrary.net; | 
|   | 
|   | 
|   | 
| import com.dayu.baselibrary.BuildConfig; | 
|   | 
| import java.util.concurrent.TimeUnit; | 
|   | 
| import okhttp3.OkHttpClient; | 
| import okhttp3.logging.HttpLoggingInterceptor; | 
| import retrofit2.Retrofit; | 
| import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory; | 
| import retrofit2.converter.gson.GsonConverterFactory; | 
|   | 
| /** | 
|  * Copyright (C), 2023, | 
|  * Author: zuo | 
|  * Date: 2023-03-27 14:56 | 
|  * Description: | 
|  */ | 
| public class RetrofitClient { | 
|   | 
| //    private static final String BASE_URL = " http://114.116.229.87/api/"; | 
|   | 
|     private static RetrofitClient mInstance; | 
|     private Retrofit retrofit; | 
|     private static final int READ_TIME_OUT = 10; | 
|     private static final int CONNECT_TIME_OUT = 10; | 
|   | 
|     private RetrofitClient() { | 
|         HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | 
|         // 包含header、body数据 | 
|         loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | 
| //        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS); | 
|         OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); | 
|         //设置连接和读取时间 | 
|         builder.readTimeout(READ_TIME_OUT, TimeUnit.SECONDS); | 
|         builder.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS); | 
|         builder.writeTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS); | 
|         //添加统一的header | 
|         builder.addInterceptor(new MyIntercepterApplication()); | 
|         //添加日志拦截器 | 
|         //添加数据请求统一处理拦截器 | 
|         if (BuildConfig.DEBUG) { | 
|             builder.addInterceptor(loggingInterceptor); | 
|         } | 
|   | 
|         OkHttpClient client = builder.build(); | 
|   | 
|         retrofit = new Retrofit.Builder() | 
|                 .baseUrl(Constants.BASE_URL) | 
|                 .addConverterFactory(GsonConverterFactory.create()) | 
|                 .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) | 
|                 .client(client).build(); | 
|     } | 
|   | 
|     public static synchronized RetrofitClient getInstance() { | 
|         if (mInstance == null) { | 
|             mInstance = new RetrofitClient(); | 
|         } | 
|         return mInstance; | 
|     } | 
|   | 
|     public ApiService getApiService() { | 
|         return retrofit.create(ApiService.class); | 
|     } | 
|   | 
|   | 
|   | 
| } |