这是一个有效的代码
我想了解的内容:
改造如何使用类似okhttp
之类的东西, gson
, Cache
下面....我知道我们注入(inject)它,但如 apimodule
中所示我可以看到 retrofit
使用了一个全局变量那里但是gson
和okhttp
不知道它是如何使用的
Apimodule.java
@Module
class ApiModule {
String mBaseUrl;
ApiModule(String mBaseUrl) {
this.mBaseUrl = mBaseUrl;
}
@Provides
@Singleton
Cache provideHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@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();
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
}
}
ApiComponent.java
@Singleton
@Component(modules = {AppModule.class, ApiModule.class})
public interface ApiComponent {
void inject(MainActivity activity);
}
MyApplication.java
public class MyApplication extends Application {
private ApiComponent mApiComponent;
@Override
public void onCreate() {
super.onCreate();
mApiComponent = DaggerApiComponent.builder()
.appModule(new AppModule(this))
.apiModule(new ApiModule("https://simplifiedcoding.net/demos/"))
.build();
}
public ApiComponent getNetComponent() {
return mApiComponent;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
//injecting retrofit
@Inject Retrofit retrofit;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((MyApplication) getApplication()).getNetComponent().inject(this);
listView = (ListView) findViewById(R.id.listViewHeroes);
getHeroes();
}
private void getHeroes() {
Api api = retrofit.create(Api.class);
Call<List<Hero>> call = api.getHeroes();
call.enqueue(new Callback<List<Hero>>() {
@Override
public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {
List<Hero> heroList = response.body();
String[] heroes = new String[heroList.size()];
for (int i = 0; i < heroList.size(); i++) {
heroes[i] = heroList.get(i).getName();
}
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, heroes));
}
@Override
public void onFailure(Call<List<Hero>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
最佳答案
当使用 dagger 时,当我们使用@Provide
时,我们告诉 dagger 如何创建一个特定的Dependency
,然后这个依赖关系可用于任何其他需要的对象那种依赖。当我们使用 @Inject 时,Dagger 将查看需要什么类型的对象,检查它需要哪些依赖项,如果您尝试注入(inject)的这个对象需要 dagger 已经知道如何制作的东西(通过某些东西)使用 @Provides
进行注释),然后它将为该对象创建此依赖项的一个新实例,并继续此过程,直到创建了您通过注入(inject)请求的整个对象。
举个例子:
@Provides
@Singleton
fun provideContext(app: YourApplication): Context = app
@Provides
@Singleton
fun provideDatabase(context: Context): YourAppDb = YourAppDb.create(context)
在此示例中,每当您请求 YourAppDb
实例时,dagger 都会检查其依赖项,发现它需要 context
,它将检查它是否“知道”如何创建上下文(检查用 @Provides
注释的内容),然后使用此 context
来创建数据库
关于java - 在使用 dagger2 的示例中如何使用 okhttp 和 gson 进行改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57125068/