| | |
| | | package com.dayu.general.net |
| | | |
| | | import com.dayu.baselibrary.BuildConfig |
| | | import com.dayu.qiheonlinelibrary.net.MyIntercepterApplication |
| | | import okhttp3.OkHttpClient |
| | | import okhttp3.logging.HttpLoggingInterceptor |
| | | import retrofit2.Retrofit |
| | |
| | | * Author: zuo |
| | | * Date: 2025-03-06 |
| | | */ |
| | | class RetrofitClient { |
| | | class RetrofitClient// 包含header、body数据 |
| | | // loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS); |
| | | //设置连接和读取时间 |
| | | //添加统一的header |
| | | //添加日志拦截器 |
| | | //添加数据请求统一处理拦截器 |
| | | () { |
| | | |
| | | |
| | | |
| | |
| | | val READ_TIME_OUT: Int = 10 |
| | | val CONNECT_TIME_OUT: Int = 10 |
| | | |
| | | private fun RetrofitClient() { |
| | | |
| | | init { |
| | | val loggingInterceptor = HttpLoggingInterceptor() |
| | | // 包含header、body数据 |
| | | loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY) |
| | | // loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS); |
| | | val builder: OkHttpClient.Builder = OkHttpClient().newBuilder() |
| | | //设置连接和读取时间 |
| | | builder.readTimeout(READ_TIME_OUT.toLong(), TimeUnit.SECONDS) |
| | | builder.connectTimeout(CONNECT_TIME_OUT.toLong(), TimeUnit.SECONDS) |
| | | builder.writeTimeout(CONNECT_TIME_OUT.toLong(), TimeUnit.SECONDS) |
| | | //添加统一的header |
| | | builder.addInterceptor(MyIntercepterApplication()) |
| | | //添加日志拦截器 |
| | | //添加数据请求统一处理拦截器 |
| | | if (BuildConfig.DEBUG) { |
| | | builder.addInterceptor(loggingInterceptor) |
| | | } |
| | | |
| | | val client: OkHttpClient = builder.build() |
| | | |
| | | retrofit = Retrofit.Builder() |
| | | .baseUrl(NetConstans.BASE_URL) |
| | | .addConverterFactory(GsonConverterFactory.create()) |
| | |
| | | .client(client).build() |
| | | } |
| | | |
| | | |
| | | @Synchronized |
| | | |
| | | |