我需要在运行时更改基础 URL。 我有登录按钮,当点击登录按钮时,我会调用我的登录 API,如下所示:
登录 API = http://192.168.0.61/api/authenticate API_BASE_URL = http://192.168.0.61/api/ 当我从第一个 API 中获得成功响应时,我会获取客户端服务器 URL 以更改 baseUrl。
CompanyUrlConfigEntity 公司URL配置实体 = response.body();
如下所示:
String clientUrl = companyUrlConfigEntity.getBaseUrl();
在这个项目中,主要是为客户或公司提供服务,因此他们有自己的服务器。每个公司都使用 20 多个 API。所以我需要改变基本 URL。
我还查看了下面的链接以更改基本 URL:
当我进行调试并检查我的baseUrl时,它会正确显示如下:
登录 API = http://192.168.0.61/api/authenticate API_BASE_URL = http://192.168.0.61/api/ 当我从第一个 API 中获得成功响应时,我会获取客户端服务器 URL 以更改 baseUrl。
CompanyUrlConfigEntity 公司URL配置实体 = response.body();
如下所示:
String clientUrl = companyUrlConfigEntity.getBaseUrl();
clientUrl = http://192.168.0.238/api/
在这个项目中,主要是为客户或公司提供服务,因此他们有自己的服务器。每个公司都使用 20 多个 API。所以我需要改变基本 URL。
我还查看了下面的链接以更改基本 URL:
https://futurestud.io/tutorials/retrofit-2-how-to-change-api-base-url-at-runtime-2
并且改变了代码像这样
public static void changeApiBaseUrl(String newApiBaseUrl) {
API_BASE_URL = newApiBaseUrl;
builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(new NullOnEmptyConverterFactory())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()));
}
当我进行调试并检查我的baseUrl时,它会正确显示如下:
API_BASE_URL = http://192.168.0.238/api/
But when i call my customer api it shows the my first base url calling,
the url not changed.
expected customer api : http://192.168.0.238/api/customers
reality customer api : http://192.168.0.61/api/customers
I am also checked below link :
https://futurestud.io/tutorials/retrofit-2-how-to-use-dynamic-urls-for-requests
thats working , But each api need to pass fullPath url with each api like below:
@GET
public Call<ResponseBody> profilePicture(@Url String url);
But using this method , each api calling place i need to attach full path of url.
There is any other options? Please help me.
ServiceGenerator.class
public class ServiceGenerator {
public static String API_BASE_URL = "http://192.168.0.61/api/";
private static Retrofit retrofit;
private static OkHttpClient.Builder httpClient = new
OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(new NullOnEmptyConverterFactory())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new
Gson()));
private ServiceGenerator() {
}
public static void changeApiBaseUrl(String newApiBaseUrl) {
API_BASE_URL = newApiBaseUrl;
builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(new NullOnEmptyConverterFactory())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()));
}
public static Retrofit retrofit() {
return retrofit;
}
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}
public static <S> S createService(Class<S> serviceClass,
final String authToken,
final ProgressListener progressListener) {
if (authToken != null) {
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
final String headerValue = AUTHORIZATION_TYPE + authToken;
Request request = original.newBuilder()
.header(AUTHORIZATION_HEADER_KEY, headerValue)
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
}
addResponseProgressListener(progressListener);
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor httpLoggingInterceptor = new
HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(httpLoggingInterceptor);
}
if (authToken != null) {
if (picasso == null) {
setUpPicasso(authToken);
}
}
OkHttpClient client = httpClient.build();
httpClient.connectTimeout(15, TimeUnit.SECONDS);
httpClient.readTimeout(2, TimeUnit.MINUTES);
httpClient.writeTimeout(2, TimeUnit.MINUTES);
retrofit = builder.client(client).build();
return retrofit.create(serviceClass);
}
}
LoginFragment.java
@OnClick(R.id.bt_login)
void onLogin() {
checkValidityOfUser();
}
private void checkValidityOfUser() {
final Login login = getLoginCredentials();
Call<CompanyUrlConfigEntity> callCheckValidity = dataProcessController.
getApiClient().
checkValidityOfUsers(login.getUsername());
callCheckValidity.enqueue(new Callback<CompanyUrlConfigEntity>() {
@Override
public void onResponse(Call<CompanyUrlConfigEntity> call,
Response<CompanyUrlConfigEntity> response) {
if (response.code() == 200) {
CompanyUrlConfigEntity companyUrlConfigEntity = response.body();
boolean status = companyUrlConfigEntity.isValidUser();
if (status) {
String baseUrls = companyUrlConfigEntity.
getBaseUrl();
baseUrls = baseUrls + "/api/";
ServiceGenerator.changeApiBaseUrl(baseUrls);
logins();
} else {
ToastHelper.show("please contact admin");
}
} else {
ToastHelper.show("" + response.code() + response.message());
}
}
@Override
public void onFailure(Call<CompanyUrlConfigEntity> call, Throwable t) {
ToastHelper.show("please contact admin");
}
});
}
private void logins() {
login = getLoginCredentials();
Call<Void> callLogin = dataProcessController.
getApiClient().
login(login);
callLogin.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.code() == 200) {
} else if (response.code() == 401) {
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
}
});
}
ApiClient.java
public interface ApiClient {@POST("authenticate") Call<Void> login(@Body Login login); } }DataProcessController.java
public class DataProcessController {private ApiClient apiClient = null; private DataProcessController() { apiClient = ServiceGenerator.createService(ApiClient.class);}
} - jesto paul以下是BaseFragment的代码:
public abstract class BaseFragment extends Fragment {protected DataProcessController dataProcessController = DataProcessController .getDataProcessController();
} - jesto paul