解决类Koin的实例时出错

时间:2018-11-11 12:05:02

标签: android koin

帮助解决问题!((((我有3个用于DI的模块。natworkModule中有一个改装对象,viewModelModule中有所有viewModel,而respositoryModule中有所有对服务器的请求。我根据文档进行了所有操作,但是我在Google中找不到此错误。提前谢谢!对不起,我的英语!)

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())
    }
}

1 个答案:

答案 0 :(得分:0)

此常量声明此常量const val SERVER_URL =“ https://api.github.com/”,koin找不到它,因此存在异常。谢谢大家!