Created
February 25, 2016 13:14
-
-
Save MaartenS/7c2413a63a06993fe477 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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