這是我的單例類。使用單例類改進動態基礎URL更改
public class GetRetrofit {
static volatile Retrofit retrofit = null;
public static Retrofit getInstance() {
if (retrofit == null) {
synchronized (GetRetrofit.class) {
if (retrofit == null) {
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.readTimeout(30, TimeUnit.SECONDS);
builder.connectTimeout(30, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
// builder.addInterceptor(new UnauthorisedInterceptor(context));
OkHttpClient client = builder.build();
retrofit =
new Retrofit.Builder().baseUrl("DYNAMIC_URL")
.client(client).addConverterFactory(GsonConverterFactory.create()).build();
//addConverterFactory(SimpleXmlConverterFactory.create())
}
}
}
return retrofit;
}
}
我想更改動態基本網址。
例如:http://192.168.1.60:8888/property/Apiv1需要在運行時更改此網址http://192.168.1.50:8008/inventory/Apiv1。
如何在運行時動態更改這兩個網址。請幫幫我。