I am using okhttp 2.0 in my Android app and didn\'t find a way to set some common User Agent for all outgoing requests.
I thought I could do something like
You have to use builder
in newer versions. (Jan 2021)
client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request originalRequest = chain.request();
Request requestWithUserAgent = originalRequest.newBuilder()
.header("User-Agent", "My Agent is so cool")
.build();
return chain.proceed(requestWithUserAgent);
}
})
.build();