i used franmontiel PeristentCookieJar library for okhttp3 and retrofit.2. the benefit of this approach is : not need manipulate your okhttp request. Just set cookies or session when creating retrofit
1. first add this to your build.gradle(projectname)
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
2. add this to your build.gradle
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
3. build retrofit like this
public static Retrofit getClient(Context context) {
ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.build();
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit;
}