Created
July 29, 2016 04:35
-
-
Save nikhiljha/52d45ca69a8415c6990d2a63f61184ff to your computer and use it in GitHub Desktop.
Retrofit2/OkHttp3 Cookies (Drag and Drop, One Size Fits 99%)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great help. Thanks