在DI模块中提供改造时,如何从元数据中读取主机名? [英] How to read hostname from meta-data when providing Retrofit in DI Modules?

查看:92
本文介绍了在DI模块中提供改造时,如何从元数据中读取主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Dagger 2 + Retrofit来实现我的接口,该接口向Web服务发送数据/从Web服务接收数据

I am using Dagger 2 + Retrofit to implement my interfaces which sends/receives data to/from my web service

我指的是 Philippe BOISNEY 的AppModule.java如下所示

I am referring Philippe BOISNEY's AppModule.java as below

private static String BASE_URL = "https://api.github.com/";

@Provides
Gson provideGson() { return new GsonBuilder().create(); }

@Provides
Retrofit provideRetrofit(Gson gson) {
    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(BASE_URL)
            .build();
    return retrofit;
}

但是我有一个问题,如果我有多个Web服务主机怎么办,例如 Production Staging Development

However I have a question that what if I have multiple hosts of my web services, such as Production, Staging and Development?

我已经设置了这些不同的主机连接到我的AndroidManifest.xml中的Build Config,但是我不知道如何读取AppModule中的元数据,以便用相应的Build config替换BASE_URL

I already setup those different hosts connected to Build Config in my AndroidManifest.xml, but I don't have an idea how to read meta-data in AppModule, in order to replace BASE_URL with corresponding build config

请给我建议,谢谢

推荐答案

您可以在build.gradle中定义几种口味类型,例如dev,prod和stage并为每种口味定义构建配置变量

You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable

productFlavors {
    dev {
        buildConfigField "String", "SERVER_URL", "\"your dev url\""
    }
    stage {
        buildConfigField "String", "SERVER_URL", "\"your stage url\""
    }
    prod {
        buildConfigField "String", "SERVER_URL", "\"your prod url\""
    }
}

然后使用它

private static String BASE_URL = BuildConfig.SERVER_URL;

如果您想使用匕首动态地提供它,可以用这种方式实现

If you like to provide it dynamically using dagger, you can do it in that way

@Module
public class AppModule {
    @Named("server_url")
    @Provides
    String provideServerUrl() {
        return "https://api.github.com/";
    }

    @Provides
    Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
        Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
        return retrofit;
    }
}

使用匕首动态提供服务器网址的另一种方式-使用建造者。例如,

Another way of dynamically providing server url using dagger - using builder. For example,

@Component(AppModule.class)
interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder serverUrl(@Named("server_url") String url);

        AppComponent build();
    }
}

@Module
public class AppModule {
    @Provides
    Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
        Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build();
        return retrofit;
    }
}

DaggerAppComponent.Builder()
    .appModule(new AppModule())
    .serverUrl("https://api.github.com/")
    .build();

这篇关于在DI模块中提供改造时,如何从元数据中读取主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆