I am using MVP Pattern for my android app, I need to add access token to my request headers. The access token is saved in the SharedPreferences. How to access that SharedPreferences in MVP Pattern. I am using Retrofit for network request.
public class RetrofitInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "http://123124.ngrok.io/api/";
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
okhttpBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder newRequest = request.newBuilder().addHeader("Authorization", "Bearer "); //need to add value from SharedPreferences
return chain.proceed(newRequest.build());
}
});
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okhttpBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}