Created
April 26, 2018 09:04
-
-
Save AkshayMoorthy/9848525038b0e3555683fefde34281f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); | |
//Basic Auth | |
if (!TextUtils.isEmpty(username) | |
&& !TextUtils.isEmpty(password)) { | |
authToken = Credentials.basic(username, password); | |
} | |
//Create a new Interceptor. | |
Interceptor headerAuthorizationInterceptor = new Interceptor() { | |
@Override | |
public okhttp3.Response intercept(Chain chain) throws IOException { | |
okhttp3.Request request = chain.request(); | |
Headers headers = request.headers().newBuilder().add("Authorization", authToken).build(); | |
request = request.newBuilder().headers(headers).build(); | |
return chain.proceed(request); | |
} | |
}; | |
//Add the interceptor to the client builder. | |
clientBuilder.addInterceptor(headerAuthorizationInterceptor); | |
return new Retrofit.Builder().baseUrl(ROOT_URL) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment