New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add Interceptor/EventListener to all OkhttpClient #4228

Closed
Leaking opened this Issue Aug 22, 2018 · 6 comments

Comments

4 participants
@Leaking
Copy link

Leaking commented Aug 22, 2018

Maybe your project have serveral OkhttpClients,you need to add your custom Interceptor/EventListener to every OkhttpClients one by one. But some OkhttpClients come from 3rd library, and you can't add your custom Interceptor/EventListener to them.

Is there any way to add add Interceptor/EventListener to all OkhttpClients conveniently.

@yschimke

This comment has been minimized.

Copy link
Collaborator

yschimke commented Aug 23, 2018

Unfortunately not, you should file an issue with those libraries to allow you to share clients. As well common interceptors, it should provide benefits of sharing connections on the device.

@yschimke yschimke closed this Aug 23, 2018

@wuairc

This comment has been minimized.

Copy link

wuairc commented Oct 10, 2018

We also need this feature to do accounting and monitoring logic. Add interceptors case by case is error prone, not future proof, and sometimes not doable.

@swankjesse

This comment has been minimized.

Copy link
Member

swankjesse commented Oct 11, 2018

Consider using dependency injection? It's my favorite way to make configuration uniform within an application.

@Leaking

This comment has been minimized.

Copy link

Leaking commented Oct 12, 2018

We also need this feature to do accounting and monitoring logic. Add interceptors case by case is error prone, not future proof, and sometimes not doable.

Hi, recently, I develop a plugin based on asm and gradle transform api to insert global interceptor for every okhttpclient, you can find it here https://github.com/Leaking/Hunter#okhttp-plugin

You can have a try.

@wuairc

This comment has been minimized.

Copy link

wuairc commented Oct 15, 2018

Consider using dependency injection? It's my favorite way to make configuration uniform within an application.

Is that possible when the third party library that use OkHttpClient do not use DI and we can not change that situation ?

@wuairc

This comment has been minimized.

Copy link

wuairc commented Oct 15, 2018

We also need this feature to do accounting and monitoring logic. Add interceptors case by case is error prone, not future proof, and sometimes not doable.

Hi, recently, I develop a plugin based on asm and gradle transform api to insert global interceptor for every okhttpclient, you can find it here https://github.com/Leaking/Hunter#okhttp-plugin

You can have a try.

Yes, this is the ideal way to do it, but a little overkill. Will try it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment