Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Interceptor the team sounds like something which is used in between the source and destination. In another language, an interceptor can hijack HTTP requests before they reach the endpoint.
By the Wikipedia,
In the field of software development, an interceptor pattern is a software design pattern that is used when software systems or frameworks want to offer a way to change, or augment, their usual processing cycle. Key aspects of the pattern are that the change is transparent and used automatically. In essence, the rest of the system does not have to know something has been added or changed and can keep working as before. To facilitate this, a predefined interface for extension has to be implemented, some kind of dispatching mechanism is required where interceptors are registered (this may be dynamic, at runtime, or static, e.g. through configuration files) and context objects are provided, which allow access to the framework’s internal state.
Step 1: Create a retrofit client,
public static Retrofit getClient(final Context mContext) { retrofit = new Retrofit.Builder() .baseUrl(ApplicationConstant.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } 123456789 public static Retrofit getClient(final Context mContext) { retrofit = new Retrofit.Builder() .baseUrl(ApplicationConstant.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; }
Step 2: Create interceptor,
OkHttpClient.Builder oktHttpClient = new OkHttpClient.Builder() .connectTimeout(ApplicationConstant.DEFAULT_CONNECT_TIMEOUT_IN_MIN, TimeUnit.MINUTES) .writeTimeout(ApplicationConstant.DEFAULT_WRITE_TIMEOUT_IN_MIN, TimeUnit.MINUTES) .readTimeout(ApplicationConstant.DEFAULT_READ_TIMEOUT_IN_MIN, TimeUnit.MINUTES); 1234 OkHttpClient.Builder oktHttpClient = new OkHttpClient.Builder() .connectTimeout(ApplicationConstant.DEFAULT_CONNECT_TIMEOUT_IN_MIN, TimeUnit.MINUTES) .writeTimeout(ApplicationConstant.DEFAULT_WRITE_TIMEOUT_IN_MIN, TimeUnit.MINUTES) .readTimeout(ApplicationConstant.DEFAULT_READ_TIMEOUT_IN_MIN, TimeUnit.MINUTES);
Step 3: Add interceptor,
oktHttpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); Request request = original.newBuilder() .header("token", AppSharedPref.getToken(mContext)) .method(original.method(), original.body()) .build(); return chain.proceed(request); } }); 1234567891011 oktHttpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); Request request = original.newBuilder() .header("token", AppSharedPref.getToken(mContext)) .method(original.method(), original.body()) .build(); return chain.proceed(request); } });
Step 4: Add to the retrofit client,
retrofit = new Retrofit.Builder() .baseUrl(ApplicationConstant.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(oktHttpClient.build()) // add this line .build(); 12345 retrofit = new Retrofit.Builder() .baseUrl(ApplicationConstant.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(oktHttpClient.build()) // add this line .build();
Thanks for reading this blog, You have any questions or problems comment below.
Happy Coding. Stay Super.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.