想請教各位大大!請問該如何修改 可以讓使用者自行修改URL並傳入到這一個 CLASS?
public class RetrofitBuilder {
private static final String BASE_URL = "http://"+"127.0.0.1"+"/api/";
private final static OkHttpClient client = buildClient();
private final static Retrofit retrofit = buildRetrofit(client);
private static OkHttpClient buildClient(){
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder()
.addHeader("Accept","application/json")
.addHeader("Connection","close");
request = builder.build();
return chain.proceed(request);
}
});
if (BuildConfig.DEBUG){
builder.addNetworkInterceptor(new StethoInterceptor());
}
return builder.build();
}
private static Retrofit buildRetrofit(OkHttpClient client){
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(MoshiConverterFactory.create())
.build();
}
public static <T> T createService(Class<T> service){
return retrofit.create(service);
}
public static <T> T createServiceWithAuth(Class<T> service, final TokenManger tokenManger){
OkHttpClient newClient = client.newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder();
if(tokenManger.getToken().getAccessToken() != null){
builder.addHeader("Authorization","Bearer " + tokenManger.getToken().getAccessToken());
}
request = builder.build();
return chain.proceed(request);
}
}).authenticator(CustomAuthenticator.getInstance(tokenManger)).build();
Retrofit newRetrofit =retrofit.newBuilder().client(newClient).build();
return newRetrofit.create(service);
}
public static Retrofit getRetrofit() {
return retrofit;
}
}