Skip to content

Instantly share code, notes, and snippets.

@MaartenS
Created February 25, 2016 13:14
Show Gist options
  • Select an option

  • Save MaartenS/7c2413a63a06993fe477 to your computer and use it in GitHub Desktop.

Select an option

Save MaartenS/7c2413a63a06993fe477 to your computer and use it in GitHub Desktop.
String credentials = "username:password";
final String basic =
"Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().clear();
List<Interceptor> interceptors = new ArrayList<>();
interceptors.add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Accept", "application/json")
.header("Authorization", basic)
.method(original.method(), original.body()).build();
Response response = chain.proceed(request);
return response;
}
});
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
interceptors.add(logInterceptor);
}
httpClient.interceptors().addAll(interceptors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment