Android 如果没有@Inject构造函数或@Provides注释方法,则无法提供okhttp3.Cache
我正在使用Android Dagger2,但我得到了下面的错误。 我的AppModule类是:Android 如果没有@Inject构造函数或@Provides注释方法,则无法提供okhttp3.Cache,android,dependency-injection,dagger-2,android-mvp,Android,Dependency Injection,Dagger 2,Android Mvp,我正在使用Android Dagger2,但我得到了下面的错误。 我的AppModule类是: @Module public class AppModule { RetrofitExample retrofitExample; AppModule(RetrofitExample retrofitExample) { this.retrofitExample = retrofitExample; } @Singleton @Provides RetrofitExample pro
@Module
public class AppModule {
RetrofitExample retrofitExample;
AppModule(RetrofitExample retrofitExample) {
this.retrofitExample = retrofitExample;
}
@Singleton
@Provides
RetrofitExample provideApplication() {
return retrofitExample;
}
}
我的API模块类
@Module
class ApiModule {
String BaseUrl;
private MainActivity mainActivity;
ApiModule(String baseUrl) {
this.BaseUrl = baseUrl;
}
public ApiModule(MainActivity downloadFileView) {
this.mainActivity = downloadFileView;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkhttpClient(Cache cache) {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.cache(cache);
return client.build();
}
@Singleton
@Provides
Retrofit providesRetrofit(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BaseUrl)
.client(okHttpClient)
.build();
}
}
我的API组件类
void inject(MainActivity activity);
这是我的申请课
private static RetrofitExample mInstance;
private ApiComponent mApiComponent;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
mApiComponent = DaggerApiComponent.builder()
.appModule(new AppModule(this))
.apiModule(new ApiModule("https://simplifiedcoding.net/demos/"))
.build();
}
public static synchronized RetrofitExample getInstance() { return mInstance; }
ApiComponent getApiComponent()
{
return mApiComponent;
}
我得到以下错误
okhttp3.Cache cannot be provided without an @Inject constructor or from an
@Provides-annotated method.
okhttp3.Cache is injected at
net.simplifiedlearning.retrofitexample.ApiModule.provideOkhttpClient(cache)
okhttp3.OkHttpClient is injected at
net.simplifiedlearning.retrofitexample.ApiModule.providesRetrofit(…,
okHttpClient)
retrofit2.Retrofit is injected at
net.simplifiedlearning.retrofitexample.MainActivity.retrofit
net.simplifiedlearning.retrofitexample.MainActivity is injected at
net.simplifiedlearning.retrofitexample.ApiComponent.inject(activity)
你需要
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
在您的模块中。在@Raghunandan查看与您类似的NetModule,我可以知道您的电子邮件id吗?您可以研究一下这个问题吗?我无法在@Raghunandan解决这个问题
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}