Diy*_*hat 1 android dependency-injection dagger-2 android-mvp
我正在使用 Android Dagger2,但收到以下错误。\n我的 AppModule 类是:
\n\n@Module\npublic class AppModule {\n\nRetrofitExample retrofitExample;\n\nAppModule(RetrofitExample retrofitExample) {\n\n this.retrofitExample = retrofitExample;\n\n}\n\n@Singleton\n@Provides\nRetrofitExample provideApplication() {\n return retrofitExample;\n}\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我的API模块类
\n\n@Module\nclass ApiModule {\n\nString BaseUrl;\n\nprivate MainActivity mainActivity;\n\nApiModule(String baseUrl) {\n this.BaseUrl = baseUrl;\n}\n\n\npublic ApiModule(MainActivity downloadFileView) {\n this.mainActivity = downloadFileView;\n}\n@Provides\n@Singleton\nGson provideGson() {\n GsonBuilder gsonBuilder = new GsonBuilder();\n gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);\n return gsonBuilder.create();\n}\n\n@Provides\n@Singleton\nOkHttpClient provideOkhttpClient(Cache cache) {\n OkHttpClient.Builder client = new OkHttpClient.Builder();\n client.cache(cache);\n return client.build();\n}\n\n@Singleton\n@Provides\nRetrofit providesRetrofit(Gson gson, OkHttpClient okHttpClient) {\n return new Retrofit.Builder()\n .addConverterFactory(GsonConverterFactory.create(gson))\n .baseUrl(BaseUrl)\n .client(okHttpClient)\n .build();\n}\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我的 API 组件类。
\n\nvoid inject(MainActivity activity);\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的应用程序类
\n\nprivate static RetrofitExample mInstance;\n\nprivate ApiComponent mApiComponent;\n\n@Override\npublic void onCreate() {\n super.onCreate();\n\n mInstance = this;\n\n mApiComponent = DaggerApiComponent.builder()\n .appModule(new AppModule(this))\n .apiModule(new ApiModule("https://simplifiedcoding.net/demos/"))\n .build();\n}\n\npublic static synchronized RetrofitExample getInstance() { return mInstance; }\nApiComponent getApiComponent()\n{\n return mApiComponent;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我收到以下错误
\n\nokhttp3.Cache cannot be provided without an @Inject constructor or from an \n@Provides-annotated method.\nokhttp3.Cache is injected at\nnet.simplifiedlearning.retrofitexample.ApiModule.provideOkhttpClient(cache)\nokhttp3.OkHttpClient is injected at\nnet.simplifiedlearning.retrofitexample.ApiModule.providesRetrofit(\xe2\x80\xa6, \nokHttpClient)\nretrofit2.Retrofit is injected at\nnet.simplifiedlearning.retrofitexample.MainActivity.retrofit\nnet.simplifiedlearning.retrofitexample.MainActivity is injected at\nnet.simplifiedlearning.retrofitexample.ApiComponent.inject(activity)\nRun Code Online (Sandbox Code Playgroud)\n
okhttp3.Cache cannot be provided without an @Inject constructor or from an
@Provides-annotated method.
Run Code Online (Sandbox Code Playgroud)
你需要
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
Run Code Online (Sandbox Code Playgroud)
在你的 ApiModule 中。检查与您的类似的 NetModule https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2
| 归档时间: |
|
| 查看次数: |
1428 次 |
| 最近记录: |