Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use interceptor to add Headers in Retrofit 2.0?

Our team decide to adopt Retrofit 2.0 and I'm doing some initial research on it. I'm a newbie to this library.

I'm wondering how to use interceptor to add customized headers via Retrofits 2.0 in our Android app. There are many tutorials about using interceptor to add headers in Retrofit 1.X, but since the APIs have changed a lot in the latest version, I'm not sure how to adapt those methods in the new version. Also, Retrofit hasn't update its new documentation yet.

For example, in the following codes, how should I implement the Interceptor class to add extra headers? Besides, what exactly is the undocumented Chain object? When will the intercept() be called?

    OkHttpClient client = new OkHttpClient();     client.interceptors().add(new Interceptor() {         @Override         public Response intercept(Chain chain) throws IOException {             Response response = chain.proceed(chain.request());              // How to add extra headers?              return response;         }     });      Retrofit retrofit = new Retrofit.Builder()             .baseUrl(BASE_API_URL)             .client(client)             .addConverterFactory(GsonConverterFactory.create())             .build(); 
like image 435
hackjutsu Avatar asked Oct 06 '15 06:10

hackjutsu


People also ask

How do I put the OkHttp interceptor in retrofit?

OkHttp and Retrofit Client With the interceptors created, we need to build the OkHttpClient and add the Encryption and Decryption interceptors. We need to be mindful of the order in which we add the interceptors. Ideally, a request is first made to the server which in turn responds with a response.

What is the use of interceptor in retrofit Android?

Interceptors, according to the documentation, are a powerful mechanism that can monitor, rewrite, and retry the API call. So, when we make an API call, we can either monitor it or perform some tasks. In a nutshell, Interceptors function similarly to airport security personnel during the security check process.

What is network interceptor in retrofit?

Network InterceptorsAble to operate on intermediate responses like redirects and retries. Not invoked for cached responses that short-circuit the network. Observe the data just as it will be transmitted over the network. Access to the Connection that carries the request.


1 Answers

Check this out.

public class HeaderInterceptor implements Interceptor {     @Override     public Response intercept(Chain chain) throws IOException {         Request request = chain.request()                 .newBuilder()                 .addHeader("appid", "hello")                 .addHeader("deviceplatform", "android")                 .removeHeader("User-Agent")                 .addHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0")                 .build();         Response response = chain.proceed(request);         return response;     } } 

Kotlin

class HeaderInterceptor : Interceptor {     override fun intercept(chain: Interceptor.Chain): Response = chain.run {         proceed(             request()                 .newBuilder()                 .addHeader("appid", "hello")                 .addHeader("deviceplatform", "android")                 .removeHeader("User-Agent")                 .addHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0")                 .build()         )             } } 
like image 173
EpicPandaForce Avatar answered Sep 23 '22 00:09

EpicPandaForce