|
@@ -2,6 +2,8 @@ package com.rc.httpcore
|
|
|
|
|
|
import android.content.Context
|
|
import android.content.Context
|
|
import android.util.Log
|
|
import android.util.Log
|
|
|
|
+import com.blankj.utilcode.util.AppUtils
|
|
|
|
+import com.blankj.utilcode.util.LogUtils
|
|
import com.rc.httpcore.client.factory.ClientFactory
|
|
import com.rc.httpcore.client.factory.ClientFactory
|
|
import com.rc.httpcore.client.factory.RetrofitFactory
|
|
import com.rc.httpcore.client.factory.RetrofitFactory
|
|
import com.rc.httpcore.config.OkHttpDNS
|
|
import com.rc.httpcore.config.OkHttpDNS
|
|
@@ -19,13 +21,14 @@ import java.util.logging.Logger
|
|
|
|
|
|
object HttpClient {
|
|
object HttpClient {
|
|
|
|
|
|
-// private const val TIMEOUT_DEFAULT = 3 * 60L
|
|
|
|
|
|
+ // private const val TIMEOUT_DEFAULT = 3 * 60L
|
|
private const val TIMEOUT_DEFAULT = 10L
|
|
private const val TIMEOUT_DEFAULT = 10L
|
|
|
|
|
|
private var mAppContext: Context? = null
|
|
private var mAppContext: Context? = null
|
|
private val LOGGER: Logger = Logger.getLogger(HttpClient::class.java.name)
|
|
private val LOGGER: Logger = Logger.getLogger(HttpClient::class.java.name)
|
|
- var token: String?=null
|
|
|
|
|
|
+ var token: String? = null
|
|
var vName: String = "1.0.0"
|
|
var vName: String = "1.0.0"
|
|
|
|
+ var httpClient: OkHttpClient? = null
|
|
|
|
|
|
|
|
|
|
fun init(appContext: Context) {
|
|
fun init(appContext: Context) {
|
|
@@ -50,7 +53,7 @@ object HttpClient {
|
|
|
|
|
|
private fun buildRetrofit(
|
|
private fun buildRetrofit(
|
|
baseUrl: String = HttpConfig.API_BASE_URL,
|
|
baseUrl: String = HttpConfig.API_BASE_URL,
|
|
- okHttpClient: OkHttpClient = buildHttpClient()
|
|
|
|
|
|
+ okHttpClient: OkHttpClient? = buildHttpClient()
|
|
): Retrofit {
|
|
): Retrofit {
|
|
return Retrofit.Builder()
|
|
return Retrofit.Builder()
|
|
.client(okHttpClient)
|
|
.client(okHttpClient)
|
|
@@ -61,21 +64,44 @@ object HttpClient {
|
|
.build()
|
|
.build()
|
|
}
|
|
}
|
|
|
|
|
|
- private fun buildHttpClient(): OkHttpClient {
|
|
|
|
|
|
+ private fun buildHttpClient(): OkHttpClient? {
|
|
// //设置日志打印级别
|
|
// //设置日志打印级别
|
|
// val interceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
|
|
// val interceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
|
|
// LOGGER.log(Level.INFO, message)
|
|
// LOGGER.log(Level.INFO, message)
|
|
// })
|
|
// })
|
|
// interceptor.level = HttpLoggingInterceptor.Level.BODY
|
|
// interceptor.level = HttpLoggingInterceptor.Level.BODY
|
|
- return OkHttpClient.Builder()
|
|
|
|
- .readTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置读取超时时间
|
|
|
|
- .connectTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置请求超时时间
|
|
|
|
- .writeTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置写入超时时间
|
|
|
|
- .addNetworkInterceptor(TokenHeaderInterceptor())
|
|
|
|
- .addInterceptor(LogInterceptor())//添加请求日志
|
|
|
|
- .retryOnConnectionFailure(true) // 设置出现错误进行重新连接
|
|
|
|
|
|
+
|
|
|
|
+ val logPath = "/sdcard/logs/"
|
|
|
|
+ val config = LogUtils.getConfig()
|
|
|
|
+ // log开关控制
|
|
|
|
+ config.setLogSwitch(true)
|
|
|
|
+ // log控制台开关
|
|
|
|
+ config.setConsoleSwitch(true)
|
|
|
|
+ // logTag
|
|
|
|
+ config.setGlobalTag("Jayce")
|
|
|
|
+ // log头部信息开关
|
|
|
|
+ config.setLogHeadSwitch(true)
|
|
|
|
+ // log文件开关
|
|
|
|
+ config.setLog2FileSwitch(true)
|
|
|
|
+ config.setDir(logPath)
|
|
|
|
+ // log文件前缀
|
|
|
|
+ config.setFilePrefix(AppUtils.getAppName())
|
|
|
|
+ // log边框开关
|
|
|
|
+ config.setBorderSwitch(true)
|
|
|
|
+ // log文件保存天数
|
|
|
|
+ config.setSaveDays(7)
|
|
|
|
+ if (null == httpClient) {
|
|
|
|
+ return OkHttpClient.Builder()
|
|
|
|
+ .readTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置读取超时时间
|
|
|
|
+ .connectTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置请求超时时间
|
|
|
|
+ .writeTimeout(TIMEOUT_DEFAULT, TimeUnit.SECONDS) // 设置写入超时时间
|
|
|
|
+ .addNetworkInterceptor(TokenHeaderInterceptor())
|
|
|
|
+ .addInterceptor(LogInterceptor())//添加请求日志
|
|
|
|
+ .retryOnConnectionFailure(true) // 设置出现错误进行重新连接
|
|
// .dns(OkHttpDNS())
|
|
// .dns(OkHttpDNS())
|
|
- .build()
|
|
|
|
|
|
+ .build()
|
|
|
|
+ }
|
|
|
|
+ return httpClient;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|