在设置中配置 RetroFit BASE URL
Configure RetroFit BASE URL in Settings
我有一个应用程序将部署到不同站点上的多个客户。它使用 RetroFit 与服务器产品进行通信。
问题是每个客户都会在自己的本地服务器上部署此服务器产品,并有自己的 BASE_URL,因此无法将 BASE_URL 硬编码到应用程序中。在现场部署应用程序时需要针对每个设备进行配置,而不是为每个客户构建应用程序。
我创建了一个带有字段的设置屏幕,用于编辑 BASE_URL 并将其存储在 SharedPreferences
中。我的问题是:是否可以在每个 Call
上用 SharedPreferences
中的这个值替换 BASE_URL?
其实不一定是SharedPreferences
。我只需要能够配置 BASE_URL 并将其存储在任何地方。甚至在SD卡上存储一个文件就足够了。
有人有什么建议吗?
谢谢。
更新:这是我的客户端代码:
public class RestClient {
private static RestInterface REST_CLIENT;
private static final String API_BASE_URL = "http://myurl/";
static {
setupRestClient();
}
private RestClient() {
}
public static RestInterface get() {
return REST_CLIENT;
}
private static void setupRestClient() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
REST_CLIENT = retrofit.create(RestInterface.class);
}
}
谢谢。
这是我得出的解决方案:
public class RestClient {
private static RestInterface REST_CLIENT;
private static final String API_BASE_URL = "http://myurl/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static RestInterface get(@NonNull Context context) {
if (null == REST_CLIENT) {
setupRestClient(context);
}
return REST_CLIENT;
}
private static void setupRestClient(@NonNull Context context) {
String baseUrl = CustomPrefs.getInstance(context).getEndpointBaseUrl();
httpClient.addInterceptor(getInterceptorWithAuth(context));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
REST_CLIENT = retrofit.create(RestInterface.class);
}
private static Interceptor getInterceptorWithAuth(@NonNull final Context context) {
final String username = CustomPrefs.getInstance(context).getHttpUsername();
final String password = CustomPrefs.getInstance(context).getHttpPassword();
final String credentials = username + ":" + password;
final String basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
return new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", basic)
.header("Accept", "application/json")
.header("User-Agent", "myApp")
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
};
}
}
我还在 HTTP Basic Auth 中添加了。
如果不需要 Auth,请删除 httpClient
和 getInterceptorWithAuth()
方法。
用法:
call = RestClient.get(context).whateverMethod();
我有一个应用程序将部署到不同站点上的多个客户。它使用 RetroFit 与服务器产品进行通信。
问题是每个客户都会在自己的本地服务器上部署此服务器产品,并有自己的 BASE_URL,因此无法将 BASE_URL 硬编码到应用程序中。在现场部署应用程序时需要针对每个设备进行配置,而不是为每个客户构建应用程序。
我创建了一个带有字段的设置屏幕,用于编辑 BASE_URL 并将其存储在 SharedPreferences
中。我的问题是:是否可以在每个 Call
上用 SharedPreferences
中的这个值替换 BASE_URL?
其实不一定是SharedPreferences
。我只需要能够配置 BASE_URL 并将其存储在任何地方。甚至在SD卡上存储一个文件就足够了。
有人有什么建议吗? 谢谢。
更新:这是我的客户端代码:
public class RestClient {
private static RestInterface REST_CLIENT;
private static final String API_BASE_URL = "http://myurl/";
static {
setupRestClient();
}
private RestClient() {
}
public static RestInterface get() {
return REST_CLIENT;
}
private static void setupRestClient() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
REST_CLIENT = retrofit.create(RestInterface.class);
}
}
谢谢。
这是我得出的解决方案:
public class RestClient {
private static RestInterface REST_CLIENT;
private static final String API_BASE_URL = "http://myurl/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static RestInterface get(@NonNull Context context) {
if (null == REST_CLIENT) {
setupRestClient(context);
}
return REST_CLIENT;
}
private static void setupRestClient(@NonNull Context context) {
String baseUrl = CustomPrefs.getInstance(context).getEndpointBaseUrl();
httpClient.addInterceptor(getInterceptorWithAuth(context));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
REST_CLIENT = retrofit.create(RestInterface.class);
}
private static Interceptor getInterceptorWithAuth(@NonNull final Context context) {
final String username = CustomPrefs.getInstance(context).getHttpUsername();
final String password = CustomPrefs.getInstance(context).getHttpPassword();
final String credentials = username + ":" + password;
final String basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
return new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", basic)
.header("Accept", "application/json")
.header("User-Agent", "myApp")
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
};
}
}
我还在 HTTP Basic Auth 中添加了。
如果不需要 Auth,请删除 httpClient
和 getInterceptorWithAuth()
方法。
用法:
call = RestClient.get(context).whateverMethod();