【问题标题】:Configure Retrofit with different base urls使用不同的基本 url 配置改造
【发布时间】:2018-02-03 20:37:03
【问题描述】:
我正在切换我的 Android 应用程序以使用 Retrofit2 而不是 Volley。最初,我有一个单独的 Retrofit 实例,我将使用它来创建 Retrofit 服务对象。
但是该应用程序需要与具有不同 url 基本 url 的服务通信。我正在尝试找出在 Retrofit 中切换基本 url 的最佳方式。我已阅读以下解决方案:
- 我已经阅读了建议在拦截器级别切换基本 URL 的线程。这似乎是一个 hacky 解决方案,在网络层切换基本 url。
- 还可以选择使用多个 Retrofit 实例来处理不同的 url。我不太喜欢这样,因为它最终可能会创建很多 Retrofit 实例。
在我的应用中,90% 的调用都是针对相同的基本 url。其他 10% 有 4-5 个不同的网址。
现在我觉得最好只使用 OkHttp 来使用这些异常值调用。
关于什么是一个好的解决方案有什么想法吗?
【问题讨论】:
标签:
android
retrofit2
okhttp3
【解决方案1】:
我已经解决了,这样:
这是我的改造实例:
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("http://baseurl....")
.client(client)
.build()
当我下载数据时,我只是这样更改网址:
@GET
fun downloadData(@Url url: String): Observable<Response<ResponseBody>>
【解决方案2】:
为什么不创建另一个 API 服务,它在我身上工作。
API1 类
public class UtilsApi1 {
public static final String BASE_URL1 = "http://YourBaseURL1";
public static BaseApiService getAPI1(){
return RetrofitClient1.getClient(BASE_URL1).create(BaseApiService.class);
}
}
API2 类
public class UtilsApi2 {
public static final String BASE_URL2 = "http://YourBaseURL2";
public static BaseApiService getAPI2(){
return RetrofitClient2.getClient(BASE_URL2).create(BaseApiService.class);
}
}
例如制作 2 个改造客户端
public class RetrofitClient1 {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;
}
}
和 API 服务的 [INTERFACE]
public interface BaseApiService {
@FormUrlEncoded
@POST("complete the URL")
Call<ResponseBody> get_methode (@Field("?") String ?);
}
你可以用这个打电话
BaseApiService Api1; //above the onCreate
在 onCreate 里面
Api1 = UtilsApi1.get_methode();
【解决方案3】:
public class RetrofitService {
public static String apiBaseUrl = "http://myurl";
private static Retrofit retrofit;
private static Retrofit.Builder builder =
new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(apiBaseUrl);
private static OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
public static void changeApiBaseUrl(String newApiUrl) {
apiBaseUrl = newApiUrl;
builder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(apiBaseUrl);
}
public static <S> S createRetrofitService(Class<S> serviceClas) {
retrofit = builder.build();
return retrofit.create(serviceClass);;
}
您的第一个 API 调用将是
MyFirstApi api1=RetrofitService.createRetrofitService(MyFirstApi.class);
//..............
您的第二个 API 调用将是。
RetrofitService.changeApiBaseUrl("your new url");
MySecondApi api2=RetrofitService.createRetrofitService(MySecondApi.class);
【解决方案4】:
如果你检查函数的文档。
public Builder baseUrl(HttpUrl baseUrl)
提到如果您提供完整的 url 与主机名和方案,那么它将覆盖基本 url。
<p>Values which have a host replace the host of {@code baseUrl} and values also with a scheme
replace the scheme of {@code baseUrl}.
<p>Base URL: http://example.com/<br>
Endpoint: https://github.com/square/retrofit/<br>
Result: https://github.com/square/retrofit/
所以如果你想配置多个base url。如官方文档中所述,在端点中使用完整的 url。