配置 Retrofit 以使用不同的基础 URL。

6

我想要将我的Android应用程序从Volley切换到Retrofit2。最初,我使用单例的Retrofit实例来创建Retrofit服务对象。 但是应用程序需要与具有不同基础url的服务进行通信。我正在尝试找出在Retrofit中切换基础url的最佳方法。我已经阅读了以下解决方案:

  1. 我已经阅读了一些线程,在这些线程中建议在拦截器级别上切换基础url。这似乎是一个hacky的解决方案,在网络层切换基础url。
  2. 还可以选择使用多个Retrofit实例来处理不同的urls。我不太喜欢这个解决方案,因为它可能会创建大量的Retrofit实例。

在我的应用程序中,90%的调用都是针对同一个基础url进行的。另外10%有4-5个不同的urls。 现在我认为最好只使用OkHttp来处理这些异常情况的调用。

对于这个问题,您有什么好的解决方案吗?

4个回答

6
我已经解决了,方法如下:
这是我的Retrofit实例:
    val retrofit = Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .baseUrl("http://baseurl....")
            .client(client)
            .build()

当我下载数据时,我只需要这样更改URL:

@GET
fun downloadData(@Url url: String): Observable<Response<ResponseBody>>

3

为什么不创建另一个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);
    }
}

创建两个Retrofit客户端,例如:
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服务的[接口]

public interface BaseApiService {

@FormUrlEncoded
@POST("complete the URL")
Call<ResponseBody> get_methode (@Field("?") String ?);
}

你可以用这个来打电话。
 BaseApiService Api1; //above the onCreate

在 onCreate 方法内部

Api1    = UtilsApi1.get_methode();

1
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); 

0
如果您查看函数的文档。
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/

如果您想配置多个基本URL,请在端点中使用完整的URL,如官方文档中所述。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接