12345678910111213141516171819202122232425262728293031323334353637 |
- package com.example.pda.network
- import retrofit2.Retrofit
- import retrofit2.converter.gson.GsonConverterFactory
- import com.example.pda.model.AppPrefs
- // RetrofitClient 对象用于创建和管理 Retrofit 实例
- // 添加重置方法
- object RetrofitClient {
- private var retrofitInstance: Retrofit? = null
- private var apiService: ApiService? = null
- fun reset() {
- retrofitInstance = null
- apiService = null
- }
- val instance: ApiService
- get() {
- if (apiService == null) {
- retrofitInstance = Retrofit.Builder()
- .baseUrl(formatBaseUrl(AppPrefs.base_url))
- .client(HttpClient.instance)
- .addConverterFactory(GsonConverterFactory.create())
- .build()
- apiService = retrofitInstance!!.create(ApiService::class.java)
- }
- return apiService!!
- }
- private fun formatBaseUrl(url: String): String {
- var formatted = url
- if (!formatted.startsWith("http")) formatted = "http://$formatted"
- if (!formatted.endsWith("/")) formatted += "/"
- return formatted
- }
- }
|