简体   繁体   中英

Error while resolving instance for class Koin

Help solve the problem!(((I have 3 modules for DI. There is a retrofit object in natworkModule, all viewModels in viewModelModule, and all requests to the server in respositoryModule. I did everything according to the documentation, but I cannot find this error in Google. Thank you in advance!!! Sorry for my english!)

class App : Application(){

    override fun onCreate() {
        super.onCreate()
        startKoin(this, listOf(natworkModule, viewModelModule,repositoryModule))
       }
    }


var natworkModule = module {

  single { createOkHttpClient() }
  single { createApiService<ApiService>(get () ,getProperty(SERVER_URL)) 

  }

}


const val SERVER_URL = "https://api.github.com/"

fun createOkHttpClient() : OkHttpClient{
   val httpLoggingInterceptor = HttpLoggingInterceptor()
   httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
   return OkHttpClient.Builder()
        .connectTimeout(60L, TimeUnit.SECONDS)
        .readTimeout(60L, TimeUnit.SECONDS)
        .addInterceptor(httpLoggingInterceptor).build()
   }

 inline fun <reified T> createApiService(okHttpClient: OkHttpClient,  url: String): T {
   val retrofit = Retrofit.Builder()
        .baseUrl(url)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(LiveDataCallAdapterFactory()).build()
   return retrofit.create(T::class.java)
 }

var repositoryModule = module {
    factory<TestRepository> {
        TestRepositoryImpl(get())
    }

}

var viewModelModule = module {
    viewModel {
        TestViewModel(get())
    }
}

Problem was in this constant value -> SERVER_URL = " https://api.github.com/ " Koin could not find it. Therefore there was an exception. Thanks to all!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM