Our team decide to adopt Retrofit 2.0 and I'm doing some initial research on it. I'm a newbie to this library and hope to get some help from the community:)
As stated in the title, I'm wondering how to use
interceptor
interceptor
Interceptor
Chain
intercept()
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();
Check this out.
public class HeaderInterceptor
implements Interceptor {
@Override
public Response intercept(Chain chain)
throws IOException {
Request request = chain.request();
request = 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;
}
}