Search code examples
androidkotlinretrofit2builder

How to transfer string from MainActivity into single object?


I have ServiceBuilder object for init retrofit instance

object ServiceBuilder {

    //private var url: String? = null
    var url = "http://no-google.com"    // The default link

    fun loadUrl(url: String): ServiceBuilder{
        this.url = url
        return this
    }

    private var logger = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)

    val headerInterceptor = object: Interceptor {

        override fun intercept(chain: Interceptor.Chain): Response {

            var request = chain.request()

            request = request.newBuilder()
                .addHeader("x-device-type", Build.DEVICE)
                .addHeader("Accept-Language", Locale.getDefault().language)
                .build()

            val response = chain.proceed(request)
            return response
        }

    }

    // Create OkHttp Client
    private val okHttp = OkHttpClient.Builder()
        .callTimeout(5, TimeUnit.SECONDS)
        .addInterceptor(headerInterceptor)
        .addInterceptor(logger)

    // Create Retrofit Builder
    private val builder = Retrofit.Builder()
        .baseUrl(url)
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttp.build())

    // Create Retrofit Instance
    private val retrofit = builder.build()

    fun <T> buildService(serviceType: Class<T>): T {
        return retrofit.create(serviceType)
    }
}

getUrlFromServer() method inside MainActivity

private fun getUrlFromServer(str: String){
        val destinationService = ServiceBuilder
            .loadUrl("http://google.com")       // <-- This call can not reply url into ServiceBuilder object
            .buildService(DestinationService::class.java)

        val requestCall = destinationService.getList()

        requestCall.enqueue(object: Callback<List<Destination>> {
            override fun onResponse(
                call: Call<List<Destination>>,
                response: Response<List<Destination>>
            ) {
                if (response.isSuccessful){
                    val destinationList = response.body()
                    //Toast.makeText(this, destinationList.toString(), Toast.LENGTH_LONG)
                }
            }

            override fun onFailure(call: Call<List<Destination>>, t: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }

I don't understand why the loadUrl() function which is inside ServiceBuilder can not load url. I need to send url from MainActivity to ServiceBuilder object.

Please tell me how I should decide this issue in good style


Solution

  • Because create retrofit instance, take happen before ServiceBuilder loadUrl function. Actually retrofit instance, always is created with "http://no-google.com" url!!

    fun <T> buildService(serviceType: Class<T>): T {
        // Create Retrofit Builder
        private val builder = Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttp.build())
    
        // Create Retrofit Instance
        private val retrofit = builder.build()
    
        return retrofit.create(serviceType)
    }