18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.example.smart.reqeust
 
import android.annotation.SuppressLint
import androidx.multidex.BuildConfig
import com.example.smart.reqeust.interceptor.LoggingInterceptor
import com.example.smart.reqeust.interceptor.HeaderInterceptor
import com.example.smart.reqeust.interceptor.LoggingInterceptorZmc
import com.google.gson.Gson
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
 
/**
 * @Description 网络请求
 */
object RetrofitClient {
 
    private val baseUrl: String = rootUrl()
 
    private const val DEFAULT_TIMEOUT = 30L
 
    const val serverUrl = "http://47.108.113.190:8182/"
//    const val serverUrl = "http://192.168.110.111:8182/"
 
    private fun rootUrl(): String {
        return serverUrl
    }
 
    fun getRootUrl() = baseUrl
 
    private val stsRootUrl = "http://47.93.28.249:7080/"
 
    private var retrofit: Retrofit
 
    private var ossRequest: Retrofit
 
    /**
     * init代码块实际上会放到构造函数里面执行
     * 而构造函数里面的代码会在init之后执行
     * 多个init 按代码里的顺序执行
     */
    init {
        val okHttpClient = okHttpClientHeader()
 
        retrofit = Retrofit.Builder()
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
//            .addCallAdapterFactory(CoroutineCallAdapterFactory())
            .baseUrl(baseUrl)
            .build()
 
        val ossClient = okHttpClientHeader()
 
        ossRequest = Retrofit.Builder()
            .client(ossClient)
            .addConverterFactory(GsonConverterFactory.create())
//            .addCallAdapterFactory(CoroutineCallAdapterFactory())
            .baseUrl(stsRootUrl)
            .build()
    }
 
    fun <T> create(service: Class<T>): T = retrofit.create(service)
 
 
    fun <T> createOss(service: Class<T>): T = ossRequest.create(service)
 
    val gson by lazy { Gson() }
 
    val api by lazy { create(ApiService::class.java) }
 
    val ossApi by lazy { createOss(ApiService::class.java) }
 
    @SuppressLint("SuspiciousIndentation")
    fun okHttpClientHeader(): OkHttpClient {
        val builder = OkHttpClient.Builder()
            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            .addInterceptor(HeaderInterceptor())
            .addInterceptor(LoggingInterceptorZmc())
        return builder.build()
    }
 
    fun getBody(map: Map<String, Any?>): RequestBody {
        return RequestBody.create(
            "application/json; charset=utf-8".toMediaTypeOrNull(),
            gson.toJson(map)
        )
    }
}