I'm using Retrofit to do some requests to web services, but until recently everything went wrong and I started getting the exception mentioned in the title. I have a base class which basically looks like this:
protected BaseService(int version) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (cookieJar == null) {
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
cookieJar = new JavaNetCookieJar(cookieManager);
}
builder.cookieJar(cookieJar);
if (BuildConfig.IS_DEBUG) {
BASE_URL = "http://nasko.dev.trainingassetsgateway.com";
builder.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request newRequest = chain
.request()
.newBuilder()
.addHeader("Authorization", "Basic REMOVED")
.build();
return chain.proceed(newRequest);
}
});
} else {
BASE_URL = "https://trainingassetsgateway.com";
}
API_URL = BASE_URL + "/mapi/" + version + "/";
client = new Retrofit
.Builder()
.baseUrl(API_URL)
.client(okHttpClient = builder.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
The problem starts in the interceptor chain. I've traced it to the HttpEngine.createAddress method where a new instance of Address is being created.
As seen the host part of the URL is there, but later on in the next call in the stack the "uriHost" is null, while it should have been the same thing that is seen in the "Watches" window.
From there it reaches the point where the host is null and the exception is thrown. Am I doing something wrong or maybe missing something?