/**
* Somewhere you create a new OkHttpClient and use it on all your requests.
*/
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new AddCookiesInterceptor());
okHttpClient.interceptors().add(new ReceivedCookiesInterceptor());
import android.net.Uri;
import com.greenland.gclub.network.retrofit.CookieStore;
import java.io.IOException;
import java.util.HashSet;
import okhttp3.Interceptor;
import okhttp3.Response;
public class CookiesRecInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
String url = chain.request().url().toString();
String host = Uri.parse(url).getHost();
Response originalResponse = chain.proceed(chain.request());
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
CookieStore.setCookies(host, new HashSet<>(originalResponse.headers("Set-Cookie")));
}
return originalResponse;
}
}