Android 在“设置”中配置改装基本URL
我有一个应用程序,将部署到不同网站上的多个客户。它使用改造与服务器产品进行通信 问题是,每个客户都会在自己的本地服务器上部署此服务器产品,并使用自己的基本URL,因此不能将基本URL硬编码到应用程序中。在现场部署应用程序时,需要针对每个设备进行配置,而不是针对每个客户构建应用程序 我创建了一个带有字段的设置屏幕,用于编辑基本URL并将其存储在Android 在“设置”中配置改装基本URL,android,retrofit2,Android,Retrofit2,我有一个应用程序,将部署到不同网站上的多个客户。它使用改造与服务器产品进行通信 问题是,每个客户都会在自己的本地服务器上部署此服务器产品,并使用自己的基本URL,因此不能将基本URL硬编码到应用程序中。在现场部署应用程序时,需要针对每个设备进行配置,而不是针对每个客户构建应用程序 我创建了一个带有字段的设置屏幕,用于编辑基本URL并将其存储在SharedReferences中。我的问题是:是否可以在每次调用时用SharedReferences中的此值替换基本URL 实际上,它不必是SharedR
SharedReferences
中。我的问题是:是否可以在每次调用时用SharedReferences
中的此值替换基本URL
实际上,它不必是SharedReferences
。我只需要能够配置基本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基本身份验证。
如果不需要Auth,请删除httpClient
和getInterceptorWithAuth()
方法
用法:
call = RestClient.get(context).whateverMethod();
如文档中所述,url通常将在reformation.Builder
类中使用baseUrl(String baseUrl)
方法设置。从SharedReferences
读取url是非常好的。请注意错误的url处理。那么你的问题是什么?我会用我的代码更新,你会看到问题。我正在静态地创建改造
客户端。因此,我无法访问上下文
。请看一看,并感谢您。你让我把问题打出来,为我指明了正确的方向!!我稍微重构了上面的代码,它工作得非常好。