밍쯔와 안작고 안귀여운 에러들🖤

[Android/Kotlin] 서버 BASE_URL 동적으로 바꾸기 본문

Develop/Android | iOS

[Android/Kotlin] 서버 BASE_URL 동적으로 바꾸기

밍쯔 2022. 8. 8. 11:53

[ 문제 ]

로그인 시 받는 access(int = 0~5) 값에 따라 BASE_URL을 동적으로 바꿔줘야 하는 상황

 

[ 해결 방법 ]

해당 어플은 재시작 할 때마다 로그인을 해야하는 형태임. 

로그인 시 받는 access 값을 전역 변수에 set 하고 이후, get 형태로 활용.

access 값을 받을 때, Retrofit의 setUrl 실행. 

LoginActiviti 에서는 BASE_URL을 무조건 사용하고, 이후 access 값에 따라 다른 url로 실행.

 

[ 기존 코드 ]

class RetrofitClient {
    companion object{
        private var retrofitClient: RetrofitService? = null
        fun getClient(): RetrofitService?{
            val httpLoggingInterceptor = HttpLoggingInterceptor()
            httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            val client = OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60,TimeUnit.SECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .build()

            if(retrofitClient == null){
                retrofitClient = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(client)
                    .build().create(RetrofitService::class.java)
            }
            return retrofitClient
        }
    }
}

 

[ 수정된 코드 ]

class RetrofitClient {
    companion object{
        private var retrofitClient: RetrofitService? = null
        private var Base_Url = "https://-------0.--------/app/"
        private var Flag = false

        fun getClient(isLogin : Boolean): RetrofitService?{
            val httpLoggingInterceptor = HttpLoggingInterceptor()
            httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            val client = OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60,TimeUnit.SECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .build()

            if (isLogin) {
                retrofitClient = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(client)
                    .build().create(RetrofitService::class.java)
            } else if (Flag || retrofitClient == null) {
                retrofitClient = Retrofit.Builder()
                    .baseUrl(Base_Url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(client)
                    .build().create(RetrofitService::class.java)
                    
                    setFlag(false)
            }
            return retrofitClient
        }

        fun setUrl(access : Int) {
            if (access in 1..5) {
                Base_Url = "https://-------" + access + ".--------/app/"
            } else {
                Base_Url = ""https://-------0.--------/app/""
            }
            setFlag(true)
        }

        private fun setFlag(flag : Boolean) {
            Flag = flag
        }
    }
}

 

https://realapril.tistory.com/46

 

kotlin RxJava, Retrofit2 예제 4 동적 base url 만들기

예제1 단순 retrofit2+ rxjava만들기 : https://realapril.tistory.com/39 예제2 http status code 받기 : https://realapril.tistory.com/42 예제3 http response 분리 및 편의개선 쿼리나 상부 주소를 변경하..

realapril.tistory.com

https://unluckluck.medium.com/using-two-or-more-base-urls-with-kotlin-and-retrofit-a70f5d9a5798

 

Using two or more Base URLs with Kotlin and Retrofit

I recently developed a project with two different endpoints in the same application. I know that’s often, mainly in holding’s projects…

unluckluck.medium.com

https://www.baeldung.com/retrofit-dynamic-url

 

Retrofit 2 - Dynamic URL | Baeldung

Learn how to create a dynamic URL in Retrofit2

www.baeldung.com