如何将Dagger2与Retrofit一起使用?

时间:2019-06-14 11:14:23

标签: android kotlin retrofit dagger-2

我在一个项目中工作,我需要用Dagger 2注入Retrofit对象。我搜索了信息,但只能执行几个步骤,而现在我不知道如何继续:

我的组件

<select>
    <option selected="">USD (United States Dollar)</option>
    <option>GBP (Great British Pound)</option>
    <option>AUD (Australian Dollar)</option>
</select>

我的模块:

@Singleton
@Component(modules = arrayOf(NetworkModule::class))
interface NetworkComponent {
    fun inject(foo: TheApplication)
}

现在我看到我必须创建一个@Module class NetworkModule { @Provides @Singleton fun provideRetrofit(): Retrofit { return Retrofit.Builder() .baseUrl("https://api.chucknorris.io/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() } } ,该人从Class扩展来初始化Application,然后再创建一个Component

@Inject

从理论上讲,当我放置class TheApplication: Application() { override fun onCreate() { super.onCreate() val net: NetworkModule = Dagger... } } 时,它应该是Dagger...,但是我重建项目并且仍然丢失。 任何人都可以向我解释DaggerNetworkComponent的用途以及我该如何继续?

1 个答案:

答案 0 :(得分:2)

尝试一下

  

AppComponent.kt

@Component(modules = [NetworkModule::class]) 
@Singleton 
interface AppComponent {
    fun inject(app: MyApp)
}
  

NetworkModule.kt

@Module
class NetworkModule {

    @Singleton
    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("YOUR_BASE_URL")
            .client(okHttpClient)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }

    @Singleton
    @Provides
    fun provideApi(retrofit: Retrofit): WebApi {
        return retrofit.create(WebApi::class.java)
    }

    @Singleton
    @Provides
    fun provideOkHttpClient(
        interceptors: ArrayList<Interceptor>
    ): OkHttpClient {
        val clientBuilder = OkHttpClient.Builder()
            .followRedirects(false)
        interceptors.forEach {
            clientBuilder.addInterceptor(it)
        }
        return clientBuilder.build()
    }


    @Singleton
    @Provides
    fun provideInterceptors(): ArrayList<Interceptor> {
        val interceptors = arrayListOf<Interceptor>()
        val loggingInterceptor = HttpLoggingInterceptor().apply {
            level = if (BuildConfig.DEBUG) {
                HttpLoggingInterceptor.Level.BODY
            } else {
                HttpLoggingInterceptor.Level.NONE
            }
        }
        interceptors.add(loggingInterceptor)
        return interceptors
    }
}
  

MyApp.kt

class MyApp : Application() {
    companion object {
        lateinit var instance: MyApp
            private set
    }

    lateinit var appComponent: AppComponent
        private set


    override fun onCreate() {
        super.onCreate()
        instance = this
        initComponent()
    }

    private fun initComponent() {
        appComponent = DaggerAppComponent.builder()
            .build()
        appComponent.inject(this)
    }
}
  

AndroidManifest.xml

<application
            android:name=".MyApp"
            ....