使用匕首从firebase远程配置中使用`base_url`注入改造。

问题描述 投票:0回答:1

我知道,我们可以用匕首注入改造的方式来进行,当 BASE_URL 是固定的。

@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create(gson)).build();

    return retrofit;
}

@Provides
@Singleton
ApiCallInterface provideApiCallInterface(Retrofit retrofit) {
    return retrofit.create(ApiCallInterface.class);
}

但是,这里的问题是我的 BASE_URL 来自firebase远程配置,而我只能在运行后才能获得,这太晚了,因为匕首会在运行前初始化改造。如何解决这个问题?我看到的所有教程和例子,都是用静态的基站URL工作的,因此没有遇到这个问题。

我使用的是 remoteconfig 为我的基础网址,这样我就可以控制应用环境,而不用推送应用更新。

先谢谢了。

android firebase retrofit dagger firebase-remote-config
1个回答
0
投票

About GET-annotation (the same with POST):A relative or absolute path, or full URL of the endpoint. This valueis optional if the first parameter of the method is annotated with@Url.

  1. So with @URL parameter of your endpoint and empty GET parameter you can set full URL in runtime with parameter @URL of your endpoint

So try with something like this in your endpoint:

and set parameter of method (full URL) in runtime @提供 @Singleton Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { Retrofit ...。

根据 改装文件

关于base_URL。端点值可以是一个完整的URL,有host的值代替baseUrl的host,有scheme的值代替baseUrl的scheme。

有host的值可以替换baseUrl的host,有scheme的值可以替换baseUrl的scheme。基本URL。

例如:基本URL:http:/example.com

  1. 端点。

https:/github.comsquareretrofit。

结果。

https:/github.comsquareretrofit。

@GET()
fun getData(@Url url : String): Call<SomeResult>

因此,如果端点的URL拥有完整的URL,基础URL将被替换为

© www.soinside.com 2019 - 2024. All rights reserved.