简体   繁体   English

使用 Dagger2 注入 OkHttpClient

[英]Injecting OkHttpClient using Dagger2

I'd like to be able to inject a new OkHttpClient using Dagger2 but am having a hard time.我希望能够使用 Dagger2 注入一个新的OkHttpClient但我遇到了困难。 Most of the tutorials and instructions are written for applications and I'm building an aar (android library).大多数教程和说明都是为应用程序编写的,我正在构建一个 aar(android 库)。

here's my Component:这是我的组件:

@Singleton
@Component(modules = {NetworkModule.class})
public interface AndroidSdkComponent {
    void inject(OkHttpClient httpClient);
}

here's my module:这是我的模块:

@Module
public class NetworkModule {

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(Context context) {
        final OkHttpClient client = new OkHttpClient();
        if (Configuration.isAlphaBuild(context)) {
            client.networkInterceptors().add(new StethoInterceptor());
        }
        return client;
    }
}

At an entry point of my library I build the component like so:在我的库的入口点,我像这样构建组件:

DaggerAndroidSdkComponent.builder().networkModule(new NetworkModule())
                                            .build();

but later when I'm trying to @Inject OkHttpClient okHttpClient it appears to always be null.但后来当我尝试@Inject OkHttpClient okHttpClient时,它似乎总是空的。 What am I doing wrong?我究竟做错了什么?

What I'm I doing wrong?我做错了什么?

void inject(OkHttpClient httpClient);

This will inject an OkHttpClient with the dependencies it needs...But since you neither call it, nor have access to the fields of the okhttp client anyways this method is redundant and useless.这将注入一个OkHttpClient与它需要的依赖关系......但是因为你既没有调用它,也没有访问 okhttp 客户端的字段,所以这个方法是多余的和无用的。

DaggerAndroidSdkComponent.builder().networkModule(new NetworkModule())
                                        .build();

This will build a component.这将构建一个组件。 It will build it.它会建造它。 You still need to use it.你仍然需要使用它。 eg call inject() and inject the object you want to inject.例如调用inject()并注入您要注入的对象。


What you need to do is update your interface to actually inject the object where you want the OkHttpClient in .您需要做的是更新您的界面,以实际将对象注入您想要OkHttpClient位置。 You don't provide any information of where you want that client to be used, but you should have SomeClass like the following, where you already build the component with the code you provided above:您没有提供任何有关您希望在何处使用该客户端的信息,但您应该拥有如下所示的SomeClass ,您已经在其中使用上面提供的代码构建了组件:

class SomeClass {
    @Inject
    OkHttpClient mClient(); // THIS is how you mark a field for injection

    void someMethod() {
        AndroidSdkComponent component = DaggerAndroidSdkComponent.builder().networkModule(new NetworkModule())
                .build();

        component.inject(this); // THIS is how you inject a field (okhttp!)
    }
}

Then you need to update the component to know about SomeClass that you want to be injected:然后您需要更新组件以了解您想要注入的SomeClass

@Singleton
@Component(modules = {NetworkModule.class})
public interface AndroidSdkComponent {
    void inject(SomeClass someclass); // THIS will inject okhttp to your someclass object, as seen above
}

Please read some more tutorials about dagger and try playing with it, since it seems that you don't really understand how it works or what the different classes do.阅读更多关于 dagger 的教程并尝试使用它,因为您似乎并不真正了解它的工作原理或不同类的作用。 The User's Guide is a good place to start.用户指南是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM