I have an application in Kotlin for retrieving data from some IP address. The data is in JSON format. Everything is fine except I have hard coded this IP address in an interface called SybaseApi. What I need is the code for changing this IP address at runtime of the application. E.g., user opens some window and here he can change this IP. The IP address is then stored in the application.
This is the module to retrieve data using retrofit.
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
@Singleton
fun provideRetrofit(): Retrofit =
Retrofit.Builder()
.baseUrl(SybaseApi.BASE_URL)
.client(provideOkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build()
@Provides
@Singleton
fun provideSybaseApi(retrofit: Retrofit): SybaseApi =
retrofit.create(SybaseApi::class.java)
@Provides
@Singleton
fun provideDatabase(app: Application): SybaseDatabase =
Room.databaseBuilder(app, SybaseDatabase::class.java, "horovice")
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build()
@Provides
@Singleton
fun provideOkHttpClient() =
OkHttpClient.Builder()
.readTimeout(6000, TimeUnit.SECONDS)
.callTimeout(6000, TimeUnit.SECONDS)
.build()
}
This is the interface - IP address is in variable BASE_URL. The best way would be to set the value to this variable, because I am using it in different places in the code of my application.
interface SybaseApi {
companion object {
const val BASE_URL:String = "http://192.168.3.118:5000/"
}
@GET("getItem")
suspend fun getItem(): List<Items>
}
You need to change
const valtovar. To change itSybaseApi.BASE_URL = "some value"