-
-
Save nikhiljha/52d45ca69a8415c6990d2a63f61184ff to your computer and use it in GitHub Desktop.
// By Nikhil Jha | |
// License MIT | |
// DON'T ADD THIS FILE TO YOUR OTHER CLASSES | |
// THIS IS AN EXAMPLE TO TEACH YOU HOW TO MAKE REQUESTS WITH THE MAGIC COOKIES | |
OkHttpClient client = new OkHttpClient(); | |
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | |
builder.addInterceptor(new AddCookiesInterceptor(context)); // VERY VERY IMPORTANT | |
builder.addInterceptor(new ReceivedCookiesInterceptor(context)); // VERY VERY IMPORTANT | |
client = builder.build(); | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("my-base") // REQUIRED | |
.client(client) // VERY VERY IMPORTANT | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); // REQUIRED | |
Your.ThingyClass = retrofit.create(Your.ThingyClass.class); | |
Call<...> call = yourclass.somecall(new yourclass(example, example)); |
Thank you very much, This work for me.
Lifesaver !! Thank you.
Anyone has problem on Android Pie with these interceptors? It seems that they are not triggered on Android 9, on other versions it works perfectly..
It seems that the problem was in CertificateFactory.getInstance("X.509", "BC"), X.509 is not found for Android 9. CertificateFactory.getInstance("X509") is the fix.
Thanks, Worked for me
Thanks Bro, its working.. :)
Great help. Thanks Bro.
yo bro thanks ,,, its working, you saved my ass :D
Great help. Thanks
I'm using hilt and am struggling to make 'context' available. Any suggestions?
I got it. My problem was trying to use @ApplicationContext as is (@ApplicationContext context: ApplicationContext). It works fine if you leave it as Context (@ApplicationContext context: Context)
I've pulled everything together for a Hilt-based solution at https://stackoverflow.com/questions/67950681/using-cookies-with-retrofit-and-hilt-and-recommended-architecture
@makiabuan the interceptors needs to be added once in the http client. Make sure that all requests are using the same okhttp client.
You do not need to call anything. The interceptor does the work by itself.